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

Error In Crystal ball Project.

I'm currently working on the crystal ball app project and in this line

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

I get this error.

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

I'm sure the issue in right under my nose, but i copied the code from the tutorial video.

5 Answers

I bet the problem is with the return type of self.predictions.count, which is an NSUInteger.

32-bit applications treat NSUInteger primitives as u_int32_t (unsigned 32-bit integers), while 64-bit applications (like those compiled for an iPhone 5S or OS X) perceive NSUIntegers to be u_int64_t.

The C function arc4random_uniform() requires an unsigned 32-bit integer as its argument, rather than an NSUInteger, which may in practice be either a 32- or 64-bit unsigned integer. If you're sure that self.predictions.count will be less than 4,294,967,295 ((2^32 -- 1), the length of an unsigned integer), you could try casting your self.predictions.count to int (or even to u_int32_t, though that's probably not necessary). This would be written as arc4random_uniform((int)self.predictions.count). To be extra-fastidious, you could do something like:

NSUInteger randomInt;
if (self.predictions.count <= UINT_MAX) {
    randomInt = arc4random_uniform((unsigned int)self.predictions.count);
} else {
    // Handle it
    // randomInt = something else;
}

...but I'm betting that your array count will be less than 4,294,967,295, so I won't go googling for uint64 number generator functions. If I'm wrong about that, let me know. Hope this gets rid of your warning!

Yes it worked! just adding (int) in this line did it. arc4random_uniform((int)self.predictions.count) Thanks!

Yep, just int is fine, but adding unsigned int is more precise since 1. arc4random_uniform shouldn't ever receive an int < 0, and 2. Calling count on an array never returns a negative number. The unsigned just means that the number will always be greater than or equal to 0 (it'll never have the - sign; "un-signed").

Happy to have helped! :)

Just realized arc4random_uniform((int)self.predictions.count) was my suggestion--sorry! I'm being really picky when I say you should use unsigned int here, but accuracy never hurts!

is it an error (red x, wont compile) or just a warning (yellow triangle, will compile just fine)? its probably a warning since the random function returns positive integers and an int can store both negative and positive.

Yes it's a Warning sorry! Not an error It does compile and the program works. but is there a way to solve this warning?

Yes it's a Warning sorry! Not an error It does compile and the program works. but is there a way to solve this warning?

try using

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

Program still works and compiles but the warning is still coming up.

try cleaning your project by going to product->clean in the xcode menu

cleaned it and the warning disappeared but upon compiling it came back. The warning underlines:

arc4random_uniform

Then breaks and continues to underline whats within the ( ) :

self.perdictions.count

self.perdictions.count

is mispelled. is it like that in your code

Sorry that was a typo in my comment on my code it is spelled correctly. r before e.

I was noticing the same error in xcode as Andrew. The following suggestion from Colin Jackson worked for me.

<p> int random = arc4random_uniform((int)self.predictions.count);</p>

Happy to help!