Skip to content

Instantly share code, notes, and snippets.

@indragiek
Created January 29, 2015 02:48
Show Gist options
  • Save indragiek/6b500e84927f0be9c2ae to your computer and use it in GitHub Desktop.
Save indragiek/6b500e84927f0be9c2ae to your computer and use it in GitHub Desktop.
Swift bug when formatting CGFloats
import Cocoa
// Doesn't work for CGFloats
let a: CGFloat = 1.0
let b: CGFloat = 2.0
String(format: "%f %f", a, b) // "0.000000 0.000000"
// Works for Floats
let c: Float = 1.0
let d: Float = 2.0
String(format: "%f %f", c, d) // "1.000000 2.000000"
// Works for Doubles
let e: Double = 1.0
let f: Double = 2.0
String(format: "%f %f", e, f) // "1.000000 2.000000"
@indragiek
Copy link
Author

@bavarious Yep, it is using the NSString initializer. The public interface for Swift's String does not have that method defined on it, it just uses the bridging. Here's another interesting bit:

// The following code crashes
let numbers = [a, b]
String(format: "%f %f", arguments: numbers)

(This is using the version of the initializer that allows passing the arguments as an array as opposed to a variadic argument list)

@natecook1000
Copy link

Swift's String is extended in Foundation to include most of NSString's initializers—you can see all of them by command-clicking on an import Foundation statement or on the SwiftDoc.org String page marked with [Foundation].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment