Objective-C - “success” not equal to “success”
-
22-12-2019 - |
Question
The basic gist: I've got some JSON coming back from a webservice to validate a login; that part works. I'm pulling values out of the array into an NSDictionary; that part works. I need to check one of the values that comes back to know if it was successful or not. That's where it's failing. And as far as I can tell, it's telling me that "success" is not equal to "success".
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
NSString *result = [jsonArray valueForKey:@"result"];
NSLog(@"%@",result);
if ([result isEqual:@"success"]) {
The log shows "result" is getting set as "success", but it never seems to evaluate as true.
If I set "result" manually:
NSString *result = @"success";
...it gets into the if statement just fine, so it seems like there's something I'm missing that's pointing to a data type or something similar... I'm just at a loss of what else to try at this point.
I'm normally a web dev, but I'm new to iOS, so my debugging is still a little lacking in xcode but I'm familiar with general logic and such. Any help you guys could give me would be fantastic!
Edit:
NSLog showing the JSON coming back from the webservice:
2014-01-10 16:22:42.568 LoginTest[1640:70b] (
{
code = 1;
fname = Joe;
lname = Tests;
result = success;
token = 2555f13bce42b14cdc9e60b923bb2b20;
vendornum = 50000000;
}
)
Edit - final working code:
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
NSLog(@"jsonArray: %@", jsonArray);
NSString *result = [jsonArray[0] objectForKey:@"result"];
NSLog(@"%@",result);
if ([result isEqual:@"success"]) {
Solution
Earlier you commented (from a now removed answer) that -isEqualToString:
threw an unrecognized selector. I believe it was -[__NSCFArray -isEqualToString:]
or something very similar.
Based on your comment, you don't have "success"
, you have [ "success" ]
in your JSON.
That is an array which wraps the value of a string. You need to get the first element of the array and use that.
[result[0] isEqual:@"success"]
Based on the output in your log, your JSON is not an object
{
…
"result" = "success"
…
}
It is an array with only one object in it.
[
{
…
"result" = "success"
…
}
]
You are working with an array of data so the output of -valueForKey:
will be an array of data.
@MartinR is correct, it may be clearer to use
[jsonArray[0] objectForKey:@"result"]
to get the result.
OTHER TIPS
You didn't show us the actual output of the log. That's bad. Deducing from your comments, it should have shown something like
(
"success"
)
which is the description of an array object (NSArray
) containing a string, rather than the string object itself.
If this is indeed the case, then you need to get the (only? first?) element in the array and compare that using isEqual:
or isEqualToString:
.
if we want to compare two NSString
we use [str1 isEqualToString:str2];
You should do same instead of isEqual:
isEqual:
compares a string to an object, and will return NO if the object is not a string. do it if you are not sure if object is NSString.
`isEqualToString:` use it when you are sure both Objects are NSString.