var x = jokes[arc4random_uniform(UInt32(jokes.count))]
Why does this line of code produce such error?
When this code is written
var x = jokes[Int(arc4random()%jokes.count)]
This error appear
Int is not convertible to UInt32
Array subscript takes an Int
, not a UInt32
. You need to convert back:
let x = jokes[Int(arc4random_uniform(UInt32(jokes.count)))]
If this gets a bit noisy for you, you may want to create a function to handle it:
func randomValueLessThan(x: Int) -> Int {
return Int(arc4random_uniform(UInt32(x)))
}
Or you could extend Array to help out:
extension Array {
func uniformSelection() -> T {
return self[Int(arc4random_uniform(UInt32(self.count)))]
}
}
EDIT: It is worth digging a little deeper into the Int(arc4random()%jokes.count)
case because you might be tempted to fix it incorrectly, and it demonstrates why Swift works the way it does.
Let's start with your version
let n = Int(arc4random() % jokes.count)
// => Could not find an overload for 'init' that accepts the supplied arguments
That's a little confusing. Let's simplify to see the problem
let n = arc4random() % jokes.count
// => Cannot invoke '%' with an argument list of type '(UInt32, Int)'
That should be clearer. arc4random
returns a UInt32
and jokes.count()
returns an Int
. You can't modulus different types. You need to get them to the same place. Well, we want an Int
, right? Seems easy:
let n = Int(arc4random()) % jokes.count // WARNING!!!! Never ever do this!!!!
Why is Apple so pedantic and forces us to do that by hand? Couldn't the compiler just cast it automatically? Well, the above code will work fine on a 64-bit processor and crash about half the time on a 32-bit processor. That's because by calling Int()
, you're promising that the value will always be in the Int
range. And on a 32-bit processor, that's the 32-bit signed range. But arc4random
returns a value in the whole 32-bit unsigned range, which includes lots of numbers that won't fit in an Int
. Blam! (Or if you turn off bounds checking, then it just trashes your numbers like it does in C, which is no better.)
That's why Swift is picky about integer conversions. You need to be absolutely certain when you convert that it's a safe conversion. You shouldn't just sprinkle them around until it compiles.
That said, of course you should never use modulus on arc4random
. But that's a different question.
As one more side note, you'll notice that the numeric casting in randomValueLessThan()
creates many possible invalid situations. If you pass a number less than 0 you'll crash (that shouldn't be surprising, though). If you pass a number greater than UInt32.Max
, you'll also crash, which is slightly more surprising, but pretty unlikely in most code. The point of this is that by adding these casts, we've made randomValueLessThan
a partial function. It's not defined over all of its range of inputs (it's "domain"). In "real life" programming, we do that all the time and we just hope it never bites us. But Swift is trying to help us get bitten even less by making it more obvious when you're breaking type safety.
uniformSelection
has a similar problem. It's only defined for arrays with fewer than UInt32.Max
elements. These sometimes feel like meaningless corner cases, and they are, until suddenly they aren't and your program crashes. (Array.subscript
is also a partial function, since it is undefined for values outside the array's range. I actually suggested to Apple that Array.subscript
return an optional to account for that. They were probably wise to ignore me.)