8

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.

4
  • 2
    Can you NSLog(@"%@",strResult); before your if statement and add the output to your question? Commented Mar 27, 2012 at 22:36
  • 4
    Make it NSLog(@"[%@] len=%d, strResult, [strResult length]); for good measure. Commented Mar 27, 2012 at 22:43
  • 1
    as peterept suggests, you almost certainly have leading or trailing spaces. Commented Mar 27, 2012 at 23:56
  • This is what I get with the NSLog: ["N"] len=3 Commented Mar 28, 2012 at 13:40

1 Answer 1

9

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]; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.