Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if strings are equal in Objective C?

I read a string from a JSON result as follows:

NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                               length:[data length] 
                                             encoding:NSUTF8StringEncoding]; 

I then try to determine if the string is equal to the value "N"

if ([strResult isEqualToString:@"N"])
{ 
  [lblImageOK setHidden:YES]; 
} 
else 
{ 
  [lblImageOk setHidden:NO]; 
}

The if statement allways returns the else part, even though the result is "N". They both have the same value but the statement returns false always.

like image 686
Angie Avatar asked Mar 27 '12 22:03

Angie


1 Answers

I found a way to clean the string and then check if they are equal.

NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                           length:[data length] 
                                         encoding:NSUTF8StringEncoding]; 

strResult = [strResult stringByReplacingOccurrencesOfString:@"\"" withString:@""];
strResult = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

if ([strResult isEqualToString:@"N"])
{ 
  [lblImageOK setHidden:YES]; 
} 
else 
{ 
  [lblImageOk setHidden:NO]; 
}
like image 146
Angie Avatar answered Sep 19 '22 10:09

Angie