创建一个包含多个NSMutableDictionary对象的NSMutableArray

时间:2012-07-30 23:36:48

标签: objective-c ios nsmutablearray nsdictionary nsuserdefaults

我环顾四周,看过几篇关于排序包含NSDictionaries的NSMutableArray的帖子,但是却找不到一些如何实际制作由NSDictionaries组成的NSMutableArray的具体例子。

我正在使用NSUserDefaults来跟踪用户对多个不同项目的偏好。 NSMutableArray将用于跟踪总项目,NSDictionaries用于关于每个项目的个人偏好。

我已经编写了这些方法来跟踪我的首选项类中的项目,但我不太确定如何初始化NMutableArray以包含NSDictionaries。

-(NSMutableArray *)deviceListArray
{
    return [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"device_list_array"];
}

-(void) setDeviceListArray:(NSMutableArray *)deviceListArray
{
    [[NSUserDefaults standardUserDefaults] setObject:deviceListArray forKey:@"device_list_array"];
}

如果有人能指出我正确的方向,我会非常感激。非常感谢你!或者,如果某人有更好的策略来跟踪每个具有不同偏好的项目,那么这也很棒!

谢谢!

2 个答案:

答案 0 :(得分:4)

将对象交给userDefaults后,它拥有该对象 - 因此您无法更改其中的字典属性。

你可以做的是一个两遍副本,你有一个带有可变字典的本地可变数组。您将创建一个相同大小的新可变数组,然后为每个可变字典调用[NSDictionary dictionaryWithDictionary:],将新的非可变字典添加到新数组中。

如果你的字典中有可变对象,那么你需要做同样的事情。

所有这一切,如果你有如此大量的东西,我怀疑你没有正确考虑如何使用默认系统。我几乎总是保存字符串,数字,颜色等。

编辑:代码

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

for(int i=0; i<10; ++i) {
  NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
  // add stuff to the dictionary
  [array addObject:dict];
}

// later on want to change second dictionary:
NSMutableDictionary *dict = [array objectAtIndex:1];
// fool around with dict
// no need to re-save it in array, since its mutable

答案 1 :(得分:1)

不确定这是否是您要找的:

    #define kDeviceKey1 @"DeviceKey1"
    #define kDeviceKey2 @"DeviceKey2"
    #define kDeviceKey3 @"DeviceKey3"
    #define kDeviceKey4 @"DeviceKey4"
    #define kDeviceID @"DeviceID"

    NSMutableArray *devices = [[NSMutableArray alloc]init];
    NSMutableDictionary *deviceInfo = [[NSMutableDictionary alloc]init];
    // create a dictionary record for earch device which contains thing...
    // probably employ some kind of loop
    deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       [NSNumber numberWithInt:1], kDeviceID, 
//                                        @"DeviceName1", kDeviceID,   // or this
                                       @"whateverthing1forDevice1", kDeviceKey1,
                                       @"whateverthing2forDevice1", kDeviceKey2,
                                       @"whateverthing3forDevice1", kDeviceKey3,
                                       @"whateverthing4forDevice1", kDeviceKey4,
                                       nil];

    [devices addObject:deviceInfo];
    // this is just an example, you would have some kind of loop to replace "whateverthing1forDevice1" with actual data
    deviceInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                  [NSNumber numberWithInt:2], kDeviceID,
//                                        @"DeviceName2", kDeviceID,   // or this
                  @"whateverthing1forDevice2", kDeviceKey1,
                  @"whateverthing2forDevice2", kDeviceKey2,
                  @"whateverthing3forDevice2", kDeviceKey3,
                  @"whateverthing4forDevice2", kDeviceKey4,
                  nil];

    [devices addObject:deviceInfo];

    NSLog(@"devices: %@", devices);