Reputation: 12431
I am hitting the error (stated in the subject) because there are times the property 'id' does not store the hash containing '$oid' in the returned json. For example
Sometimes I get:
"id":{"$oid":"4eea972209f47a0028000140"}
Some other times I get
"id":"4eea972209f47a0028000140"
I am trying to do a check in the following code to cater for such irregularity
if ([[question valueForKey:@"id"] valueForKey:@"$oid"])
{
question_id = [[question valueForKey:@"id"] valueForKey:@"$oid"];
}
else
{
question_id = [question valueForKey:@"id"];
}
However, it still doesn't work as the code fails during the checking phase.
How can I implement a check so that I will take question_id from '$oid' only if it exists?
Upvotes: 2
Views: 3687
Reputation: 119242
You need to check what type is being returned by [question valueForKey:@"id"]
, it looks like sometimes it is a string, and sometimes it is another object which is KVC compliant for your other key. Your error will be in the very first if statement.
Upvotes: 3
Reputation: 17478
Try the following code.
id quesDict = [question valueForKey:@"id"];
if( [quesDict isKindOfClass:[NSDictionary class]] )
{
question_id = [quesDict valueForKey:@"$oid"];
}
else
{
question_id = quesDict;
}
Upvotes: 5