0

In the following piece of code, loginString is 0 when an incorrect username password is entered and is 2061128143657912001 when a correct user name password is entered. However no matter what I do, I always get the pop up.

What am I missing?

//loginString is a 0 or a 2061128143657912001
if ([loginString intValue]>0) {

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:userName.text forKey:@"userNameKey"];
        [defaults setObject:password.text forKey:@"passwordKey"];
        [defaults synchronize];
        [self dismissModalViewControllerAnimated:YES];

    }else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Validation Error" message:@"Unable to validate Login/Password combination" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [alert show];
        [alert release];

    }
Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
  • What is `loginString` and where is it declared and set? – PengOne Jun 16 '11 at 03:11
  • NSMutableString *loginString; declared in the same class in the header file. It is set in the XML parser functions. I have done an NSLog right above the code above and I get a 0 or 2061128143657912001 – Strong Like Bull Jun 16 '11 at 03:14
  • 2
    That’s not possible. An `int` is at most 2147483647. –  Jun 16 '11 at 03:17

1 Answers1

2

Try throwing in

NSLog(@"loginString  = %@",loginString);
NSLog(@"loginString  = %d",[loginString intValue]);

before the if statement to see if things are really what you think they are.

That done, see what happens if you reverse the if statement by

if ([loginString intValue] == 0) {
...
} else {
...
}

Or maybe even check [loginString isEqualToString:@"0"].

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • very good observance I will try right now. Thanks for stating the obvious what I should have done :S – Strong Like Bull Jun 16 '11 at 03:17
  • Yup sure enough I had released and nil'd loginString. Thanks for setting me on right track. – Strong Like Bull Jun 16 '11 at 03:19
  • 1
    @jini: In light of Bavarious's observation, you may want to do your check with `![loginString isEqualToString:@"0"]` since it isn't clear what the `intValue` will be for a number larger than 2147483647. – PengOne Jun 16 '11 at 03:21
  • 2
    `-[NSString intValue]` returns either `INT_MAX` or `INT_MIN` on overflow, and returns 0 if the string doesn’t begin with a valid number. –  Jun 16 '11 at 03:26
  • 1
    just a note --- even if you want to convert such a big number can not be handled using intValue -- you have to use longLongValue even if you have to convert. – Girish Kolari Jun 16 '11 at 03:27
  • still having some problems. please see http://stackoverflow.com/questions/6367002/why-am-i-getting-strange-values-in-my-nsinteger-object. It is a different issue so had to open a new question since PengOne already answered my question for this round. – Strong Like Bull Jun 16 '11 at 04:09