• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to convert String.Index to UInt?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to convert String.Index to UInt?


  • Subject: Re: How to convert String.Index to UInt?
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Sat, 16 Aug 2014 17:48:01 +0700

On 16 Aug 2014, at 16:25, Stephen J. Butler <email@hidden> wrote:

> Try:
>
> import Cocoa
>
> let s = "hallo\there"
> let aas = NSMutableAttributedString(string: s, attributes: nil)
>
> if let rangeOfTab = s.rangeOfString( "\t" ) {
>     let colour = NSColor.grayColor()
>     let length = distance(s.startIndex, rangeOfTab.startIndex)
>     let aRange = NSRange(location: 0, length: length)
>     aas.addAttribute(NSForegroundColorAttributeName, value: colour, range: aRange)
> }

Did try:

let s = "a 🚲 bicycle"
let endGray = "c"

if let rangeOfB = s.rangeOfString(endGray )
{
	let length = distance(s.startIndex, rangeOfB.startIndex)
	println( "\(endGray) at composed character index index \(length)")		//	6
}

let ns = s as NSString
let range = ns.rangeOfString(endGray)
let nsLength = range.location
println( "\(endGray)  at NSString index \(nsLength)")	//	7

Using "length" with addAttribute:value:range would NOT be quite correct, would it?
Seems like "nsLength" is more appropriate.

Kind regards,

Gerriet.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >How to convert String.Index to UInt? (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to convert String.Index to UInt? (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Creating pdf document
  • Next by Date: Class in Swift
  • Previous by thread: Re: How to convert String.Index to UInt?
  • Next by thread: Creating pdf document
  • Index(es):
    • Date
    • Thread