NSUserDefaults から「yourArray」を取得するには、コントローラーに次のように記述できます。
-(id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
yourArray = [[NSMutableArray alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults objectForKey: [NSString stringWithFormat:@"sample"]])
{
//if nothing saved
//... do whatever you want
//Use this array if there is nothing saved
NSLog(@"no saved array");
NSArray *modelArray1 = @[@"New York", @"London", @"Paris"];
yourArray=[modelArray1 mutableCopy];
}
else{
NSLog(@"existing array");
NSData *myDecodedObject = [defaults objectForKey: [NSString stringWithFormat:@"sample"]];
yourArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];
NSLog(@"decode array init=%u", yourArray.count);
}
}
return self;
}
保存用..
-(void)saving
{
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:yourArray];
[userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"sample"]];
[userDefault synchronize];
}