-[NSUserDefaults registerDefaults:] 有什么用? [英] What is the use of -[NSUserDefaults registerDefaults:]?

查看:17
本文介绍了-[NSUserDefaults registerDefaults:] 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

[[NSUserDefaults standardUserDefaults] registerDefaults:
  [NSDictionary dictionaryWithObjectAndKey:anObject, @"something"]];

还有这个:

[[NSUserDefaults standardUserDefaults] setObject:anObject forKey:@"something"];

推荐答案

不同之处在于您注册的第一个代码片段将在用户未对属性"进行任何更改时使用.

The difference is that the first code-snippet you register defaults that will be used when the user has not made any changes to the "property".

因此,如果您想提供一个键名为欢迎消息"的属性",您可以代替返回 nil 的属性插入一条默认消息欢迎首次使用的用户",该消息将在出现时显示属性没有变化.

So if you want to provide let's say a "property" with the key name 'Welcome message', you could instead of having the property returning nil insert a default message 'Welcome first-time user' that will be displayed when there have been no changes to the property.

这将简化您的逻辑,因为您无需编写 if 测试来检查属性"是否返回 nil,如果是这种情况,则再发送另一条消息.

This will simplify your logic because you don't need to write an if test to check if the "property" returns nil and then make another message if this is the case.

NSString *greeting = [[NSUserDefaults standardUserDefaults] stringForKey:@"Greeting"];

if(greeting == nil) {
    NSLog(@"Welcome first-time user!");
}

您发布的第二个代码片段用于将属性设置为另一个值.您将有不同的设置方法(setString、setObject、setBoolean)来根据您在 Userdefaults 中的程序状态来设置值.

The second code-snippet you posted is for setting the property to another value. You will have different set methods (setString, setObject, setBoolean) to set values depending on your program state in the Userdefaults.

EDIT----根据评论中的要求进行更新.

EDIT-----Updates as requested in comment.

顾名思义,第一种方法是将值注册为默认值.第一次使用某个键名访问属性时,对象的值将为 nil,布尔值为 false 或数字为 0.如果值未在程序中设置,而不是进行大量测试等,然后执行一些默认"操作,例如上面的示例,您可以使用这些键的一些已经预定义的值来交付应用程序.

The first method is for registering values to defaults, as the name implies. The first time you access the property with some key name the value will be either nil for objects, false for booleans or 0 for numbers. Instead of doing a lot of tests and so on to so if the values is not set in the program, and then do something "default" action such as the example above, you can ship your application with some already predefined values for these keys.

放置 registerDefaults 的典型位置是在 appDelegate 的初始化方法中.

A typical place to put the registerDefaults is in the initializer-method in the appDelegate.

然后在你的程序的某个地方你可能想要设置这些字段的值,然后你使用 setObject、setString、setBoolean...并且为了检索你使用 stringForKey、objectForKey...

Then somewhere in your program you may want to set the values of these fields then you use the setObject, setString, setBoolean...and for retrieving you use stringForKey, objectForKey...

这样想

registerDefaults 是构造函数,您可以在其中为对象提供合理的值,否则您将获得一些我已经编写的默认值.然后,如果您想更改对象的属性,则不要使用构造函数",而是使用 set/get 方法.

The registerDefaults is the constructor where you may supply sensible values for the object, otherwise you get some defaults which I already wrote. Then later if you want to change the object's attributes you do NOT use the "constructor" but the set/get methods.

这篇关于-[NSUserDefaults registerDefaults:] 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆