1

Is it really necessary to bridge to objective c like i'm doing for my Doubles below? Seems to work without this step in the playground but doesn't compile in a regular swift file.

Is there a cleaner way?

   var prevX = 0.0
    var prevY = 0.0
    var count = 0

    for item : AnyObject in myGraph.vertices {
        count++
        if let v = item as? Vertex {

            x = Double(x)
            y = Double(y)

            x = x - width / 2.0
            y = y - width / 2.0
            //var fwidth = Float(width)

            var rect = CGRect()
            rect.origin.x = x.bridgeToObjectiveC().floatValue
            rect.origin.y = y.bridgeToObjectiveC().floatValue
            rect.size.width = width.bridgeToObjectiveC().floatValue
            rect.size.height = rect.size.width
Justin Milo
  • 575
  • 4
  • 15
  • 1
    https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_416 – Robert Harvey Jun 13 '14 at 00:03
  • Appears to be related to whether the target is 64 bit or 32 bit – Justin Milo Jun 13 '14 at 00:20
  • Double is a 64 bit number, always has been. Float is a 32 bit number, always has been. See http://en.wikipedia.org/wiki/Double-precision_floating-point_format and http://en.wikipedia.org/wiki/Single-precision_floating-point_format – Robert Harvey Jun 13 '14 at 01:36
  • CGFloat (the constituents of a CGRect) is 32-bit when building for 32-bit architectures, 64-bit when building for 64-bit architectures. This can surprise people. I'm [trying to get a handle on dealing with this in current Swift](http://stackoverflow.com/questions/24184810/should-conditional-compilation-be-used-to-cope-with-difference-in-cgfloat-on-dif) but it wouldn't surprise me if Apple adds some more helpful glue for this in future releases of Swift. – Matt Gibson Jun 13 '14 at 06:33
  • That's fine (Float 32 and Double 64). But never had to deal with this problem in Objective C for some reason. – Justin Milo Jun 13 '14 at 20:11

1 Answers1

2

No, you can use a float constructor to do it

rect.origin.x = Float(x)
rect.origin.y = Float(y)
rect.size.width = Float(width)
rect.size.height = rect.size.width

N.B. I've opened a bug report with Apple about this. At the least, the behavior should match.

Kevin
  • 53,822
  • 15
  • 101
  • 132