将 NSMutableArray 保存在 iOS
Save NSMutableArray in iOS
我需要用我的自定义对象保存许多 NSMutableArray,我想知道什么是最好的方法。
也许 NSUserDefaults 不是最好的方法。
我应该使用什么?
如果您的数组包含 非 plist 对象,then you cannot use NSUserDefaults
without first encoding the array.
The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.
您需要对其进行编码 using NSKeyedArchiver
. 这将为您提供一个 NSData
对象,然后您可以将其存储在 NSUserDefaults
中, 或通过 NSKeyedArchiver
本身将其写入文件。
您所要做的就是在您的自定义对象中遵循 NSCoding
,并覆盖 initWithCoder:
以在加载对象时初始化您的对象,并在对象获取时覆盖 encodeWithCoder:
对您的变量进行编码编码。例如,您的自定义对象将如下所示:
@interface customArrayObject : NSObject <NSCoding>
@property (nonatomic) NSString* foo;
@property (nonatomic) NSInteger bar;
@end
@implementation customArrayObject
-(instancetype) initWithCoder:(NSCoder *)aDecoder { // decode variables
if (self = [super init]) {
_foo = [aDecoder decodeObjectForKey:@"foo"];
_bar = [aDecoder decodeIntegerForKey:@"bar"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder { // encode variables
[aCoder encodeObject:_foo forKey:@"foo"];
[aCoder encodeInteger:_bar forKey:@"bar"];
}
@end
还值得注意的是 NSUserDefaults
用于存储用户 偏好 ,因此如果您的数组包含数据与用户偏好没有任何关系,您不应该使用 NSUserDefaults
- 您应该自己将其写入磁盘。
将数组写入磁盘实际上比听起来简单得多,您可以在 NSKeyedArchiver
上使用 archiveRootObject:toFile:
方法。例如,这会将您的自定义数组写入文档目录:
// Gets the documents directory path
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES)[0];
// Archive and save the file to foo.dat in the documents directory. Returns whether the operation was successful.
BOOL success = [NSKeyedArchiver archiveRootObject:customArray toFile:[NSString stringWithFormat:@"%@/%@", documentsPath, @"foo.dat"]]
然而,同样值得注意的是,关于操作是否成功的平淡 (yes/no) 在错误处理方面并不是那么好。 如果你想实现自定义错误处理,那么你需要先使用 NSKeyedArchiver
的 archivedDataWithRootObject:
方法对对象进行编码,然后再使用 NSData
的 writeToFile:options:error:
方法.
我需要用我的自定义对象保存许多 NSMutableArray,我想知道什么是最好的方法。
也许 NSUserDefaults 不是最好的方法。
我应该使用什么?
如果您的数组包含 非 plist 对象,then you cannot use NSUserDefaults
without first encoding the array.
The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.
您需要对其进行编码 using NSKeyedArchiver
. 这将为您提供一个 NSData
对象,然后您可以将其存储在 NSUserDefaults
中, 或通过 NSKeyedArchiver
本身将其写入文件。
您所要做的就是在您的自定义对象中遵循 NSCoding
,并覆盖 initWithCoder:
以在加载对象时初始化您的对象,并在对象获取时覆盖 encodeWithCoder:
对您的变量进行编码编码。例如,您的自定义对象将如下所示:
@interface customArrayObject : NSObject <NSCoding>
@property (nonatomic) NSString* foo;
@property (nonatomic) NSInteger bar;
@end
@implementation customArrayObject
-(instancetype) initWithCoder:(NSCoder *)aDecoder { // decode variables
if (self = [super init]) {
_foo = [aDecoder decodeObjectForKey:@"foo"];
_bar = [aDecoder decodeIntegerForKey:@"bar"];
}
return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder { // encode variables
[aCoder encodeObject:_foo forKey:@"foo"];
[aCoder encodeInteger:_bar forKey:@"bar"];
}
@end
还值得注意的是 NSUserDefaults
用于存储用户 偏好 ,因此如果您的数组包含数据与用户偏好没有任何关系,您不应该使用 NSUserDefaults
- 您应该自己将其写入磁盘。
将数组写入磁盘实际上比听起来简单得多,您可以在 NSKeyedArchiver
上使用 archiveRootObject:toFile:
方法。例如,这会将您的自定义数组写入文档目录:
// Gets the documents directory path
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES)[0];
// Archive and save the file to foo.dat in the documents directory. Returns whether the operation was successful.
BOOL success = [NSKeyedArchiver archiveRootObject:customArray toFile:[NSString stringWithFormat:@"%@/%@", documentsPath, @"foo.dat"]]
然而,同样值得注意的是,关于操作是否成功的平淡 (yes/no) 在错误处理方面并不是那么好。 如果你想实现自定义错误处理,那么你需要先使用 NSKeyedArchiver
的 archivedDataWithRootObject:
方法对对象进行编码,然后再使用 NSData
的 writeToFile:options:error:
方法.