Wednesday 19 December 2012

UserDefault with NSMutableArray ..


    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 Data in this UserDefaults Use Bellow Code...
    
    [appDelegate.yourArray addObject:yourDataString];///set your value or anything which you want to store here...
    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:appDelegate.yourArray];
    [appDelegate.userDefaults setObject:data forKey:@"yourArray"];
    [appDelegate.userDefaults synchronize];

And when you want to retrive data just retrive like bellow..

 i hope this help you...

No comments:

Post a Comment