0

I have got a JSON serialized NSDictionary with a structure like:

key1_1 = ( //Array of dictionaries
         { //dict 1
           key2_1 = val2_1
           key2_2 = @"TO_BE_REPLACED"
           key2_3 = ( //Another array of dictionaries
                      { //dict
                        key2_3_1 = val2_3_1
                        key2_3_2 = @"TO_BE_REPLACED"
                        ...
                      },
                      ... //more dicts
                    )
          },
          ... //more dicts
          ),
key1_2 = ...

So its basically got a complex structure of arrays and dictionaries. At any level I may encounter a "TO_BE_REPLACED" string which needs to replaced by a different string. Please note that the keys will be different.

I am able to navigate the structure successfully using recursion. But my problem is that a NSDictionary cannot be modified while its being iterated over.

What are the possible solutions to this kind of problem?

NSRover
  • 932
  • 1
  • 12
  • 29

2 Answers2

2

Type NSDictionary to NSMutableDictionary.Or u can use NSObject class for storing from NSMutableDictionary to Model Class object so that it is much easy to edit the value in it and restore back to mutableDictionary.

ashForIos
  • 399
  • 1
  • 5
  • 18
  • the same rules apply to NSMutableDictionary, you can't modify it while it is being iterated over. – NSRover Feb 19 '15 at 06:50
  • But at the time of iteaating if you found your desired value instead of inserting that value using setvalue to dictionary you can put the value which you wants to be. – ashForIos Feb 19 '15 at 06:51
  • not sure I understand you, but assuming you are talking about converting my dictionary to an NSObject: It won't work in my case, the dictionary does not have a fixed structure. – NSRover Feb 19 '15 at 07:02
  • Ok.If I'm getting you correctly you are getting response from server and when you get this value "@"TO_BE_REPLACED" "u need to replace it. – ashForIos Feb 19 '15 at 07:06
0

If the nested dictionaries contains the same keys, then you can go with a model class. The model class will have keys as properties and a property of the object of same class. Using this you can replace the "To be replaced" string easily.

Ank
  • 514
  • 1
  • 5
  • 19