How to determine if strings are equal in Objective-C?

I read the line from the JSON result as follows:

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

Then I 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 always returns the else part, even if the result is "N". Both of them have the same value, but the operator always returns false.

+6
source share
1 answer

I found a way to clear 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]; } 
+6
source

Source: https://habr.com/ru/post/911813/


All Articles