【问题标题】:Trying add NSMutableDictionary to NSMutableArray尝试将 NSMutableDictionary 添加到 NSMutableArray
【发布时间】:2014-06-06 04:44:32
【问题描述】:

我是 Objective-C 和 iOS 编程的新手,所以要温柔:)

我正在尝试向 nsmutablearray 添加一个 nsmutabledictionary。我成功了,但没有达到我希望的结果。这是我的代码:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *messages = [[NSMutableDictionary alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];



[dictionary setValue:@"lat1" forKey:@"lat"];
[dictionary setValue:@"long1" forKey:@"long"];
[dictionary setValue:@"alt1" forKey:@"alt"];


[messages setObject:dictionary forKey:@"messages"];

[array addObject:messages];

[dictionary setValue:@"lat2" forKey:@"lat"];
[dictionary setValue:@"long2" forKey:@"long"];
[dictionary setValue:@"alt2" forKey:@"alt"];

[messages setObject:dictionary forKey:@"messages"];

[array addObject:messages];


NSLog(@"%@",array);
NSLog(@"%lu",(unsigned long)[array count]);

这里是 NSLog 输出:

2014-06-05 10:29:27.377 dicttest[4863:60b] (
    {
    messages =         {
        alt = alt2;
        lat = lat2;
        long = long2;
    };
},
    {
    messages =         {
        alt = alt2;
        lat = lat2;
        long = long2;
    };
}

) 2014-06-05 10:29:27.386 字典测试[4863:60b] 2

这是我希望实现的目标:

2014-06-05 10:29:27.377 dicttest[4863:60b] (
    {
    messages =         {
        alt = alt1;
        lat = lat1;
        long = long1;
    };
},
    {
    messages =         {
        alt = alt2;
        lat = lat2;
        long = long2;
    };
}

) 2014-06-05 10:29:27.386 字典测试[4863:60b] 2

如果我将字典直接添加到数组中(而不是将字典添加到消息中,然后将其添加到数组中),那么我会得到我正在寻找的输出。有人可以准确地向我解释我做错了什么吗?

【问题讨论】:

  • 经典新手错误——您添加了两次相同的字典,而不是创建两个不同的字典。当您将字典添加到数组(反之亦然或任何其他组合)时,您只需将 指针 添加到数组(您不会将字典物理复制到数组中),所以如果您随后更改字典这些更改将通过数组指针看到。

标签: ios objective-c nsmutablearray nsmutabledictionary


【解决方案1】:

它看起来像你想要的:

一个数组

At index 0: 
  A dictionary with a single key "messages"
    A dictionary with keys "alt", "lat", and "long"
At index 1:
  A dictionary with a single key "messages"
    A dictionary with keys "alt", "lat", and "long"

第二个数组条目中的数据应该使用相同的键,但数据不同。正如其他人指出的那样,您的错误是使用单个字典“字典”

当您将对象添加到字典或数组等集合时,该集合包含指向该对象的指针,而不是该对象的副本。如果您将同一个对象多次添加到集合中,则您有 2 个指向同一个对象的指针,而不是 2 个唯一对象。

当您将“字典”对象添加到结构中、对其进行更改并再次添加时,您不会获得预期的结果,因为结构中的两个条目都指向一个字典。当您更改值时,这两个地方都会发生变化。

“消息”字典也是如此。您还需要其中的 2 个。

通过添加新词典、dictionary2 和 messages2 来修复您的代码:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *dictionary2 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *messages = [[NSMutableDictionary alloc] init];
NSMutableDictionary *messages2 = [[NSMutableDictionary alloc] init];

NSMutableArray *array = [[NSMutableArray alloc] init];

[dictionary setValue:@"lat1" forKey:@"lat"];
[dictionary setValue:@"long1" forKey:@"long"];
[dictionary setValue:@"alt1" forKey:@"alt"];

[messages setObject:dictionary forKey:@"messages"];

[array addObject:messages];

[dictionary2 setValue:@"lat2" forKey:@"lat"];
[dictionary2 setValue:@"long2" forKey:@"long"];
[dictionary2 setValue:@"alt2" forKey:@"alt"];

[messages2 setObject: dictionary2 forKey:@"messages"];

[array addObject: messages2];


NSLog(@"%@",array);
NSLog(@"%lu",(unsigned long)[array count]);

您也可以考虑使用对象字面量语法,例如:

dictionary[@"lat"] = @"lat1";
dictionary[@"long"] = @"long1";
dictionary[@"alt"] = @"alt1";

messages[@"messages"] = 字典;

如果你不需要整个事情都是可变的,你甚至可以用一行来做所有事情:

NSMutableArray *array = [
  @[
    @{@"messages": @{@"lat": @"lat1", @"long": @"long1", @"alt": @"alt1"}},
    @{@"messages": @{@"lat": @"lat2", @"long": @"long2", @"alt": @"alt2"}}
  ];

或者让它可变:

NSMutableArray *array = [
  @[
    [@{@"messages":
      [@{@"lat": @"lat1", @"long": @"long1", @"alt": @"alt1"} mutableCopy]} mutableCopy],
    [@{@"messages":
      [@{@"lat": @"lat2", @"long": @"long2", @"alt": @"alt2"} mutableCopy]} mutableCopy]
                         ] mutableCopy];

编辑:要动态添加内容,您可以使用如下方法:(假设数组是实例变量)

- (void) addMessageWithLat: (NSString *) latString 
  long: (NSString *) longString
  alt: (NSString *) altString;
{
  NSMutableDictionary *messages = [[NSMutableDictionary alloc] init];
  NSDictonary *contents = 
    [@{@"lat": latString, 
      @"long": longString, 
      @"alt": altString} 
    mutableCopy];
  messages[@"messages"] = contents;
  [array addObject: messages];
}

【讨论】:

  • 邓肯,谢谢你的解释。这很有帮助。不知道我是应该在这里问这个问题还是打开一个新线程,但她还是去了:) 以上是相当静态的。我的意思是我在示例中使用的值是硬编码的。在我的应用程序中,它们不会。并且消息字典的数量不会是静态的。如何在需要时向数组动态添加字典?
  • 查看我的编辑。我添加了一种方法,可以让您向数组中添加一个条目。顺便说一句,如果您要添加的是纬度、经度和海拔高度,那么您最好使用 NSNumber 对象而不是 NSStrings 来获取这些值。 NSNumber 可以包含双精度浮点值而不会丢失精度,其中字符串会导致精度丢失。
  • 再次感谢您的帮助。
【解决方案2】:

问题是您正在同一对象引用中添加新值。所以新的价值将取代旧的价值。只需在 [dictionary setValue:@"lat2" forKey:@"lat"]; 之前添加这一行即可;

dictionary = [NSMutableDictionary alloc]init];

和 [messages setObject:dictionary forKey:@"messages"] 的第二个实例之前的这一行;

messages = [[NSMutableDictionary alloc] init];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多