Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Refactoring into a Model Understanding @property

How can I avoid "Implicit conversion loses integer precision" warning following Treehouse steps?

I am in the midst of following the instructions, exactly, to extract a random picker from the controller to the models we are creating. I am getting a :

Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'u_int32_t' (aka 'unsigned int')

on the following code from the video on the line where random is set.

-(NSString*) randomPrediction {
    int random = arc4random_uniform(self.predictions.count);
    return [self.predictions objectAtIndex:random];

}

1 Answer

Stone Preston
Stone Preston
42,016 Points

the reason this happens is that when on a 64 bit system the count variable of NSArray is a 64 bit integer, but arc4random_uniform expects a 32 bit integer as its argument.

to make the warning go away, just add a cast to an unsigned 32 bit integer to the predictions count:

-(NSString*) randomPrediction {
    int random = arc4random_uniform((uint32_t) self.predictions.count);
    return [self.predictions objectAtIndex:random];

}