Sunder
Sunder

Reputation: 503

Compare complex NSDictionary

I have a method [Order toJSON] that maps my class into a complex NSDictionary that have to be send to a server. To send it, I use NSJsonSerialization to convert it into a string.

The dictionary is of the form :

@{ 
  @"key": @"value", 
  @"order": @[ 
    @{ @"name": @"obj1", @"content": @[ ... ] },
    @{ @"name": @"obj2", @"content": @[ ... ] }
  ] 
}

I am writing unit tests to check that the NSDictionary is correct. The problem is that I actually don't know the order of the arrays. I can have obj2 before obj1. When I am using - [NSDictionary isEqualToDictionary], it sometimes fail due to the order of the objects.

Is there a way to compare those dictionaries without comparing the order of the arrays that it contains ? Sorting the array before may be an option, but it would be useless for anything else than testing, and it's quite complicated (and expensive).

Upvotes: 0

Views: 206

Answers (1)

gunjot singh
gunjot singh

Reputation: 2598

I suppose that converting the arrays to NSCountedSets will help here. NSCountedSet ignores the order of the objects, and also counts duplicate elements, so you will get the expected comparison.

Upvotes: 1

Related Questions