Question

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?

Was it helpful?

Solution 2

Save the ordered set as an array:

NSOrderedSet *set = ... // your ordered set
NSArray *array = [set array];
// Now write the array to the plist

Later,

// read the array from the plist
NSArray *array = ... // array from plist
NSOrderedSet *set = [NSOrderedSet orderedSetWithArray:array];

OTHER TIPS

You can use NSCoding. I find it pretty handy.

Save:

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];

retrieve:

my_set = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

Use NSOrderedSet - (NSArray *)array method to get the array object and use NSArray's - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag , to write to a plist file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top