Thursday, 21 June 2012

Store Array in UserDefaults and also Update and Remove Facility.


In AppDelegate.h file just declare variable...
NSUserDefaults  *userDefaults;
NSMutableArray *arrAppFavPot;
NSMutableArray *arrAppFavPlant;


after...

In AppDelegate.m Fille in applicationDidFinishLonching: Method

userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"arrAppFavPot"];
    if (dataRepresentingtblArrayForSearch != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearch];
        if (oldSavedArray != nil)
            arrAppFavPot = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            arrAppFavPot = [[NSMutableArray alloc] init];
    } else {
        arrAppFavPot = [[NSMutableArray alloc] init];
    }
    [arrAppFavPot retain];
    
    NSData *dataRepresentingtblArrayForSearchPlant = [userDefaults objectForKey:@"arrAppFavPlant"];
    if (dataRepresentingtblArrayForSearchPlant != nil) {
        NSArray *oldSavedArrayPlant = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingtblArrayForSearchPlant];
        if (oldSavedArrayPlant != nil)
            arrAppFavPlant = [[NSMutableArray alloc] initWithArray:oldSavedArrayPlant];
        else
            arrAppFavPlant = [[NSMutableArray alloc] init];
    } else {
        arrAppFavPlant = [[NSMutableArray alloc] init];
    }
    [arrAppFavPlant retain];

after that when you want to insert,update or delete Data from this UserDefaults Use Bellow Code...


        [appDelegate.arrAppFavPot removeObjectAtIndex:carouselPot.currentItemIndex];
        
or
    
        [appDelegate.arrAppFavPot addObject:[arrPot objectAtIndex:carouselPot.currentItemIndex]];
    
    
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate.arrAppFavPot];
    [appDelegate.userDefaults setObject:data forKey:@"arrAppFavPot"];
    [appDelegate.userDefaults synchronize];

No comments:

Post a Comment

Post a Comment