printf-like formatting of floats and such

20 views
Skip to first unread message

Perry E. Metzger

unread,
Nov 22, 2015, 4:51:15 PM11/22/15
to swift-l...@googlegroups.com
Is there at this point a standard way to do things like printf-like
formatting of doubles and floats without having to call in to
Objective C?

Perry
--
Perry E. Metzger pe...@piermont.com

Jens Alfke

unread,
Nov 22, 2015, 6:16:24 PM11/22/15
to Perry E. Metzger, swift-l...@googlegroups.com

On Nov 22, 2015, at 1:51 PM, Perry E. Metzger <pe...@piermont.com> wrote:

Is there at this point a standard way to do things like printf-like
formatting of doubles and floats without having to call in to
Objective C?

Depends what you mean by ‘call into Objective-C’. You can do:
let str = NSString(format: “%d”, args: 123.456)

—Jens

Marco S Hyman

unread,
Nov 22, 2015, 6:17:58 PM11/22/15
to Perry E. Metzger, swift-l...@googlegroups.com
On Nov 22, 2015, at 1:51 PM, Perry E. Metzger <pe...@piermont.com> wrote:
>
> Is there at this point a standard way to do things like printf-like
> formatting of doubles and floats without having to call in to
> Objective C?

String(format: “format string”, val, val, val)

as in

print(String(format: "Value: %3.2f\tResult: %3.2f", arguments: [2.7, 99.8]))

Perry E. Metzger

unread,
Nov 22, 2015, 10:14:23 PM11/22/15
to Marco S Hyman, swift-l...@googlegroups.com
On Sun, 22 Nov 2015 15:17:55 -0800 Marco S Hyman <ma...@snafu.org>
wrote:
Very cool! But where is this documented? I don't seem to be able to
find it. Am I just missing the right place?

Jens Alfke

unread,
Nov 22, 2015, 10:30:57 PM11/22/15
to Perry E. Metzger, Marco S Hyman, swift-l...@googlegroups.com

On Nov 22, 2015, at 7:14 PM, Perry E. Metzger <pe...@piermont.com> wrote:

Very cool! But where is this documented? I don't seem to be able to
find it. Am I just missing the right place?

http://swiftdoc.org/v2.1/type/String/ … search for “format”

It’s a common NSString method, bridged to Swift. So you can also open NSString.h and see the Swift translation in the Counterparts assistant pane.

—Jens

Perry E. Metzger

unread,
Nov 23, 2015, 8:58:51 AM11/23/15
to Jens Alfke, swift-l...@googlegroups.com
On Sun, 22 Nov 2015 19:30:49 -0800 Jens Alfke <je...@mooseyard.com>
wrote:
Thank you! This will sound weird, but I had no idea swiftdoc.org
existed. The only documentation I knew of for the string class was in

https://developer.apple.com/library/ios//documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/struct/s:SS

and that doesn't mention format or anything like it.

Daniel T.

unread,
Nov 23, 2015, 9:19:48 AM11/23/15
to Swift Language, je...@mooseyard.com
Well technically, the answer Jens gave you does not answer the original question you asked. `String(format:)` is calling into Objective-C and you asked for a way to do it that doesn't do that.

Still, it exists and does what you need so there's no problem using it. 

Perry E. Metzger

unread,
Nov 23, 2015, 9:36:29 AM11/23/15
to Daniel T., Swift Language, je...@mooseyard.com
On Mon, 23 Nov 2015 06:19:48 -0800 (PST) "Daniel T."
<danie...@gmail.com> wrote:
> Well technically, the answer Jens gave you does not answer the
> original question you asked. `String(format:)` is calling into
> Objective-C and you asked for a way to do it that doesn't do that.
>
> Still, it exists and does what you need so there's no problem using
> it.

Hrm. I note that this takes an array of CVarArgType. Does that mean
that you're potentially type unsafe if there is a mismatch
between the passed argument and the format string, just as in C?

Daniel T.

unread,
Nov 23, 2015, 9:43:04 AM11/23/15
to Swift Language, danie...@gmail.com, je...@mooseyard.com
The answer is a qualified "no." The compiler understands the format specifiers, checks the types and will emit warnings to let you know if the types don't match the specifiers. Of course if you are in the habit of ignoring warnings you still might have a problem.

Perry E. Metzger

unread,
Nov 23, 2015, 10:58:05 AM11/23/15
to Daniel T., Swift Language, je...@mooseyard.com
On Mon, 23 Nov 2015 06:43:04 -0800 (PST) "Daniel T."
<danie...@gmail.com> wrote:
> On Monday, November 23, 2015 at 9:36:29 AM UTC-5, Perry Metzger
> wrote:
> >
> > On Mon, 23 Nov 2015 06:19:48 -0800 (PST) "Daniel T."
> > <danie...@gmail.com <javascript:>> wrote:
> > > Well technically, the answer Jens gave you does not answer the
> > > original question you asked. `String(format:)` is calling into
> > > Objective-C and you asked for a way to do it that doesn't do
> > > that.
> > >
> > > Still, it exists and does what you need so there's no problem
> > > using it.
> >
> > Hrm. I note that this takes an array of CVarArgType. Does that
> > mean that you're potentially type unsafe if there is a mismatch
> > between the passed argument and the format string, just as in C?
>
> The answer is a qualified "no." The compiler understands the format
> specifiers, checks the types and will emit warnings to let you know
> if the types don't match the specifiers. Of course if you are in
> the habit of ignoring warnings you still might have a problem.

I'm surprised that they're just warnings and not fatal errors then
(since Swift is supposed to be strictly typed). Is that because
there are times when you need to still violate the type discipline
when using formatting strings?

Daniel Tartaglia

unread,
Nov 23, 2015, 11:34:20 AM11/23/15
to Perry E. Metzger, Swift Language, je...@mooseyard.com
On Nov 23, 2015, at 10:57 AM, Perry E. Metzger <pe...@piermont.com> wrote:
> On Mon, 23 Nov 2015 06:43:04 -0800 (PST) "Daniel T.” <danie...@gmail.com> wrote:
>> On Monday, November 23, 2015 at 9:36:29 AM UTC-5, Perry Metzler wrote:
>>>
>>> Hrm. I note that this takes an array of CVarArgType. Does that
>>> mean that you're potentially type unsafe if there is a mismatch
>>> between the passed argument and the format string, just as in C?
>>
>> The answer is a qualified "no." The compiler understands the format
>> specifiers, checks the types and will emit warnings to let you know
>> if the types don't match the specifiers. Of course if you are in
>> the habit of ignoring warnings you still might have a problem.
>
> I'm surprised that they're just warnings and not fatal errors then
> (since Swift is supposed to be strictly typed). Is that because
> there are times when you need to still violate the type discipline
> when using formatting strings?

I think it is more because it’s an Objective-C thing rather than a Swift thing.

Jens Alfke

unread,
Nov 23, 2015, 12:04:19 PM11/23/15
to Daniel Tartaglia, Perry E. Metzger, Swift Language
The open-source release of Swift will need to include a self-contained standard library with printf-type formatting baked into the String class itself. Maybe this will include stronger type-checking.

Also: wouldn't a pure-Swift implementation be able to determine the actual type of each parameter (by using “as?”)? In that case it could detect mismatches at runtime and abort (instead of reading garbage off the stack.) Better yet, it could handle some types of mismatches intelligently — so if you specified “%d” but passed a Float, it could print it correctly. (The Go language’s printf does this.)

—Jens

Perry E. Metzger

unread,
Nov 23, 2015, 12:35:36 PM11/23/15
to Jens Alfke, Daniel Tartaglia, Swift Language
On Mon, 23 Nov 2015 09:04:13 -0800 Jens Alfke <je...@mooseyard.com>
wrote:
I would prefer, in the latter case, to get a compile error so that the
mistake isn't silently left in the code waiting to cause trouble. (A
language can be *too* helpful.)

That said, it would be interesting if Swift grew a typesafe mechanism
to permit varargs of non-uniform type. Presumably the use of runtime
type information and type inquiry operators would allow this.
Reply all
Reply to author
Forward
0 new messages