尝试将NSMutableDictionary添加到NSMutableArray

时间:2014-06-05 18:58:29

标签: ios objective-c nsmutablearray nsmutabledictionary

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

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

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 dicttest [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 dicttest [4863:60b] 2

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

2 个答案:

答案 0 :(得分:0)

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

dictionary = [NSMutableDictionary alloc]init];

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

messages = [[NSMutableDictionary alloc] init];

答案 1 :(得分:0)

在我看来,你想要:

数组

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个唯一对象。

当你添加"字典"对象,您的结构,更改它,并再次添加它,您没有得到您期望的结果,因为您的结构中的两个条目都指向一个字典。更改值时,它会在两个位置都发生变化。

同样适用于您的"消息"字典。你也需要其中的两个。

通过添加新词典,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";

消息[@"消息"] =字典;

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

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];
}