I'm trying to use the writeToFile method of an NSArray
of NSDictionaries which doesn't allow nulls. I figured just loop through and find them and replace with empty NSString
's but I can't get it to work. This is what I'm trying.
EDIT: The NSArrays and NSDictionaries are created by NSJSONSerialization.
for (NSMutableDictionary *mainDict in mainArray)
{
for(NSMutableDictionary *subDict in mainDict)
{
for(NSString *key in subDict)
{
id value = [subDict objectForKey:key];
if (value == [NSNull null])
{
[subDict setObject:@"" forKey:key];
}
}
}
}
But this throws an exception. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
But as you can see all NSDictionaries are mutable.
What's up? Is there a better way to do this?
EDIT: Moved new version to a separate answer.