我正在阅读一些关于在 NSUserDefaults 中存储自定义对象的教程。我不知道在将模型转换回 NSDictionary 并存储字典与使用 NSKeyedArchiver 存储对象后,存储模型的更好方法和/或好处是什么在 NSUserDefaults 中。根据我的理解,您必须设置 encodeWithCoder 和 initWithCoder 方法,这将使您能够设置各种键的值并将所有内容基本上转换为 NSData 。将模型转换回 NSDictionary 也将遵循相同的步骤。
那么使用一种方法比另一种方法有什么好处,或者如果一种方法可能导致某些地方出现问题?
使用 NSKeyedArchiver 将执行以下操作:
- (void)encodeWithCoderNSCoder *)encoder {
[encoder encodeObject:self.test forKey"string"];
[encoder encodeObject:self.surname forKey"surname"];
}
并将模型转换回 NSDictioanry :
-(NSDictionary *)dictionaryWithModelModel *)model{
NSDictionary *dictionary = @{
@"dict":[model.innerModel dictionaryWithModel:model.innerModel],
@"string":self.test
};
return dictionary;
}
在 NSUserDefaults 中存储对象时,基本上哪个会更好?
编辑:所以根据答案中链接的文档,我们应该使用 NSDictionary 将对象存储在 NSUserDefaults 中(性能更好),那为什么苹果推荐使用NSKeyedArchiver ?
Best Answer-推荐答案 strong>
Here是我对另一个类似问题的回答。
你可以这样设置对象:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:savingbean];
[currentDefaults setObject:data forKey"DATA"];
[currentDefaults synchronize];
对于像这样获取对象:
NSData *data = [currentDefaults objectForKey"DATA"];
SavingBean *token = [NSKeyedUnarchiver unarchiveObjectWithData:data];
对于自定义类,您必须在 bean 类中编辑此方法:
- (void)encodeWithCoderNSCoder *)encoder
{
[encoder encodeObject:self.userName==nil?@"":self.userName forKey: @"userName"];
[encoder encodeObject:self.passWord==nil?@"":self.passWord forKey: @"passWord"];
}
-(id)initWithCoderNSCoder *)decoder
{
self = [super init];
if(self)
{
self.userName = [decoder decodeObjectForKey: @"userName"];
self.passWord = [decoder decodeObjectForKey: @"passWord"];
}
return self;
}
Here是投票最多的类似答案。你也可以从那里得到好东西。
您也可以使用 JSON Accelerator创建 bean 类。这是一个非常简单而强大的工具。
编辑:
The NSUserDefaults class provides convenience methods for accessing
common types such as floats, doubles, integers, Booleans, and URLs. A
default object must be a property list, that is, an instance of (or
for collections a combination of instances of): NSData, NSString,
NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any
other type of object, you should typically archive it to create an
instance of NSData.
Values returned from NSUserDefaults are immutable, even if you set a
mutable object as the value. For example, if you set a mutable string
as the value for "MyStringDefault", the string you later retrieve
using stringForKey: will be immutable.
Note: The user defaults system, which you programmatically access
through the NSUserDefaults class, uses property lists to store objects
representing user preferences. This limitation would seem to exclude
many kinds of objects, such as NSColor and NSFont objects, from the
user default system. But if objects conform to the NSCoding protocol
they can be archived to NSData objects, which are property
list–compatible objects. For information on how to do this, see
““Storing NSColor in User Defaults”“; although this article focuses on
NSColor objects, the procedure can be applied to any object that can
be archived.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html
编辑:
When defining your app’s preferences, it is better to use simple
values and data types whenever possible. The preferences system is
built around property-list data types such as strings, numbers, and
dates. Although you can use an NSData object to store arbitrary
objects in preferences, doing so is not recommended in most cases.
Storing objects persistently means that your app has to decode that
object at some point. In the case of preferences, a stored object
means decoding the object every time you access the preference. It
also means that a newer version of your app has to ensure that it is
able to decode objects created and written to disk using an earlier
version of your app, which is potentially error prone.
A better approach for preferences is to store simple strings and
values and use them to create the objects your app needs. Storing
simple values means that your app can always access the value. The
only thing that changes from release to release is the interpretation
of the simple value and the objects your app creates in response.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html#//apple_ref/doc/uid/10000059i-CH2-SW2
关于ios - 将自定义对象存储为 NSDictionary 与 NSData,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/40189115/
|