Convert NSDictionary value (id?) to NSString or NSNumber to compare it Convert NSDictionary value (id?) to NSString or NSNumber to compare it json json

Convert NSDictionary value (id?) to NSString or NSNumber to compare it


Did you use the comparison operator (==) to compare your objects?

If so, you didn't compare the values of your objects but their memory addresses.
If the object returned by [returnDictionary objectForKey:@"code"] is of type NSString you should use NSString's isEqualToString: If it returns a NSNumber instance, you could compare the intValue of that object to 1.

[[returnDictionary objectForKey:@"code"] isEqualToString:@"1"]  

or

[[returnDictionary objectForKey:@"code"] intValue] == 1


You can use

[[returnDictionary objectForKey:@"code"] isKindOfClass:[NSString class]]

to check the class of the object pulled from the dictionary.

Hope it helps!


If you expect the output to be an integer, you can call intValue.

[[returnDictionary objectForKey:@"code"] intValue] == 1

Also, you can log the type of the returned object by + class; method.

NSLog(@"%@", [[returnDictionary objectForKey:@"code"] class]);