我正在尝试解压字典并将其存储在用户默认值中。该字典包含字符串和数字/ bool 值。
在我的用例中,我使用 intergerForKey:
和 boolForKey:
检索数字
对于此用例,我是否需要使用 setInteger:forKey: 在用户默认值中存储数字,或者我可以使用通用 setObject:forKey: 并给它一个数字*吗?
- (void)configureWithJSON:(NSDictionary *)conf {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *timeout = (NSNumber *)conf[@"timeout"];
if(timeout != nil) {
[defaults setInteger:[timeout intValue] forKey:@"timeout"];
}
//OR
[defaults setObject:timeout forKey:@"timeout"];
}
//then, in code I use
[defaults boolForKey:@"timeout"]?
最佳答案
最后,没关系,两者都可以。然后可读性就开始显现,setInteger
确实比 setObject
更清楚地表达了意图。
关于iOS 用户默认值 - 我需要使用 setInteger :forKey: if I intend to retrieve primitives from defaults using integerForKey:?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26143281/