Objective c 无法更新NSMutableDictionary中的值
我知道有很多问题和我的一样,但我还是不能让它发挥作用 我有一个Objective c 无法更新NSMutableDictionary中的值,objective-c,dictionary,nsmutabledictionary,Objective C,Dictionary,Nsmutabledictionary,我知道有很多问题和我的一样,但我还是不能让它发挥作用 我有一个NSMutableDictionary,我甚至没有枚举,我只是想更改它的值,但我得到了错误消息: 变异对象发送到不可变对象 这是密码。。 我将我的字典作为一个参数,我们称它为myDictionary NSString *stringToUpdate = @"SomeString"; [myDictionary setObject:stringToUpdate forKey:@"time"]; 这是我买字典的地方 GameInfo.
NSMutableDictionary
,我甚至没有枚举,我只是想更改它的值,但我得到了错误消息:
变异对象发送到不可变对象
这是密码。。
我将我的字典作为一个参数,我们称它为myDictionary
NSString *stringToUpdate = @"SomeString";
[myDictionary setObject:stringToUpdate forKey:@"time"];
这是我买字典的地方
GameInfo.m
GameInfo.h
默认情况下,
+[NSJSONSerialization JSONObjectWithData:options:error:
将返回NSDictionary
的不可变NSArray
您需要获取该变量的副本并将其分配给实例变量
NSError *JSONError = nil;
jsonDict = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&JSONError] mutableCopy];
if (!jsonDict) {
NSLog(@"Failed to parse JSON: %@", JSONError.localizedDescription);
}
或者为JSON解析方法提供NSJSONReadingMutableContainers
选项
NSError *JSONError = nil;
jsonDict = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&JSONError];
if (!jsonDict) {
NSLog(@"Failed to parse JSON: %@", JSONError.localizedDescription);
}
错误很明显。您的
myDictionary
是一个NSDictionary
,而不是一个NSMutableDictionary
。但是我清楚地将它标记为NSMutableDictionary作为参数,并且我从中发送它的是一个NSMutableDictionary。假设它是一个NSDictionary,那么我如何解决它呢?变量类型是不相关的。实际对象不是可变字典。显示实际设置或获取的代码myDictionary
。
NSError *JSONError = nil;
jsonDict = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&JSONError] mutableCopy];
if (!jsonDict) {
NSLog(@"Failed to parse JSON: %@", JSONError.localizedDescription);
}
NSError *JSONError = nil;
jsonDict = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&JSONError];
if (!jsonDict) {
NSLog(@"Failed to parse JSON: %@", JSONError.localizedDescription);
}