0
appDelegate.categoryData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                         categoryStr, @"name",image ,@"image", nil];

[appDelegate.categories addObject:appDelegate.categoryData];
NSLog(@"Category Data:--->%@",appDelegate.categories);

I am successfully added object mutabledictionary into mutuablearray but I want to store that array and retrieve when application is launched so please give me idea to develop this functionality.

Thanks in advance.

3
  • if you want that array permanently it is impossible because that array is initialize when the app is launch ..Solution is store that in userDefaults then u get any time any where in your app until reset your app Commented Sep 30, 2011 at 11:17
  • so please tell me how can i use NSUserDefaults to store & retrieve array Commented Sep 30, 2011 at 11:24
  • i add code below try using that you can get Commented Sep 30, 2011 at 11:34

3 Answers 3

1
    //you can save array in NSUserDefaults like below
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"detail"]==nil)
    {
        NSMutableDictionary *dic_detail = [NSMutableDictionary dictionaryWithObjectsAndKeys:
             categoryStr, @"name",image ,@"image", nil];
            NSMutableArray *ary_detail = [[NSMutableArray alloc] init];
            [ary_detail addObject:dic_detail];
            [[NSUserDefaults standardUserDefaults] setObject:ary_detail forKey:@"detail"];
            [[NSUserDefaults standardUserDefaults] synchronize];
     }

            //if you want read that array you can read like below in your app  
            NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"detail"]];

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        NSLog(@"applicationDidEnterBackground = %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"detail"]);
    }

    - (void)applicationWillEnterForeground:(UIApplication *)application {
        NSLog(@"applicationWillEnterForeground = %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"detail"]);
    }
in my log its printing like this 

    applicationDidEnterBackground = (
            {
            image = img1;
            name = cat1;
        }
    applicationWillEnterForeground = (
            {
            image = img1;
            name = cat1;
        }
Sign up to request clarification or add additional context in comments.

4 Comments

where I can retrieve store array after quit my app and again run with display categories which stored in array
thanks a lot for storing array but I can't retrieve that array after quit the app
in viewDidload method of RootViewController for retrieveing that array but array is not display in tableview
I am intialize my appDelegate.categories in appDelegate so any issue to retrieve that array please tell me
0

Perhaps something like this:

NSMutableArray * array = [NSMutableArray array];

appDelegate.categoryData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                         categoryStr, @"name",
                                         image ,@"image", 
                                         array, @"array", nil];

2 Comments

Can you be more specific? Do you mean you want to write the array to a file?
Ok I just store appDelegate.categories object & and quit the app and after that stored category is display in tableview
0

Something like:

On Application launch:

[[NSUserDefaults standardUserDefaults] registerDefaults:
   [NSDictionary dictionaryWithObjectsAndKeys:
      [NSArray new], @"StoredArray", nil]];

In your class that "owns" control of the array:

- (void)setArray:(NSMutableArray *)array {
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"StoredArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray*)array {
    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"StoredArray"] mutableCopy];
}

Hope this helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.