So I am trying to convert a game I was making in Objective-C to Swift.
I am trying to get this to work, but it keeps giving me an error.
var actualX = (Double(arc4random() ) % Double(rangeX) ) + Double(minX);
I have also tried:
var actualX = (arc4random() % rangeX) + minX;
I have looked at the other posts on Stack Overflow of similar problems, but none of them have helped or solved my problem...
This works fine for me:
After execution actualX will be a Double because of type inference of Swift.
@otherRepliants: Of course arc4random() returns an UInt32, but why not cast it to Double. Works fine.
arc4random()
returns aUInt32
, any argument to your%
operator has to be a compatible type.Co-erce your variables to a
UInt32
. You don't mention what type they are, but I am assuming they are some form of integer. You can obviously coerce to another type later.This is a result of the strong typing in swift.