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

查看:961
本文介绍了什么是 - [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!");
}

您发布的第二个代码段用于将属性设置为另一个值。您将有不同的set方法(setString,setObject,setBoolean)根据用户默认中的程序状态设置值。

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-----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中的initializer方法中。

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天全站免登陆