NSUserDefaults:+ resetStandardUserDefaults方法的优点是什么?我该如何提供“系统设置”?

时间:2022-12-14 07:35:19

I want the user to be able to make some preferences like colors, prefered images, etc. When I use NSUserDefaults for this, on first start of the app there will be no preferences, right? So, every time I want to get a preference like

我希望用户能够做出一些偏好,如颜色,首选图像等。当我使用NSUserDefaults时,首次启动应用程序时将没有首选项,对吧?所以,每次我想获得喜欢的偏好

NSInteger avatarID = (NSInteger)[[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"];

NSInteger avatarID =(NSInteger)[[NSUserDefaults standardUserDefaults] objectForKey:@“avatar”];

I have to check if it's null, and then use my system preference. But then I've seen this in the docs: +resetStandardUserDefaults

我必须检查它是否为null,然后使用我的系统首选项。但后来我在文档中看到了这一点:+ resetStandardUserDefaults

Are there two branches of defaults saved somewhere? The ones from the user, and the ones from the developer?

在某处保存了两个默认分支吗?来自用户的那些,以及来自开发者的那些?

1 个解决方案

#1


Yes, but it's a little different than what you're doing. NSUserDefaults has the registerDefaults method, which lets you populate the "blank" defaults from an NSDictionary with values you provide. Put this in the +initialize method in your app controller, and you'll know the default values are sensible. If the user defaults object finds a "real" value for a key, either one you set during the current application launch or loaded from disk, it will always take precedent over what you provided in registerDefaults. resetStandardUserDefaults will remove the shared user defaults object from memory and undo any customization you've made to it, it's not something you'll need to provide default values.

是的,但它与你正在做的有点不同。 NSUserDefaults具有registerDefaults方法,该方法允许您使用您提供的值填充NSDictionary中的“空白”默认值。把它放在app控制器的+ initialize方法中,你就会知道默认值是合理的。如果用户默认对象找到键的“实际”值,无论是在当前应用程序启动期间设置还是从磁盘加载的值,它始终优先于您在registerDefaults中提供的值。 resetStandardUserDefaults将从内存中删除共享用户默认值对象并撤消您对其所做的任何自定义,这不是您需要提供默认值的东西。

Also, keep in mind you can't just cast an object to a primitive NSInteger value as you're doing, you'll just end up with a number representing the memory location of the object's pointer. Either use NSUserDefault's integerForKey: to get a primitive directly, or use objectForKey: to get an NSNumber and use integerValue to get the primitive value.

另外,请记住,您不能像往常一样将对象转换为原始NSInteger值,您最终会得到一个表示对象指针的内存位置的数字。使用NSUserDefault的integerForKey:直接获取原语,或使用objectForKey:获取NSNumber并使用integerValue获取原始值。

#1


Yes, but it's a little different than what you're doing. NSUserDefaults has the registerDefaults method, which lets you populate the "blank" defaults from an NSDictionary with values you provide. Put this in the +initialize method in your app controller, and you'll know the default values are sensible. If the user defaults object finds a "real" value for a key, either one you set during the current application launch or loaded from disk, it will always take precedent over what you provided in registerDefaults. resetStandardUserDefaults will remove the shared user defaults object from memory and undo any customization you've made to it, it's not something you'll need to provide default values.

是的,但它与你正在做的有点不同。 NSUserDefaults具有registerDefaults方法,该方法允许您使用您提供的值填充NSDictionary中的“空白”默认值。把它放在app控制器的+ initialize方法中,你就会知道默认值是合理的。如果用户默认对象找到键的“实际”值,无论是在当前应用程序启动期间设置还是从磁盘加载的值,它始终优先于您在registerDefaults中提供的值。 resetStandardUserDefaults将从内存中删除共享用户默认值对象并撤消您对其所做的任何自定义,这不是您需要提供默认值的东西。

Also, keep in mind you can't just cast an object to a primitive NSInteger value as you're doing, you'll just end up with a number representing the memory location of the object's pointer. Either use NSUserDefault's integerForKey: to get a primitive directly, or use objectForKey: to get an NSNumber and use integerValue to get the primitive value.

另外,请记住,您不能像往常一样将对象转换为原始NSInteger值,您最终会得到一个表示对象指针的内存位置的数字。使用NSUserDefault的integerForKey:直接获取原语,或使用objectForKey:获取NSNumber并使用integerValue获取原始值。