2011-09-30 67 views
0
appDelegate.categoryData = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
             categoryStr, @"name",image ,@"image", nil]; 

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

我成功地将对象mutabledictionary添加到mutuablearray中,但我想存储该数组并在应用程序启动时检索,所以请给我一些想法来开发此功能。iPhone:存储和检索NSMutableArray对象

在此先感谢。

+0

如果你想数组永久这是不可能的,因为该数组时,应用程序是推出..Solution是该行存放在userDefaults然后u得到任何时候任何地方在您的应用程序,直到重新设置你的应用程序 – Narayana

+0

所以请初始化告诉我如何使用NSUserDefaults来存储和检索数组 –

+0

我在下面添加代码尝试使用,你可以得到 – Narayana

回答

0

也许是这样的:

NSMutableArray * array = [NSMutableArray array]; 

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

我想存储mutablearray任何想法? –

+0

你能更具体吗?你的意思是你想将数组写入文件吗? –

+0

好吧,我只是商店appDelegate.categories对象与和退出应用程序和存储类别后,在tableview中 –

0

喜欢的东西:

在应用程序启动:

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

在你的类 “拥有” 数组的控制:

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

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

希望这有助于!

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; 
     } 
+0

显示在那里我可以检索存储阵列退出后,我的应用程序,并再次与存储在阵列显示器类运行 –

+0

感谢了很多存储数组,但我不能检索数组中退出的RootViewController的viewDidLoad方法的应用 –

+0

后retrieveing该阵列,但阵列中没有实现代码如下显示 –