I have looked at SO for similar questions, but am open to being pointed to a duplicate.
I am receiving some JSON from a site, and I want to test for a 404 response.
I have this expression:
NSString *responseString = [json objectForKey:@"statusCode"];
NSLog(@"responseString: %@", responseString);
NSString *myString1 = @"404";
NSLog(@"%d", (responseString == myString1)); //0
NSLog(@"%d", [responseString isEqual:myString1]); //0
NSLog(@"%d", [responseString isEqualToString:myString1]); //Crash
The response string returns 404. The first and second logs result in 0, and the 3rd crashes with this log:
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000001943
2015-01-29 16:23:33.302 Metro[19057:5064427] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000001943'
statusCode
is a number, not a string. The error makes this clear by telling you that you are trying to call isEqualToString
on an NSNumber
.
Try this:
NSInteger responseCode = [json[@"statusCode"] integerValue];
NSInteger notFoundCode = 404;
if (responseCode == notFoundCode) {
// process 404 error
}