- (NSMutableDictionary *)updateTemplates:(NSMutableDictionary *)oldTemplates
forSpecType:(NSString *)specType {
// oldTemplates is an NSMutableDictionary pulled from a plist
// specType is used for flexible paths, to eliminate duplicate code
// Make a dict of the parameters object (about to be overwritten)
NSMutableDictionary *parameters = [oldTemplates valueForKeyPath:
[NSString stringWithFormat:@"root.%@.parameters", specType]];
// Dump the new data into the matching object
[oldTemplates setValue:[updateTemplates valueForKeyPath:
[NSString stringWithFormat:@"data.%@", specType]]
forKeyPath:[NSString stringWithFormat:@"root.%@", specType]];
// Put the parameters back, since they don't exist anymore
/* Instant crash, with the debugger claiming something is immutable
* But I just used the exact same method on the line above
* updateTemplates isn't immutable either; it's only when I try to mutate
oldTemplates after putting in updateTemplates -- and only the update
seems to be breaking things -- that I get the exception and crash
*/
[oldTemplates setValue:parameters forKeyPath:
[NSString stringWithFormat:@"root.%@.parameters", specType]];
return oldTemplates;
}
我就可以建立一个循环来写的updateTemplates.specType
一个对象在一个时间,所以只有那些部分被替换,然后我不知道必须对参数做任何事情,但是如果它现在是不可变的,那么当我尝试再次写入它时将会如此。这对我没有任何好处。“ - [__ NSCFDictionary的setObject:forKey:]:变异的方法发送到不可变对象”
这可能是件好事同时记录'oldTemplates'和'parameters'只是为了看看什么类型的运行时间认为他们是。 (错误是否给出了对象地址?如果是这样,它是否与您在此代码中使用的其中一个字典匹配?) –