iOS 用户默认值 - 我需要使用 setInteger :forKey: if I intend to retrieve primitives from defaults using integerForKey:?

标签 ios objective-c iphone nsuserdefaults

我正在尝试解压字典并将其存储在用户默认值中。该字典包含字符串和数字/ 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/

相关文章:

iphone - UIScrollView 中的动画

ios - 如何使用 Alamofire 将订单发布到 woocommerce 中?

ios - Swift BigInt 到 [UInt8]

ios - 如何在iOS App中完全注销Facebook SDK Auth

objective-c - 在 IOS 中关闭应用程序时将调用什么委托(delegate)方法?

iphone - 使用 Touchevent 在 jQuery 中拖放(iPhone 浏览器)

ios - 如何在ios中管理tableview内的按钮?

ios - 将 deviceToken 从 AppDelegate 传递到 swift 中的另一个类时出错

iPhone,检查常量是否存在

ios - 不在需要参数的方法中使用参数?