I have a JSON service that registers a new user. When there's an error like "E-mail in use" or "Taken user name" etc., it returns error codes seperated by "|" character (string). When the registration is successful, it returns the registered user's ID (integer).
For example, a registered e-mail and user-name (both in one case) error would return:
{"error" = "200|300|";}
And a successful registration would return:
{"error" = 1234;}
So I split the error by "|" character into an array, then show error(s) like this:
NSArray *errorCodes = [[jsonData objectForKey:@"error"] componentsSeparatedByString:@"|"];
When there is error/are errors, this line works great. But when there is no error, this line of code crash as following:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber componentsSeparatedByString:]: unrecognized selector sent to instance 0x7579fc0'
If I'm not mistaken, it crashes because it tries to split a string which isn't actually a string, but a number.
So I tried to work around this by checking the type of object class like:
if ([[jsonData objectForKey:@"error"] isKindOfClass:[NSNumber class]]) {
// success
} else if ([[jsonData objectForKey:@"error"] isKindOfClass:[NSString class]])
// show errors
}
However, it didn't get into none of these statemens. When I tried to check like [__NSCFNumber class]
instead of [NSNumber class]
, it returned error:
Unknown receiver '__NSCFNumber'; did you mean 'NSNumber'?
Same for [NSString class]
.
How can I work this out by checking the object type? I know I can use other ways but this one seems like the proper way since I might be needing this control in the future for different data types.