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

Trouble with If / Else Condtion

Im currently working on the build a self-destructing message app and am stuck on a if/else issue in my code. The first part where we are checking to make sure there is a value in the text box. When I run the app, and fill out the tex box's the code keeps hitting the alertView stating that I need to enter a username, password, and address. I don't see any issue with the code though is the problem.

My code is below:

- (IBAction)signup:(id)sender {


    NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *email = [self.emailFeld.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([username length] == 0 || [password length] == 0 || [email length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                                            message:@"Make sure you enter a username, password, and email address!"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
    else {
        PFUser *newUser = [PFUser user];
        newUser.username = username;
        newUser.password = password;
        newUser.email = email;

        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!"
                                                                    message:[error.userInfo objectForKey:@"error"]
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }
            else {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
    }



}

Any suggestions?

Thanks!

5 Answers

if you want you could just say if there is nothing in username.text or password.text

           if([_usernamefield.text isEqualToString:@""]||[_passwordfield.text isEqualToString:@""])
           {
             // do whatever you want
          }
             else{
                // do whatever you want

          }

That kind of worked... but now i get this error in the log:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't remove the username field for a PFUser.'

i think its because you didn't set it up like

                   PFUser *newUser = [PFUser user];
                  newUser.username = username.text;
                   newUser.password = password.text;
                   newUser.email = emai.textl;

hmm.. I switched it too that and still the same. Not too sure what the error really means

if you want you could put your project online and i could take a look at it for you

okay, I need to get GitHub on this laptop, and Ill post a link to the repository here shortly.

whoops! looks like some of the files aren't there.. hang on .. need to do another commit