Can NSUserdefault save two arrays of same name or it replaces previous array?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
yes it replace with same variable and xcode not allowed to duplicate the same name of any object in same class so your application not compile until you not remove any one array name from class...
Update:-
In AppDelegate.h
file just declare variable...
NSUserDefaults *userDefaults;
NSMutableArray *yourArray;
after...
In AppDelegate.m
Fille in applicationDidFinishLonching:
Method
userDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"yourArray"];
if (dataRepresentingtblArrayForSearch != nil) {
NSArray *oldSavedArray = [NSKeyedUnarchiverunarchiveObjectWithData:dataRepresentingtblArrayForSearch];
if (oldSavedArray != nil)
yourArray = [[NSMutableArray alloc]initWithArray:oldSavedArray];
else
yourArray = [[NSMutableArray alloc] init];
} else {
yourArray = [[NSMutableArray alloc] init];
}
[yourArray retain];
after that when you want to insert,update or delete Data from this userDefaults Use Bellow Code...
For Delete record use bellow
[appDelegate.yourArray removeObjectAtIndex:IndexValue];// define integer value here on IndexValue
or for insert record use bellow..
[appDelegate.yourArray addObject:AddValueHere];
or for replace whole array with new array then write this
appDelegate.yourArray = YourNewArray;
[appDelegate.yourArray retain];
after that for save whole your data synchronize
NSUserDefaults
like bellow...
NSData *data=[NSKeyedArchiverarchivedDataWithRootObject:appDelegate.yourArray];
[appDelegate.userDefaults setObject:data forKey:@"yourArray"];
[appDelegate.userDefaults synchronize];