【问题标题】:Create an NSMutableArray that contains several NSMutableDictionary objects创建一个包含多个 NSMutableDictionary 对象的 NSMutableArray
【发布时间】:2012-07-30 23:36:48
【问题描述】:

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

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

谢谢!!

【问题讨论】:

    标签: objective-c ios nsmutablearray nsdictionary nsuserdefaults


    【解决方案1】:

    一旦您将一个对象交给 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
    

    【讨论】:

    • 嘿,谢谢大卫。我其实没有那么多偏好。它与显示无关,仅与与外部设备的交互有关,而且我只需要为每个设备保存大约 4 件东西。您能否详细说明分配包含可变字典的可变数组的正确方法?那是真正让我感到震惊的部分。我只是找不到一个很好的例子。
    【解决方案2】:

    不确定这是否是您要查找的内容:

        #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);
    

    【讨论】:

    • 嘿,非常感谢这个例子。您能否也给我在设备内部时抓取“whateverthing2forDevice2”的语法。我必须先拿起字典,然后从那里取出,还是可以一次性完成?
    • 我现在有两步,所以没什么大不了的。我只是想知道这是否可能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多