ios - 如果在安装应用程序时将值分配给 objective-c 中的变量,那么如何在每次打开应用程序时访问该值?

标签 ios objective-c

嗨,我是 Objective-C 的新手。

如果在应用程序安装时将值分配给 objective-c 中的变量,那么如何在每次打开应用程序时访问该值?我尝试使用 extern,static。每个都会在第一时间分配和设置值。如果我在模拟器中重新运行应用程序,它不会采用最后分配的值。

除了文件系统存储之外还有什么可能吗?是否可以使用静态或外部变量。

场景:

虽然安装“extern int test”分配给 10 然后它更改为 20。从另一个类访问时,测试反射(reflect) 20。如果我重新运行应用程序,“测试”显示“10”

但我想访问最后分配的值(就像java中的static)

感谢您的帮助!

最佳答案

使用 NSUserDefaults 设置 AppDelegate 中的值:

NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:20  forKey:@"abc"];
[defaults synchronize];

要再次获取值,请使用此方法:

NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
var = [defaults integerForKey:@"abc"];

关于ios - 如果在安装应用程序时将值分配给 objective-c 中的变量,那么如何在每次打开应用程序时访问该值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22505016/

相关文章:

iphone - ZXing读取一维条码iPhone

objective-c - NSPredicate 查找标题键以元音开头的所有对象

ios - 如何在不影响之前 View 的情况下更改 UINavigationBar 标题的名称?

ios - 如何使用 AVCapturePhotoOutput

iphone - NSManagedObject 子类作为属性的最佳实践

ios - 体系结构 x86_64 iOS swift 的 undefined symbol

objective-c - cocoa : how to set background color of a view as light green color in cocoa without using CALayer?

iphone - IOS:我可以在xcode中通过恢复id获取对象吗?

ios - iPad 应用程序在 iPad 3 上崩溃,在 iPad 2 上运行正常,内存不足警告

ios - 如何从 Visual Studio 中选择合适的 iPhoneSimulator 设备? (iPhoneSimulator 始终启动 3.5 英寸设备)