你的位置:首页 > 软件开发 > 操作系统 > iOS Programming NSUserDefaults

iOS Programming NSUserDefaults

发布时间:2015-05-28 12:00:06
iOS Programming NSUserDefaults  When you start an app for the first time, it uses its factory settings. As you use it, a good app lea ...

iOS Programming NSUserDefaults

iOS Programming NSUserDefaults 

When you start an app for the first time, it uses its factory settings. As you use it, a good app learns your preferences. Where are your preferences stored? Inside each app bundle there is a plist that holds the user's preferences. As a developer, you will access this plist using the NSUserDefaults class. The preferences plist for your app can also be edited by the Settings app. To allow this, you create a settings bundle inside your app.

当你第一次开始你的app时,你使用它的工厂设置。当你使用它时,一个好的app 会学习你的preferences.你的preferences 存在哪了呢?在每个app bundle中都有一个plist 保持了user的preference.作为一个developer,你可以通过使用NSUserDefaults类。preferences plist 你的app能够通过setting app 编辑。为了允许这个,你需要创建一个setting bundle 在你的app内。

1 NSUserDefaults

The set of defaults for a user is a collection of key-value pairs. The key is the name of the default, and the value is some data that represents what the user prefers for that key.You ask the shared user defaults object for the value of that key – not unlike getting an object from a dictionary:

对一个用户的defaults 的设置是一个key-value pairs 容器。key 是default的名字,而value是代表饿了用户喜欢什么的数据。你询问shared user defaults 对象为了那个key 的value,很像从一个字典中获取对象。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *greeting = [defaults objectForKey:@"FavoriteGreeting"];

If the user expresses a preference, you can set the value for that key:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:@"Hello" forKey:@"FavoriteGreeting"];

This value will automatically be stored to the app's preferences plist. Thus, the value must be a plist type: NSArray, NSDictionary, NSString, NSData, NSDate, or NSNumber. If you want to store a non-plist type to the user defaults, you will need to convert it to a plist. Often this is accomplished by archiving the object (or objects) into an NSData, which is a plist.

这些值自动的存入app的preference plist.因此这个value必须是plist type:NSArray,NSDictionary,NSString,NSData,NSDate,or NSNumber.如果你是存的一个non-plist,你需要把它转换为plist。通常这会存档一个对象为NSData。

What if you ask for the value of a preference that has not been set by the user? NSUserDefaults will return the factory settings, the "default default," if you will. These are not stored on the file system, so you need to tell the shared instance of NSUserDefaults what the factory settings are every time your app launches. And you need to do it early in the launch process – before any of your classes try to read the defaults. Typically you will override +initialize on your app delegate:

如果你询问一个preference的值还没有被设置?NSUserDefaults将会返回factory settings ,the "default default",如果你乐意。这些没有存储到文件系统中,所以你需要告诉NSUserDefaults 的shared instance  在你的app 每次启动时要设置什么factory settings .你需要做这些在启动程序的早期:在你的类视图读取defaults之前。一般你回重+initialize 在你的app delegate上。

+ (void)initialize

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *factorySettings = @{@"FavoriteGreeting": @"Hey!",

@"HoursBetweenMothershipConnection : @2};

[defaults registerDefaults:factorySettings];

}

The class method initialize is called automatically by the Objective-C runtime before the first

instance of that class is created.

这个类方法initialize 在那个类被创建的Objective -C 运行时间自动的被调用。

1.1 Register the factory settings

注册factory Settings

At launch time, the first thing that will happen is the registering of the factory settings. It is considered good style to declare your preference keys as global constants.

启动时,将发生的第一件事就是factory settings 的注册。声明你的preference keys 为一个global  constants 是一种好的方式。Open BNRAppDelegate.h and declare two constant global variables:

extern NSString * const BNRNextItemValuePrefsKey;

extern NSString * const BNRNextItemNamePrefsKey;

In BNRAppDelegate.m, define those global variables and use them to register the factory defaults in

+initialize:

NSString * const BNRNextItemValuePrefsKey = @"NextItemValue";

NSString * const BNRNextItemNamePrefsKey = @"NextItemName";

 

@implementation BNRAppDelegate

+ (void)initialize

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *factorySettings = @{BNRNextItemValuePrefsKey: @75,

BNRNextItemNamePrefsKey: @"Coffee Cup"};

[defaults registerDefaults:factorySettings];

}

 

1.2 Read a preference      读取一个preference

When you create a new item in BNRItemStore.m, use the default values. Be sure to import BNRAppDelegate.h at the top of BNRItemStore.m so that the compiler knows about BNRNextItemValuePrefsKey.

 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

item.valueInDollars = [defaults integerForKey:BNRNextItemValuePrefsKey];

item.itemName = [defaults objectForKey:BNRNextItemNamePrefsKey];

// Just for fun, list out all the defaults

原标题:iOS Programming NSUserDefaults

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。