在AppDelegate.h
文件中只声明变量...
NSUserDefaults *userDefaults;
NSMutableArray *arrDate;
后...
AppDelegate.m
填入applicationDidFinishLonching:
法_
userDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"arrDate"];
if (dataRepresentingtblArrayForSearch != nil) {
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearch];
if (oldSavedArray != nil)
arrDate = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
arrDate = [[NSMutableArray alloc] init];
} else {
arrDate = [[NSMutableArray alloc] init];
}
[arrDate retain];
之后,当您想从此 UserDefaults 插入、更新或删除数据时,请使用波纹管代码...
从数组中删除记录
[appDelegate.arrDate removeObjectAtIndex:Index];/// give the integer value instead of Index
这是用于在数组中添加记录..
[appDelegate.arrDate addObject:currentDate];///add value or add date here
在最终将这个修改保存在数组中之后,就像下面这样存档..
NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate.arrDate];
[appDelegate.userDefaults setObject:data forKey:@"arrDate"];
[appDelegate.userDefaults synchronize];