【问题标题】:NSMutableDictionary in NSMutableArrayNSMutableArray 中的 NSMutableDictionary
【发布时间】:2013-10-21 09:07:59
【问题描述】:

我的NSMutableArray 填充了NSMutableDictionary 对象,我该如何检索NSMutableArray 中的第二个项目并更改该字典。

NSMutableArray *tempArr = [[NSMutableArray alloc]init];
NSMutableDictionary *al = [[NSMutableDictionary alloc]init];

for(int i=0; i<[theArray count]; i++) {
    id object = [theArray objectAtIndex: i];
    id object2 = @"0";
    [al  setObject:object forKey:@"titles"];
    [al setObject:object2 forKey:@"levels"];
    [tempArr addObject:al];
}


NSLog(@"fubar %@", [tempArr objectAtIndex:2]);

所以我需要通过 Key 访问 NSDictionary 并更改 Key 的值。谢谢

【问题讨论】:

    标签: objective-c nsmutablearray nsmutabledictionary


    【解决方案1】:

    如您所见,数组中的对象是使用NSArray 上的-objectAtIndex: 方法访问的。

    类似地,字典中的对象是使用-objectForKey:NSDictionary 进行的访问。

    要做你想做的事,只需像任何其他调用一样堆叠访问,如下所示:

    NSLog(@"fubar %@", [[tempArr objectAtIndex:2] objectForKey:@"titles"]);
    

    注意,NSArray 上的索引从 0 开始,而不是 1。

    要更改值,适用相同的原则:

    [[tmpArr objectAtIndex:2] setObject:titlesArray forKey:@"titles"];
    

    【讨论】:

    • 如何在数组中找到字典的这个例子中找到索引为“2”的索引?
    • @Ron 我不太清楚你的意思; -indexOfObject:,也许吧?
    • 我有一本带有特定键和值的字典。该字典位于 NSMutableArray 中。我想用一个特定的键更新字典,这个字典位于数组内的某个索引处。我想找到这个字典对象的索引。目前,我的字典在索引 0 处,而我的 indexpath 是 1,所以 'indexpath.row' 对我没有帮助。这对你有意义吗?谢谢
    【解决方案2】:

    如果您想将NSMutableDictionary 存储中的值替换为NSMutableArray,请使用以下代码:

    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",nil],[NSMutableDictionary dictionaryWithObjectsAndKeys:@"value2",@"key2",nil], nil];
    
    //Edit second value
    NSMutableDictionary *dict = (NSMutableDictionary *)[array objectAtIndex:1];
    [dict setObject:@"value3" forKey:@"key2"];
    

    【讨论】:

      【解决方案3】:
      NSMutableDictionary *tempDicts = (NSMutableDictionary *)[yourArray objectAtIndex:yourIndex];
      // Change your tempDicts 
      [tempDicts setObject:@"" forKey:@""];    
      

      最后

      [yourArray insertOjbect:tempDicts atIndex:yourIndex];
      

      【讨论】:

        【解决方案4】:

        假设数组是一个实例变量(称为_array):

        - (void)setObject:(id)value
                   forKey:(id)key
                  atIndex:(NSInteger)index {
            if (index >= [_array count]) {
                NSLog(@"Index %ld out-of-range", index);
                return;
            }
            NSMutableDictionary *dict = [_array objectAtIndex:index];
            [dict setObject:value forKey:key];
        }
        

        使用方法:

        [self setObject:@"foobar" forKey:@"title" atIndex:2];
        

        【讨论】:

          【解决方案5】:

          试试这个:

          NSMutableDictionary *dict = [NSMutableDictionary dictionary];
              for(dict in tempArr)
              {
          
                 //you can access dictionay here and set the value 
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-24
            • 1970-01-01
            • 1970-01-01
            • 2014-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多