Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

CrystalBall exercise

After following the course, I ended up with app that was at first functional, but after applying Constraints for the objects in View, functionality dropped in a way that I don't see any predictions when clicking on Prediction button. I also get warning in CrystallBall.m file where there wash't one before:

import "DJCrystalBall.h"

@implementation DJCrystalBall

  • (NSArray *) predictions { if (_predictions == nil) { _predictions = [[NSArray alloc] initWithObjects:@"It is Certain", @"It is Decidedly so", @"All signs say YES", @"The stars are not aligned", @"My reply is NO", @"It is doubtful", @"Better not tell you now", @"Concentrate and try again", @"Unable to answer now", nil]; } return _predictions; }
  • (NSString *) randomPrediction {

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

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

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

    return [self.predictions objectAtIndex:random]; }

@end

Any ideas how to sort this?

2 Answers

I think you are getting the warning because your mac is 64 bit. It's likely that you have the Intel Core i5 or 7. Just ignore it. The app should still work. ( I have the same issue)

Also check that @property (strong, nonatomic) IBOutlet UILabel *predictionLabel; is connected properly. If you are not sure, I would delete it and connect it again.

This is from stack overflow:

Page link

NSUInteger index = arc4random_uniform((uint32_t) predictionArray.count); It's not always safe to suppress warnings, so don't go casting things to get rid of the warnings until you figure out whether the operation is safe.

What's going on here is that NSUInteger is, on your platform, a typedef for a 64-bit integer type. It's not always 64 bits, just on some platforms. The compiler is warning you that some of those bits are getting thrown away. If you know that these bits are unimportant, you can just use a cast.

In this case, the result is that index will always be under 232-1. If it's even remotely possible for predictionArray to contain 232 or more elements, then your program has an error and you'll have to construct a 64-bit version of arc4random_uniform(). You can ensure this with the following code:

assert(predictionArray.count <= (uint32_t) -1);

Thanks for your answers... I sorted it out by building it from scratch. Not sure what the problem was before