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

CrystalBall Warning when running on device.

Hey,

Finished the Crystal Ball app and when running it in the simulator no errors or warnings or anything - perfect.

When I run it on my iPhone Xcode throws out a warning. The app runs fine but the warning pops up.

The warning I'm getting is on the GWSCrystallBall.m where I call

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

The warning is:

Implicit conversion loses integer precision 'NSUInteger' (aka 'unassigned long') to 'uint_32_t' (aka 'unassigned int')

Not sure if there is any more to that warning as the screen crops it.

Any assistance would be appreciated.

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The function arc4random_uniform returns an u_int32_t which a definition for unsigned int. Meaning that you can store only 32-bit positive integers. When you specify int it means that you can store positive or negative numbers. Standard best practice is to use NSUInteger which adapts to the version of iOS and can allow storage of a 32-bit or 64-bit unsigned int. NSUInteger is not a class, it is simply a typedef which define unsigned int for 32-bit architecture and unsigned long for 64-bit architecture.

Hi Amit ok so thanks for the reply, i really appreciate your time but how would I edit the code? Why would the code taken from the video cause an error? I didn't see you get the same warning when you loaded the app on the iPhone. Have I done something wrong?

I ended up using the following:

int random = arc4random_uniform((int)[self.predictions count]);

The error disappeared when I added the (int)