4

After watching a lot of the WWDC videos and reading through the Swift iBook, I came away with the impression that CGFloat was somewhat... legacy, due to the emphasis on Int and Double/Float. Perhaps somewhat comparable to NSString vs String.

I wrote a bit of code with the latter, and Double won't compile on non-64 bit devices. Int seems fine, though.

Should we still be using CGFloat for all math? Has anyone found anything that would indicate we'd be able to use the native Swift numerics? Because right now they're essentially unusable outside of scripts.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 1
    This is a known problem, they are working to fix Double/Float vs CGFloat related issues...See https://github.com/ksm/SwiftInFlux#numerical-data-type-conversion-eg-cgfloat-and-swift-doubleswift-float – Jack Jul 08 '14 at 17:52
  • 2
    *"Double won't compile on non-64 bit devices"* - this is not true. But on 32-bit devices, `Double` is 64-bit and `CGFloat = Float` is 32-bit, and you have to cast explicitly between them. – Martin R Jul 08 '14 at 17:56
  • Martin: That is inaccurate; on 64-bit devices, `CGFloat = Double = 64-bit`. That's the problem: you cannot assume CGFloat is Double or Float, because it can be either, and Swift's strictness with numeric types turns this into a headache. – Wes Campaigne Jul 08 '14 at 18:12
  • 2
    @WesCampaigne: I just said that on *32-bit devices*, Double = 64-bit and CGFloat = Float = 32-bit. What is inaccurate about that? – Martin R Jul 08 '14 at 18:31
  • Closely related: [Swift numerics and CGFloat, CGPoint, CGRect, etc.](http://stackoverflow.com/q/24108827) – jscs Jul 08 '14 at 18:32
  • 1
    Oh, sorry, never mind. I missed your "But on 32-bit devices" and consequently misunderstood the point of your comment. My apologies. – Wes Campaigne Jul 08 '14 at 19:00

2 Answers2

3

According to Chris Lattner, they recognize this problem and will have an official solution soon. I'd wait until this solution is fixed before deciding whether or not to use CGFloat.

We're aware of this problem and consider it to be serious: we are evaluating several different solutions right now and will roll one out in a later beta. As you notice, you can cope with this today by casting to Double. This is inelegant but effective :-)

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • How is it effective? I'm using `Double` now and it seems to choke on 32 bit devices. – Doug Smith Jul 08 '14 at 18:30
  • 2
    @DougSmith: Can you post some concrete code demonstrating the problem? – Martin R Jul 08 '14 at 18:32
  • You're right, I think I just wasn't casting universally. Thanks Martin. – Doug Smith Jul 08 '14 at 18:54
  • @MartinR So what is the solution in the post connor linked to? https://devforums.apple.com/message/998222#998222 How do you make that initial code block work on both 32 bit and 64 bit? – Doug Smith Jul 08 '14 at 19:50
  • @DougSmith: With `let (dx,dy) = (CGFloat (x), CGFloat(y))` the code compiles on both. - In fact that particular problem seems unrelated to "CGFloat different on 32/64-bit" to me. If CGFloat were Double on all archs then you also would have to cast the *integers* x, y to Double. – Martin R Jul 09 '14 at 07:10
0

I faced the problem when I was "fetching" from UserDefaults.

I saved float data like:

let floatValue: CGFloat = 1.3
UserDefaults.standard.set(floatValue, forKey: "Key")

Then I wanted to fetch it and parse it as a CGFloat

let floatValue = CGFloat(UserDefaults.standard.float(forKey: "Key"))

It made from 1.3 value - 1.29999...

So when you save/load to/from UserDefaults you should use Float. Otherwise, I use CGFloat and there is no problem with it.

Daveloper
  • 606
  • 5
  • 12