iphone - 如何检查 NSUserDefaults 是否存在

标签 iphone objective-c nsuserdefaults

我一直在寻找答案,但没有真正找到我要找的东西。

我有一个应用程序,正在使用 NSUserDefaults 来存储“currentGameStatus”,我想问以下问题:

  1. 如何检查 NSUserDefaults .plist 是否存在?需要这个来确定我是否需要第一次创建它,如果需要用默认值填充它

  2. 在我的 Mac(运行模拟器)上哪里可以找到它?是否需要删除它以测试第一次运行是否有效?

最佳答案

你不检查。

您注册您的默认值。如果您没有保存值,将使用默认值。

NSDictionary *defaultUserDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithBool:NO], @"Foo",
                                     @"Bar", @"Baz",
                                     [NSNumber numberWithInteger:12], @"FooBar",
                                     nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultUserDefaults];

并且您每次您的应用启动时都这样做。

关于iphone - 如何检查 NSUserDefaults 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4774671/

相关文章:

ios - 将 UIImage 保存到 UserDefault 为数组,Swift

ios - 分解 iOS 应用程序中不同存储方法的用例

iphone - 拍摄照片时切换 AVCaptureSession 预设

iphone - 如何将使用iOS SDK拍摄的照片添加到用户的相机胶卷中?

C++ 代码中的 iPhone/iPad fatal error 在日志中不产生任何输出

objective-c - NSSharingService 与 GIF 动画?

ios - 从 NSUserDefaults 解码自定义对象数组时出错?

iphone - 打破照片 ALAssetsLibrary 的迭代

iphone - ScrollView 中的 UITableView

ios - 如何从屏幕顶部为 TableView Controller 显示 UISearchBar