Method:
- (void)serverResponse:(NSData *)data
{
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", response);
NSLog(@"%i", [response isEqualToString:@"1"]);
}
Log:
2015-03-15 06:26:53.716 appName[687:60b] 1
2015-03-15 06:26:53.718 appName[687:60b] 0
This doesn't seem to make any sense. I'm guessing the problem has something to do with the encoding, but I'm not sure what. Any help is appreciated.
When comparing strings, NSString
does not take encoding into account.
Your problem is simple: The response contains 28 spaces after the "1".
Two possible solutions:
BOOL integerValueEquality = [response integerValue] == 1;
or
NSString *trimmedString = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
BOOL stringEquality = [trimmedString isEqualToString:@"1"];