-8

I have the following code:

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{

   strFinal = [theRequest responseString];
   NSLog(@"Sattus Code : %@",[theRequest responseString]);

   [self GoTo];
}

I am using ASIHttp for log in. If the login is ok strFinal is set to successfully, if not is set to failed.

my GoTo method is

-(void)GoTo{
    NSString *failStr = @"failed";
    NSString *successStr = @"successfully";
    if (strFinal == successStr) {
       //Navigate to other xib

    }else
       if (strFinal == failStr) {
            UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [AlertFail show];
        }
    }
}

The problem is that the alert is not displayed unless strFinal is failed.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Denny
  • 285
  • 1
  • 2
  • 16

8 Answers8

2

Change :

if (strFinal == successStr) {

to

if ([strFinal isEqualToString:successStr]) {
Raptor
  • 53,206
  • 45
  • 230
  • 366
1

The == operator compares the pointers, which will usually be different even if their contents are the same. The isEqualToString method compares their contents. So please compare like this...

if ([failStr isEqualToString:successStr])
{
     // ------- success
}
else
{
      // ---- fail ------
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1
 if (strFinal == failStr)

That does not work. You have to use isEqualToString.

if ([strFinal isEqualToString: failStr])
griz
  • 470
  • 3
  • 9
1

Simply edit the isEqualToString: from "=" sign. This is the right method for comparing 2 NSStrings together.

   -(void)GoTo{
    NSString *failStr = @"failed";
    NSString *successStr = @"successfully";
    if ([strFinal isEqualToString:successStr]) {
       //Navigate to other xib

    }else
       if ([strFinal isEqualToString:failStr]) {
            UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [AlertFail show];
        }

    }

Hope this helps ...

IronManGill
  • 7,222
  • 2
  • 31
  • 52
1

To compare strings you need to use isEqualToString: Here is an example of how you can use it:

if ([strFinal isEqualToString: successStr])
{
    // your code here
}
wedgemund
  • 51
  • 3
1

change your code like this and then check

isEqualToString

Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.

- (BOOL)isEqualToString:(NSString *)aString

Parameters

aString

The string with which to compare the receiver.

Return Value

YES if aString is equivalent to the receiver (if they have the same id or if they are NSOrderedSame in a literal comparison), otherwise NO.

Discussion

The comparison uses the canonical representation of strings, which for a particular string is the length of the string plus the Unicode characters that make up the string. When this method compares two strings, if the individual Unicodes are the same, then the strings are equal, regardless of the backing store. “Literal” when applied to string comparison means that various Unicode decomposition rules are not applied and Unicode characters are individually compared. So, for instance, “Ö” represented as the composed character sequence “O” and umlaut would not compare equal to “Ö” represented as one Unicode character.

Special Considerations

When you know both objects are strings, this method is a faster way to check equality than isEqual:.

Availability

Available in OS X v10.0 and later.

-(void)GoTo{
            NSString *failStr = @"failed";
            NSString *successStr = @"successfully";
            if ([strFinal isEqualToString:successStr]) {
               //Navigate to other xib

            }else
               if ([strFinal isEqualToString:failStr]) {
                    UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                    [AlertFail show];
                }

        }
9to5ios
  • 5,319
  • 2
  • 37
  • 65
1

you need to do like this

-(void)GoTo{
    NSString *failStr = @"failed";
    NSString *successStr = @"successfully";
    if ([strFinal isEqualToString:successStr]) {
       //Navigate to other xib

    }else
       if ([strFinal isEqualToString:failStr] ) {
            UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [AlertFail show];
        }

}
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
0

You can try in the following way

if ([strFinal caseInsensitiveCompare:@"successfully"] == NSOrderedSame)
    {
        <#statements#>
    }
Exploring
  • 925
  • 6
  • 18