iPhone:Store&检索NSMutableArray对象

时间:2011-09-30 11:10:37

标签: objective-c ios

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

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

我成功地将对象mutabledictionary添加到mutuablearray中,但我想存储该数组并在应用程序启动时检索,所以请让我知道开发此功能。

提前致谢。

3 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:0)

也许是这样的:

NSMutableArray * array = [NSMutableArray array];

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

答案 2 :(得分: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];
}

希望这有帮助!