Storing arrays in NSUserDefaultsController

Posted by neoneye on Stack Overflow See other posts from Stack Overflow or by neoneye
Published on 2010-04-20T05:18:23Z Indexed on 2010/04/20 5:23 UTC
Read the original article Hit count: 400

Currently I use NSUserDefaults and I'm interested in using NSUserDefaultsController, so that I get notifications when things change.

Below is my current code.

items = /* NSArray of MYItem's */;
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:items];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:kMYItems];

How should I rework my code to store items in NSUserDefaultsController ? Is NSKeyedArchiver the smartest way to store arrays?

@interface MYItem : NSObject <NSCoding> {
    NSString* name;
    NSString* path;
}
@property (copy) NSString* name;
@property (copy) NSString* path;
@end

@implementation MYItem
@synthesize name, path;

-(void)encodeWithCoder:(NSCoder*)coder {
    [coder encodeObject:name forKey:@"name"];
    [coder encodeObject:path forKey:@"path"];
}
-(id)initWithCoder:(NSCoder*)coder {
    [super init];
    [self setName:[coder decodeObjectForKey:@"name"]];
    [self setPath:[coder decodeObjectForKey:@"path"]];
    return self;
}
@end

© Stack Overflow or respective owner

Related posts about nsuserdefaults

Related posts about nsuserdefaultscontroller