views:

81

answers:

2

When the IBAction login is invoked it supposed to use a response from SOAP Web Services of either true or false, false meaning the user is not authorized to use the app. I have it using these if statements after it gets the response, but for some reason it runs both true and false ifs.

{
 [soapResults appendString: string];
 NSLog(soapResults);

 if (soapResults = @"true")
 {
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:soapResults delegate:self cancelButtonTitle:@"Try Again",nil otherButtonTitles:nil];
        [alert show];
  [alert release];
  [soapResults release];
  soapResults = nil;
  [loginIndicator stopAnimating];
  loginIndicator.hidden = TRUE;
  loggedinLabel.text = usernameField.text;
  loggedinLabel.textColor = [UIColor blackColor];
  NSLog(@"Valid Login"); 
 }
  if (soapResults = @"false")
  {
   NSLog(@"Invalid Login");
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:soapResults delegate:self cancelButtonTitle:@"Try Again",nil otherButtonTitles:nil];
   [alert show];
   [alert release];
   [soapResults release];
   soapResults = nil;
   [loginIndicator stopAnimating];
   loginIndicator.hidden = TRUE;
  }





}

Please Help

+2  A: 

There is only one equal sign in the if statements. This assigns the string to the soapResults variable which causes the if statement to evaluate the string (which will always be true).

if (@"true")

Instead, use two equal signs to do the comparison.

if (soapResults == @"true")

Some avoid this common problem by always placing the variable at the end of the comparison.

if (@"true" == soapResults)

This way if you forget the second equal sign it will cause a compilation error which is much easier to find.

Update: as the commentators kindly pointed out, you should not compare Objective-C strings using the == operator. Instead use the isEqualToString method.

if ([soapResults isEqualToString:@"true"])
ryanb
[soapResults isEqualTo: @"true"] perhaps?
Oren Trutner
Good eye! Anyway, regarding comparison: better use [soapResults isEqualToString:@"true"] otherwise it will always be false (as the equal sign is not a good way to compare strings in Obj-C).
Aviad Ben Dov
Thanks Aviad, my obj-c is rusty. I'll update the post with your mention.
ryanb
A: 

To: responders I posted this question, but couldn't register my openid right.