I need to save NSOrderedSet as a container for an inner NSDictionary objects. I tried to save the set to plist, but this class can't be saved here. Are there any other way to save it easily and don't go to Core Data jungle?
3 回答
4
您可以使用NSCoding
. 我觉得很方便。
节省:
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [array[0] stringByAppendingPathComponent:@"my_set"];
NSOrderedSet *my_set = [NSOrderedSet orderedSetWithObjects:@"a", @"b", @"c", nil];
[NSKeyedArchiver archiveRootObject:my_set toFile:path];
取回:
my_set = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
于 2014-02-17T18:57:53.617 回答
2
将有序集保存为数组:
NSOrderedSet *set = ... // your ordered set
NSArray *array = [set array];
// Now write the array to the plist
之后,
// read the array from the plist
NSArray *array = ... // array from plist
NSOrderedSet *set = [NSOrderedSet orderedSetWithArray:array];
于 2014-02-17T18:52:57.883 回答
1
使用 NSOrderedSet- (NSArray *)array
方法获取数组对象并使用 NSArray 的- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
, 写入 plist 文件
于 2014-02-17T18:55:13.493 回答