Wednesday 7 November 2012

NSUserDefault with insert,update,delete facility.


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 = [NSKeyedUnarchiver unarchiveObjectWithData: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 from array
[appDelegate. yourArray removeObjectAtIndex:Index];/// give the integer value instead of Index
and this is for add record in array..
[appDelegate. yourArray addObject:AnyValue];///add value or here
after in final save this modification in array just archive like bellow..
NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate. yourArray];[appDelegate.userDefaults setObject:data forKey:@"yourArray"];[appDelegate.userDefaults synchronize];
i hope this is helpful to you..

No comments:

Post a Comment