@rogが言ったように、NSUserDefaults
データを保存するために使用できます
& オブジェクトをプロトコールに従うようにする必要がありますNSCoding
たとえば、オブジェクトが「YouObject」の場合
@interface YouObject: NSObject {
}
@property (nonatomic, copy) NSString *uid;
@property (nonatomic, copy) NSString *name;
@end
//implement this 2 method
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.title = [decoder decodeObjectForKey:@"uid"];
self.author = [decoder decodeObjectForKey:@"name"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:title forKey:@"uid"];
[encoder encodeObject:author forKey:@"name"];
}
次に、を使用してアーカイブまたはアーカイブ解除しますNSUserDefaults
//archive
YouObject *object = [YouObject ....]
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object ];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"address"];
//unarchive
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
YouObject *object = (YouObject *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
または、YouObject Array がある場合は、NSArray を同じ方法で保存できます。
//archive
NSArray *addresses;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:address ];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"address"];
//unarchive
NSData *addressData = [[NSUserDefaults standardUserDefaults] objectForKey:@"address"];
NSArray *addresses = (NSArray*)[NSKeyedUnarchiver unarchiveObjectWithData:address];