更改NSUserDefaults中的对象,而不创建和重新设置副本 [英] Change objects in NSUserDefaults without creating and re-setting copies

查看:119
本文介绍了更改NSUserDefaults中的对象,而不创建和重新设置副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字典存储在 NSUserDefaults ,我需要在此字典中添加/删除项目。

I've got dictionary stored in NSUserDefaults and I need to add/remove items in this dictionary.

这让我困扰我,我必须创建整个字典的可变拷贝,更改一个元素,并用新的副本替换整个字典。

It bothers me that to do this I have to create mutable copy of entire dictionary, change one element and replace entire dictionary with new copy.

copy = [[defaults objectForKey:@"foo"] mutableCopy];
[copy setObject:… forKey:@"bar"];
[defaults setObject:copy forKey:@"foo"];

,它涉及对层次结构中更深的对象的更多复制和重新设置。

and it involves even more copying and re-setting for objects deeper in the hierarchy.

有更好的方法吗?

我试图使用 [defaults setValue:... forKeyPath :@foo.bar] 但是似乎不工作(对象不可变)。

I've tried to use [defaults setValue:… forKeyPath:@"foo.bar"] but that doesn't seem to work (object is not mutable).

推荐答案

我通常创建一个自定义类来保存我的所有应用程序首选项。该类可以在程序启动时加载userDefaults的可变拷贝,然后按照以下方式处理所有增量保存:

I usually create a custom class to hold all of my application preferences. That class can load mutable copies of the userDefaults once, when the program starts, and then handle all of the incremental saves along the way:

MyPreferences.h

@interface MyPreferences
{
   NSMutableDictionary allPrefs;
}
@property (readonly) NSMutableDictionary * allPrefs;
- (void)load;
- (void)save;
@end

MyPreferences.m b
$ b

MyPreferences.m

@implementation MyPreferences
@synthesize allPrefs;

- (id)init
{
    if ((self = [super init]) == nil) { return nil; }
    allPrefs = [[NSMutableDictionary alloc] initWithCapacity:0];
    return self;
}

- (void)dealloc
{
    [allPrefs release];
    [super dealloc];
}

- (void)load
{
    // load all mutable copies here
    [allPrefs setObject:[[defaults objectForKey:@"foo"] mutableCopy]
                 forKey:@"foo"];
    // ...
}

- (void)save
{
    [defaults setObject:allPrefs forKey:@"app_preferences"];
}

@end

类,然后在我的应用程序启动时调用 [myPrefs load] 。通过 myPrefs 可以修改程序运行时更改的任何首选项,然后通过调用 [myPrefs save] 希望:

I create an instance of this class in my application delegate and then call [myPrefs load] when my application launches. Any preferences changed while the program is running can be modified through myPrefs, and then saved by calling [myPrefs save] as desired:

MyPreferences * myPrefs = [myApplication myPrefs];
[myPrefs setObject:bar forKeyPath:@"allPrefs.foo.bar"];
[myPrefs save];

作为一个额外的好处,您可以构造 MyPreferences class以任何你喜欢的方式,将OO编程的好处带给整套偏好。我在这里展示了简单的方法,只是使用可变字典,但是你可以将每个偏好设置为一个属性,并对更复杂的对象,如 NSColor 进行前/

As an added bonus, you can structure the MyPreferences class any way you like, bringing the benefits of OO programming to the whole set of preferences. I showed the easy way here, simply using a mutable dictionary, but you can make each preference into a property, and do pre/post processing for more complicated objects like NSColor, for instance.

这篇关于更改NSUserDefaults中的对象,而不创建和重新设置副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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