1

My game builds and runs successfully on the iPhone 5s simulator, but when I try on any other version I get the following two errors:

`Could not find an overload for '*' that accepts the supplied arguments`
`Could not find an overload for '/' that accepts the supplied arguments`

I'm writing my game entirely in Swift, and the deployment target is iOS 7.1

The die rolls in the picture are defined as

let lengthDiceroll = Double(arc4random()) / 0x100000000
let sideDiceroll = Int(arc4random_uniform(UInt32(4)))

enter image description here

JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • Swift is still beta, you are more likely to get a response in the Apple Developer forums for SWIFT. – Cliff Ribaudo Jun 22 '14 at 14:18
  • I'm not getting any of those errors running that snippet ona 5s and 4s simulator – Connor Pearson Jun 22 '14 at 14:26
  • So, it works when CGFloat is 64-bit, but not when it's 32-bit? (`CGPointMake` takes `CGFloat` arguments, and `CGFloat` is double on 64-bit targets, float on 32-bit targets...) – Matt Gibson Jun 22 '14 at 19:31
  • That's correct, because it also works on the iPad Air emulator – JuJoDi Jun 22 '14 at 19:57
  • @MattGibson Does that make the fix obvious? (it's not obvious to me still) – JuJoDi Jun 22 '14 at 23:54
  • Well, [this is how I'm dealing with it](http://stackoverflow.com/questions/24184810/should-conditional-compilation-be-used-to-cope-with-difference-in-cgfloat-on-dif), but it's possible that this is something that will be fixed up a little better in the language itself in later releases; I've seen quite a few people struggling with it. – Matt Gibson Jun 23 '14 at 06:38

2 Answers2

1

Your problem is a difference between 32- and 64-bit architecture. Note that the target architecture you're compiling your target for is determined by the selected device—if you've got the iPhone 4S simulator selected as your target in Xcode, for example, you'll be building for 32 bit; if you've got the iPhone 5S simulator selected, you'll be building for 64-bit.

You haven't included enough code to help us figure out what exactly is going on (we'd need to know the types of the variable you're assigning to) but here's my theory. In your first error, sprite.speed is probably a CGFloat. CGFloat is 32-bit ("float") on 32-bit targets, 64-bit ("double") on 64-bit targets. So this, for example:

var x:CGFloat = Double(arc4random()) / 0x100000000

...will compile fine on a 64-bit target, because you're putting a double into a double. But when compiling for a 32-bit target, it'll give you the error that you're getting, because you're losing precision by trying to stuff a double into a float.

This will work on both:

var x:CGFloat = CGFloat(arc4random()) / 0x100000000

Your other errors are caused by the same issue (though again, I can't reproduce them accurately without knowing what type you've declared width and height as.) For example, this will fail to compile for a 32-bit architecture:

    let lengthDiceroll = Double(arc4random()) / 0x100000000
    let width:CGFloat = 5
    var y:CGPoint = CGPointMake(width * lengthDiceroll, 0)

...because lengthDiceroll is a Double, so width * lengthDiceroll is a Double. CGPointMake takes CGFloat arguments, so you're trying to stuff a Double (64-bit) into a float (32-bit.)

This will compile on both architectures:

    let lengthDiceroll = Double(arc4random()) / 0x100000000
    let width:CGFloat = 5
    var y:CGPoint = CGPointMake(width * CGFloat(lengthDiceroll), 0)

...or possibly better, declare lengthDiceroll as CGFloat in the first place. It won't be as accurate on 32-bit architectures, but that's sort of the point of CGFloat:

    let lengthDiceroll = CGFloat(arc4random()) / 0x100000000
    let width:CGFloat = 5
    var y:CGPoint = CGPointMake(width * lengthDiceroll, 0)
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
0

I've experienced similar errors where debug builds work and release builds fail for example. My advice would be make all your types explicit:

let lengthDiceroll = Double(arc4random()) / Double(0x100000000)

I've also had similar problems with CGFloat and CGPoint, make sure you explicitly use CGFloat, e.g. CGFloat(2.0)

ColinE
  • 68,894
  • 15
  • 164
  • 232