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) Creating a Data Collection Randomization

Nick Barnes
Nick Barnes
7,818 Points

Value Conversion Issue

Hello folks, the randomization code for this exercise gives me a Value Conversion Issue warning. How would I go about fixing this? The code works fine but it would be nice to be warning free! To confirm, the code is:

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

The warning is:

THViewController.m:40:37: Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'u_int32_t' (aka 'unsigned int')

I'm using Xcode 5.1.1 on OSX 10.9.4 and I have followed the course exactly as prescribed.

Thanks for your help!

Nick

2 Answers

Nick Barnes
Nick Barnes
7,818 Points

Hi Mike, thanks for your reply. Unfortunately that still gives me the same value conversion warning so I did some searching and found an article about this very problem for this very exercise on Stack Exchange - shame it was taken outside of Treehouse!

It states that arc4random_uniform takes in a value ( which is always 32bits regardless of system architecture but self.predictions.count will return an NSUInteger which is typedef'd differently for either 32bit or 64bit systems. As my system is 64bits, Xcode spits out the warning about wasted bits. The code given to prevent the warning uses casting to suppress the warning:

NSUInteger random = arc4random_uniform((uint32_t) self.predictions.count);
Mike Baxter
Mike Baxter
4,442 Points

Does this not work?

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