-4

I have a NSDictionary from the parsed json data and I want to keep all the objects and keys in that dictionary and also want to add another key value pair to the same dictionary. How can I do that? Thanks in advance for your help.

pkaur
  • 95
  • 1
  • 7
  • convert it to mutable dictionary , then you can add more object to it. – Pawan Rai May 24 '14 at 13:27
  • In lieu of finding a suitable duplicate: `NSMutableDictionary *mutableDictionary = [dictionary mutableCopy]; mutableDictionary[key] = object; dictionary = [mutableDictionary copy];` – Carl Veazey May 24 '14 at 13:43

2 Answers2

2

In NSJSONSerializer, look at the NSJSONReadingOptions that you can pass to the parser.

There is one NSJSONReadingMutableContainers that makes the parser return mutable containers. So you get an NSMutableDictionary instead of an NSDictionary, and then it's just basic use of an NSMutableDictionary.

markhunte
  • 6,805
  • 2
  • 25
  • 44
gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • I had a quick look at this and it does indeed make the returned object a mutable object. In my test a I declared the object using `id` and got it's class after : __NSDictionaryM – markhunte May 24 '14 at 16:13
1

You cannot modify NSDictionary- you need to create an NSMutableDictionary with the objects from the NSDictionary you have, then add new objects and their keys in the NSMutableDictionary using setObject:forKey or setValue:forKey.

markhunte
  • 6,805
  • 2
  • 25
  • 44
GTAE86
  • 1,780
  • 3
  • 29
  • 39