1

I have a dictionary declared, like this,

NSString *responseString = [request responseString];
            responseDict = [responseString JSONValue];
            for (id key in responseDict){
                NSLog(@"%@ : %@", key, [responseDict objectForKey:key]);
            }

Result :

013-01-22 00:14:02.323 PromoTest[2352:c07] A : 0
2013-01-22 00:14:02.325 PromoTest[2352:c07] B : 1
2013-01-22 00:14:02.325 PromoTest[2352:c07] C : 0

now, I want to compare the value and do some operation on it. I presumed the value for a key is of type NSString and compared it to my constant NSString, like this,

NSString *myString1 = @"0";     
NSString *myString2 = [responseDict objectForKey:@"A"];

NSLog(@"%d", (myString1 == myString2)); //1
NSLog(@"%d", [myString1 isEqualToString:myString2]); //1

Result:

2013-01-22 00:19:12.966 PromoTest[2423:c07] 0
2013-01-22 00:19:12.966 PromoTest[2423:c07] 0

Where am i going wrong?? Is my comparison wrong? How do I go about correctly comparing the content??
The data is being received as response data from a web service. I am just converting the data into a dictionary for easily using it. The web service returns a JSON object,

{"A":0,"B":1,"C":0}
Vincent Guerci
  • 14,379
  • 4
  • 50
  • 56
anotherCoder
  • 712
  • 3
  • 11
  • 25
  • 2
    «I presumed the value for a key is of type NSString» It looks like that presumption might be wrong. You should verify it first. Then take a look at [Understanding NSString comparison](http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison-in-objective-c) – jscs Jan 21 '13 at 18:56
  • 3
    Any reasonable JSON parser should map numbers to NSNumber and not NSString. If this isn't obvious to you at first glance, then read the documentation of the JSON parser you're using. –  Jan 21 '13 at 18:59
  • the type of keys and values in a default obj-c dict is id – tttthomasssss Jan 21 '13 at 19:00
  • 1
    `id` is just a convenience for saying that the actual type can any object type. – Gabriele Petronella Jan 21 '13 at 19:03
  • @ttt: The objects still have and know their classes. `id` is a variable type that can hold a pointer to any object. – jscs Jan 21 '13 at 19:21
  • @JoshCaswell i know that ;), i just wanted to point out that they are not eg. nsstring by default, sorry if that was delivered a bit unclear in the comment – tttthomasssss Jan 21 '13 at 19:42

3 Answers3

10

NSDictionary method isEqualToDictionary can be used to compare 2 dictionaries

Returns a Boolean value that indicates whether the contents of the receiving dictionary are equal to the contents of another given dictionary.

For example:

[myDictionary isEqualToDictionary:expectedDictionary]
sash
  • 8,423
  • 5
  • 63
  • 74
5

The only reasonable explanation is that [responseDict objectForKey:@"A"] is not returning a NSString.

You are probably getting a NSNumber back, therefore the comparison fails.

If that's the case you need to get a NSString from the NSNumber before comparing it against your constant. You can do it by

NSString * myString2 = [[responseDict objectForKey:@"A"] stringValue];

Also never use == to compare NSStrings instances. Stick with isEqualToString and you'll be good.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
2

Instead of comparing strings you could also compare number object. here including with a check, if the returned object is a NSNumber, if not, try as string:

if([responseDict[@"A"] isKindOfClass:[NSNumber class]]){
    NSNumber *myNumber1 = @0;     
    NSNumber *myNumber2 = [responseDict objectForKey:@"A"];
    NSLog("Same number: %@",[myNumber1 isEqualToNumber:myNumber2] ? @"YES" : @"NO");

} else if([responseDict[@"A"] isKindOfClass:[NSString class]]){
    NSString *myString1 = @"0";     
    NSString *myString2 = [responseDict objectForKey:@"A"];
    NSLog("Same string: %@",[myString1 isEqualToString:myString2] ? @"YES" : @"NO");
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178