Alright, so I\'ve been doing some poking around, and I realize my problem, but I don\'t know how to fix it. I have made a custom class to hold some data. I make objects fo
Taking @chrissr's answer and running with it, this code can be implemented into a nice category on NSUserDefaults
to save and retrieve custom objects:
@interface NSUserDefaults (NSUserDefaultsExtensions)
- (void)saveCustomObject:(id)object
key:(NSString *)key;
- (id)loadCustomObjectWithKey:(NSString *)key;
@end
@implementation NSUserDefaults (NSUserDefaultsExtensions)
- (void)saveCustomObject:(id)object
key:(NSString *)key {
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
[self setObject:encodedObject forKey:key];
[self synchronize];
}
- (id)loadCustomObjectWithKey:(NSString *)key {
NSData *encodedObject = [self objectForKey:key];
id object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
return object;
}
@end
Usage:
[[NSUserDefaults standardUserDefaults] saveCustomObject:myObject key:@"myKey"];