1

我环顾四周,看到了几篇关于对包含 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"];
}

如果有人能指出我正确的方向,我将不胜感激。非常感谢你!或者,如果有人有更好的策略来跟踪项目,每个项目都有不同的偏好,那也太棒了!

谢谢!!

4

2 回答 2

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
于 2012-07-30T23:45:33.620 回答
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);
于 2012-07-31T17:11:14.603 回答