Re: formatted strings in Swift
Re: formatted strings in Swift
- Subject: Re: formatted strings in Swift
- From: Quincey Morris <email@hidden>
- Date: Fri, 02 Sep 2016 22:22:17 -0700
- Feedback-id: 167118m:167118agrif8a:167118snOXEO4QuZ:SMTPCORP
On Sep 2, 2016, at 22:02 , Gerriet M. Denkmann <email@hidden> wrote:
>
> Did in a playground:
>
> let s = String(format: "%2s → %2s", "a", "b")
> print(“formatted = \"\(s)\"")
>
> But this prints random garbage (e.g.: formatted = “‡“S → ‡“S”) (no error message or compiler warning).
>
> Why?
Because %s is the specifier for a C-string, and "a", "b" are Swift strings.
> How to create a formatted string?
Use the %@ specifier as you would for NSString, or use a string interpolation expression:
let s = “\(a) → \(b)”
If you absolutely must use %2s, the following works too (in a playground):
let s = String(format: "%2s → %2s", ("a" as NSString).utf8String!, ("b" as NSString).utf8String!)
but that’s a lot of ugly.
_______________________________________________
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