Reputation: 5049
Specifically, this problem has come to me when I make a request with AFNeworking with JSONkit and receive a (id)JSON with several arrays and dictionaries nested.
If I don't want to modify the data, I don't have any problem:
self.myNSArray = [JSON objectForKey:@"result"];
But if I want to modify the data I must to store it in a mutable variable:
self.myNSMutableArray = [[JSON objectForKey:@"result"] mutableCopy];
The last one doesn't convert nested arrays or dictionaries to mutable data; it works only for first level.
The only way that I have found is on this link recursive mutable objects; but I don't know if there is a best way to resolve this kind of problem.
Thanks in advance.
Upvotes: 10
Views: 3185
Reputation: 1775
Make sure you are taking care of null
values in response string, otherwise it will return you nil which causes to horrible results.
(For Eg. Try mutataing response from http://www.json-generator.com/api/json/get/bQVoMjeJOW?indent=1)
Just place below line when converting API response to JSON Object.
responseString=[responseString stringByReplacingOccurrencesOfString:@"\":null" withString:@"\":\"\""];//To Handle Null Characters
//Search for below line in your parsing library and paste above code
data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
So there will be no null characters in your JSON object, hence no issue with using CFPropertyListCreateDeepCopy
.
Cheers!!
Upvotes: 0
Reputation: 1252
On ARC:
CFBridgingRelease(CFPropertyListCreateDeepCopy(NULL, (__bridge CFPropertyListRef)(immutableArray), kCFPropertyListMutableContainersAndLeaves))
really worked. Thanks brainjam.
Upvotes: 5
Reputation: 53551
You could use the CoreFoundation function CFPropertyListCreateDeepCopy
with the mutability option kCFPropertyListMutableContainersAndLeaves
:
NSArray *immutableArray = [JSON objectForKey:@"result"];
self.myMutableArray = [(NSMutableArray *)CFPropertyListCreateDeepCopy(NULL, immutableArray, kCFPropertyListMutableContainersAndLeaves) autorelease];
Upvotes: 11