• 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: formatted strings in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: formatted strings in Swift


  • Subject: Re: formatted strings in Swift
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Sat, 03 Sep 2016 14:28:34 +0700

> On 3 Sep 2016, at 12:22, Quincey Morris <email@hidden> wrote:
>
> 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.

Sorry. This I should have known.

But:
let scom3 = String(format: "%9@", "a")
print(“formatted %9@ = \”\(scom3)\"")	→ formatted %9@ = "a"
I.e. the size parameter 9 seems to be ignored. (I kind of remember having the same problem in Objective-C).

>
>> How to create a formatted string?

[…]

> 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.

Ugly, but better than all the other work-arounds I have tried:

let scom1 = String(format: "%9s", ("a" as NSString).UTF8String)
print(“formatted %9s = \”\(scom1)\”")	→ formatted %9s = "        a"


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


  • Follow-Ups:
    • Re: formatted strings in Swift
      • From: Quincey Morris <email@hidden>
References: 
 >formatted strings in Swift (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: formatted strings in Swift (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: formatted strings in Swift
  • Next by Date: Re: formatted strings in Swift
  • Previous by thread: Re: formatted strings in Swift
  • Next by thread: Re: formatted strings in Swift
  • Index(es):
    • Date
    • Thread