String(format:)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

String(format:)

Marco S Hyman
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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/D1AA2560-D8A4-4A9A-B84F-A3D4EDC57A87%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: String(format:)

Adam Sharp
On 23 May 2015, at 9:40 am, Marco S Hyman <[hidden email]> 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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/157B9AD6-1B1D-49FB-AAA8-D7C75790020F%40me.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: String(format:)

Fritz Anderson
In reply to this post by Marco S Hyman
> On May 22, 2015, at 6:40 PM, Marco S Hyman <[hidden email]> wrote:
>
> 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.

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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8D3D8001-ABD6-4F18-B1D1-873583A9AB1B%40manoverboard.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: String(format:)

Marco S Hyman
On May 23, 2015, at 8:37 AM, Fritz Anderson <[hidden email]> 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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/FCE0A225-412A-4933-9EAA-7379E17C9332%40snafu.org.
For more options, visit https://groups.google.com/d/optout.