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!

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

Signing Up New Users Part 1 (UIAlertView) Code Challenge.

Hey guys,

So I need some help on the following code challenge:

"If the guess is correct, we want to alert the user with a message using a UIAlertView. Add an if statement that checks if the BOOL variable 'isCorrect' is true (or 'YES'). Inside the if statement, declare a new UIAlertView variable named 'alertView'. Alloc/init it with the method 'initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles'. For the title, use "Hooray!", for the message, use "You did it!", for the delegate use nil, for cancelButtonTitle use "OK", and for otherButtonTitles use nil."

Here is my code that I keep getting errors on:

#import "NumberGuesserViewController.h"
#import "GuessEngine.h"
#import "UIAlertView.h"

@implementation NumberGuesserViewController

- (IBAction)makeGuess:(id)sender {
    NSString *userNumber = self.numberField.text;

    // Check to see if the user's number is correct
    BOOL isCorrect = [GuessEngine testGuess:userNumber];

    if([isCorrect == true) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hooray" message:@"You did it!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
   }
}
@end

Maybe Ben Jakuben

4 Answers

I'm no where near this far, so I can't really help, but it seems like you are missing these inside your code:

For the title, use "Hooray!", for the message, use "You did it!", for the delegate use nil, for cancelButtonTitle use "OK", and for otherButtonTitles use nil."

Anyway I figured out the answer to this.

if (isCorrect == YES){ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hooray!" message:@"You did it!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

For boolean values, you don't even need to check if it's == YES. Since it's either YES or NO, you can write:

if (isCorrect) { // test for YES/true

if (!isCorrect) { // test for NO/false

My personal style is below. When testing for a negative, I like to explicitly use "NO" because it's easier to notice than the not (!) operator.

if (isCorrect) {

if (NO == isCorrect) {

Hey Vlatko,

I see what you mean, don't know what happened there, I have corrected the code formatting to display correctly, I have been having some problems with the edit area on here today.

Maybe this is why I am having problems with the code challenge.

Thanks for pointing this out.

Hey Ben Jakuben,

Thanks for pointing that one out. I am always looking to simplify code in iPhone app development.

Stu :)