String(format:)

17 views
Skip to first unread message

Marco S Hyman

unread,
May 22, 2015, 7:40:45 PM5/22/15
to swift-l...@googlegroups.com
Where is String(format:) defined?

var foo = String(format: "A format string with value %03d", 42)

Is it automagically bridged from NSString stringWithFormat:?

I’ve seen comments that String(format:) is “native” which is what leads me
to ask the question.

Marc

Adam Sharp

unread,
May 23, 2015, 3:22:01 AM5/23/15
to swift-l...@googlegroups.com
On 23 May 2015, at 9:40 am, Marco S Hyman <ma...@snafu.org> wrote:

> Is it automagically bridged from NSString stringWithFormat:?

Yes it is:

1> var foo = String(format: "A format string with value %03d", 42)
repl.swift:1:11: error: cannot find an initializer for type 'String' that accepts an argument list of type '(format: String, Int)'
var foo = String(format: "A format string with value %03d", 42)
^

1> import Foundation
2> var foo = String(format: "A format string with value %03d", 42)
foo: String = "A format string with value 042"

–Adam

Fritz Anderson

unread,
May 23, 2015, 11:38:06 AM5/23/15
to Marco S Hyman, swift-l...@googlegroups.com
The Swift interface for String includes String(format:, ...) only when you import Foundation. (You know the trick of `import Swift`, then command-clicking `Swift`.) I'm pleased to see that SwiftDoc includes the Foundation extensions on its String page. http://swiftdoc.org/type/String/ . There it is tagged as part of the extension.

Naïvely, I'd suppose it goes straight to Foundation, and to Core Foundation from there, but I could imagine something so common being special-cased in the Swift libraries if it made a difference in performance. A symbolic breakpoint on the NSString method might tell the tale. I wonder if special-casing could become common as time goes by.

Consider using string interpolation if you can tolerate the default formats. It would be interesting to see whether using NSNumberFormatter yourself would be a win.

— F

Marco S Hyman

unread,
May 23, 2015, 1:53:34 PM5/23/15
to Fritz Anderson, swift-l...@googlegroups.com
On May 23, 2015, at 8:37 AM, Fritz Anderson <fri...@manoverboard.org> wrote:
>
> The Swift interface for String includes String(format:, ...) only when you import Foundation. (You know the trick of `import Swift`, then command-clicking `Swift`.)

Yup. That’s where I went looking for String(format:) Didn’t see it there.
Where I didn’t look (and should have) is in Foundation. I see I have more
stuff to study.

> I'm pleased to see that SwiftDoc includes the Foundation extensions on its String page. http://swiftdoc.org/type/String/ . There it is tagged as part of the extension.

I had assumed that SwiftDoc was the same as looking at Swift.swift.
My 2nd error. I should probably pay closer attention to SwiftDoc.

> Consider using string interpolation if you can tolerate the default formats. It would be interesting to see whether using NSNumberFormatter yourself would be a win.

I was mostly interested in “where is this stuff defined” as opposed to
any particular use. For most quick and dirty debug where I drop a println
into a function to see if values are as expected string interpolation works
quite well.

Thanks for the reply

Marc
Reply all
Reply to author
Forward
0 new messages