Cocoa touch 动态初始化NSDisctionary
哇。我试图在这里使用一个Cocoa touch 动态初始化NSDisctionary,cocoa-touch,nsdictionary,Cocoa Touch,Nsdictionary,哇。我试图在这里使用一个NSMutableDictionary,使用一个checkNull方法,如果没有设置,则使用一些默认值初始化字典。但是,iOS模拟器在for循环中的第一次相遇时崩溃 错误消息: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber mutableCopyWithZone:]: unrecognized selector sent to
NSMutableDictionary
,使用一个checkNull
方法,如果没有设置,则使用一些默认值初始化字典。但是,iOS模拟器在for
循环中的第一次相遇时崩溃
错误消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber mutableCopyWithZone:]: unrecognized selector sent to instance
代码:
[self defaults]
返回[NSUserDefaults standardDefaults]
,而
[self channelsList]
返回一个包含大约10个NSString
对象的NSArray
我哪里做错了?
提前感谢NSNumber不响应
可变副本
你为什么要这么做?它们实际上是单例对象(事实上,对于较小的数字,它们实际上是单例对象)
此外,您不再需要将BOOL转换为NSNumber,您可以使用文本
+ (void)checkNull {
if ([[self defaults] valueForKey:@"channels"] == nil) {
NSMutableDictionary *channels = [[NSMutableDictionary alloc] init];
for (NSString *channel in [self channelsList]) {
[channels setObject:@YES forKey:channel];
}
[[self defaults] setValue:channels forKey:@"channels"];
}
}
成功了。我想我误解了
mutableCopy
的概念。非常感谢。
+ (void)checkNull {
if ([[self defaults] valueForKey:@"channels"] == nil) {
NSMutableDictionary *channels = [[NSMutableDictionary alloc] init];
for (NSString *channel in [self channelsList]) {
[channels setObject:@YES forKey:channel];
}
[[self defaults] setValue:channels forKey:@"channels"];
}
}