Created
January 29, 2015 02:48
-
-
Save indragiek/6b500e84927f0be9c2ae to your computer and use it in GitHub Desktop.
Swift bug when formatting CGFloats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
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
@bavarious Yep, it is using the
NSString
initializer. The public interface for Swift'sString
does not have that method defined on it, it just uses the bridging. Here's another interesting bit:(This is using the version of the initializer that allows passing the arguments as an array as opposed to a variadic argument list)