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
Andrew Ocejo
6,197 PointsError 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
Colin Jackson
4,829 PointsI 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!
Stone Preston
42,016 Pointsis 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.
Andrew Ocejo
6,197 PointsYes it's a Warning sorry! Not an error It does compile and the program works. but is there a way to solve this warning?
Andrew Ocejo
6,197 PointsYes it's a Warning sorry! Not an error It does compile and the program works. but is there a way to solve this warning?
Stone Preston
42,016 Pointstry using
NSUInteger random = arc4random_uniform(self.predictions.count);
Andrew Ocejo
6,197 PointsProgram still works and compiles but the warning is still coming up.
Stone Preston
42,016 Pointstry cleaning your project by going to product->clean in the xcode menu
Andrew Ocejo
6,197 Pointscleaned 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
Stone Preston
42,016 Pointsself.perdictions.count
is mispelled. is it like that in your code
Andrew Ocejo
6,197 PointsSorry that was a typo in my comment on my code it is spelled correctly. r before e.
Aaron S
Courses Plus Student 2,072 PointsI 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>
Colin Jackson
4,829 PointsHappy to help!
Andrew Ocejo
6,197 PointsAndrew Ocejo
6,197 PointsYes it worked! just adding (int) in this line did it. arc4random_uniform((int)self.predictions.count) Thanks!
Colin Jackson
4,829 PointsColin Jackson
4,829 PointsYep, just
intis fine, but addingunsigned intis more precise since 1.arc4random_uniformshouldn't ever receive an int < 0, and 2. Callingcounton an array never returns a negative number. Theunsignedjust 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! :)
Colin Jackson
4,829 PointsColin Jackson
4,829 PointsJust realized
arc4random_uniform((int)self.predictions.count)was my suggestion--sorry! I'm being really picky when I say you should useunsigned inthere, but accuracy never hurts!