Re: formatted strings in Swift
Re: formatted strings in Swift
- Subject: Re: formatted strings in Swift
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 03 Sep 2016 16:54:33 +0700
> On 3 Sep 2016, at 16:23, Quincey Morris <email@hidden> wrote:
>
> On Sep 3, 2016, at 02:06 , Gerriet M. Denkmann <email@hidden> wrote:
>>
>> By the way: what I really wanted to do is:
>> let s = String(format: “”, someString.endIndex )
>
> I don’t understand this. Is a typo for %9s? Or are we now talking about numbers instead of strings? I don’t get how endIndex suddenly came into this.
I wanted to print some index with a format.
let someIndex = someString.endIndex
let indexAsFormattedString = String(format: “", someIndex )
But was told: error: argument type ‘Index' (aka 'String.CharacterView.Index’) does not conform to expected type 'CVarArgType'
So I did:
let indexAsString = String(someIndex) // ok
and tried to print indexAsString with a format.
This you helped me to do via:
let indexAsFormattedString = String(format: “%9s”, (indexAsString as NSString).utf8String )
Which works, because numbers are plain Ascii.
> Also, what are you expecting String (someString.endIndex) to be and/or mean?
It has the type ’String’ and contains a number in 0…someString.utf16.count .
It seems to be an index into the utf16 representation of the string.
Which probably is an implementation detail not to be relied on, but somewhat useful for debugging.
But was really worries me is that fprintf can handle utf8String correctly even with non-Ascii, whereas [NSString stringWithFormat:] can not.
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