jin*_*ini 13 nsdictionary foundation nsmutabledictionary ios
我正在尝试将"dateTime"添加到我的字典中,如下所示:
Symptom Ranking: {
5111ef19253b4a9150000000 = 1;
5111f029253b4add4e000000 = 1;
5111f036253b4a123d000001 = 1;
5111f045253b4a404f000000 = 1;
}
NSLog(@"date selected: %@", [[self.datePicker date] description])
[self.results setObject:[[self.datePicker date] description] forKey:@"dateTime"];
Run Code Online (Sandbox Code Playgroud)
应用程序崩溃,我得到这个:
Symptom Tracker[43134:c07] -[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7603990
2013-02-06 08:15:58.741 Symptom Tracker[43134:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7603990'
*** First throw call stack:
(0x171b012 0x1428e7e 0x17a64bd 0x170abbc 0x170a94e 0x521e 0x143c705 0x373920 0x3738b8 0x434671 0x434bcf 0x433d38 0x3a333f 0x3a3552 0x3813aa 0x372cf8 0x2652df9 0x2652ad0 0x1690bf5 0x1690962 0x16c1bb6 0x16c0f44 0x16c0e1b 0x26517e3 0x2651668 0x37065c 0x25dd 0x2505)
Run Code Online (Sandbox Code Playgroud)
bug*_*oaf 25
当我意外地声明了copy这样的属性时,我遇到了这个错误:
@property (nonatomic,copy) NSMutableDictionary* downloadHandlers;
Run Code Online (Sandbox Code Playgroud)
当我在我这样做时init:
self.downloadHandlers = [[NSMutableDictionary alloc] init];
Run Code Online (Sandbox Code Playgroud)
我实际上有一本不可变的字典.我copy原本以为调用一个可变对象也会给我一个可变对象,但显然不是.无论如何,删除copy关键字(我从来没有打算在那里)解决了问题.
作为排名靠前的答案,你需要使用NSMutableDictionary而不是NSDictionary.如果你想使用文字,请使用mutableCopy:
NSMutableDictionary* dict = [@{@"key": @"value"} mutableCopy];
Run Code Online (Sandbox Code Playgroud)
这样您就可以使用重新分配密钥了
dict[@"key"] = @"new-value";
Run Code Online (Sandbox Code Playgroud)
当我们处理Web服务响应时,大多数情况下会出现此问题,因为收到的数据是不可变的.当您尝试更改不可变数据时,应用程序肯定会崩溃.我希望以下代码片段能够提供帮助.
NSMutableDictionary *headerData;
/*Every time you need to allocate memory to the corresponding MutableDictionary variable*/
headerData =[[NSMutableDictionary alloc ]initWithDictionary:response[@"Header"]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23442 次 |
| 最近记录: |