objective c implicit conversion loses integer precision 'NSUInteger' objective c implicit conversion loses integer precision 'NSUInteger' ios ios

objective c implicit conversion loses integer precision 'NSUInteger'


You can safely suppress the warning with a cast.

NSUInteger index = arc4random_uniform((uint32_t) predictionArray.count);

It's not always safe to suppress warnings, so don't go casting things to get rid of the warnings until you figure out whether the operation is safe.

What's going on here is that NSUInteger is, on your platform, a typedef for a 64-bit integer type. It's not always 64 bits, just on some platforms. The compiler is warning you that some of those bits are getting thrown away. If you know that these bits are unimportant, you can just use a cast.

In this case, the result is that index will always be under 232-1. If it's even remotely possible for predictionArray to contain 232 or more elements, then your program has an error and you'll have to construct a 64-bit version of arc4random_uniform(). You can ensure this with the following code:

assert(predictionArray.count <= (uint32_t) -1);


As per my comment, arc4random_uniform() takes in, and returns, a u_int32_t, an unsigned integer that is always 32 bits, regardless of target architecture. However, predictionArray.count returns an NSUInteger, which is typedefd differently for 32-bit and 64-bit systems; it's 32 bits (unsigned int) on a 32-bit system, and 64-bits (unsigned long) on a 64-bit system. If you're running on a 64-bit system, passing in a 64-bit NSUInteger to a function expecting a 32-bit integer will cause the compiler to complain that you're throwing away bits.