SirRupertIII
SirRupertIII

Reputation: 12596

Code in if statement not getting hit

I've been trying to figure out why this code is not getting hit. The log says either "Success 1" or "Success 0" so I figured I could just use @"1" or @"0" in an if statement, but it's not going through either section.

     NSString *success = [JSON valueForKey:@"Success"];

    NSLog(@"Success %@", success);

    if (success  == @"1") {
    [self performSegueWithIdentifier:@"loginToInvite" sender:self];
    } else if (success == @"0")
    {
        _logInBtn.selected = NO;
        _popupLbl.hidden = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text" message:@"Text" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
    }

Thank you for your answers!

Upvotes: 1

Views: 147

Answers (4)

Nimit Parekh
Nimit Parekh

Reputation: 16864

 NSString *success = [JSON valueForKey:@"Success"];

    NSLog(@"Success %@", success);

    if ([success  isEqual:@"1"]) {
    [self performSegueWithIdentifier:@"loginToInvite" sender:self];
    } else if ([success isEqual:@"0"])
    {
        _logInBtn.selected = NO;
        _popupLbl.hidden = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text" message:@"Text" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
    }

Upvotes: 2

user1342592
user1342592

Reputation: 49

In order to compare to a string value use isEqualToString:@"1"

Upvotes: 1

Adam E
Adam E

Reputation: 31

NSString literal equality should be done with [NSString isEqualToString:] otherwise you are checking if the pointers to the objects are the same.

Upvotes: 3

James Webster
James Webster

Reputation: 32066

Your check for string equality won't work as that is checking for identity (if the objects are the same object)

if (success  == @"1")

You instead, want to check equality, which in the case of strings is true if the values are the same. You should instead use this:

if ([success isEqualToString:@"1"])

Upvotes: 5

Related Questions