如何将NSMutableArray转换为NSMutableDictionary

时间:2017-04-13 11:52:59

标签: ios objective-c nsmutablearray nsmutabledictionary

在目标c中如何将NSMutableArray转换为NSMutableDictionary我尝试使用此代码。但只有最后一个索引对象才添加到字典中。 我需要像这样的格式

ADDRESS =
{
    major = 604;
    minor = 37940;
    uuid = "xxxxxxx";
};
{
    major = 604;
    minor = 37940;
    uuid = "xxxxxxxxx";
};
 {
    major = 604;
    minor = 37940;
    uuid = "xxxxxxxxxx";
};

我尝试使用此代码

NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];

for (int j = 0; j <[self.beaconListArray count]; j ++)

{
    [dic1 setObject:[self.beaconListArray objectAtIndex:j] forKey:@"ADDRESS"];

}

3 个答案:

答案 0 :(得分:2)

尝试这样,因为你的字典是覆盖对象,你已经有数据的数组,你需要使用地址键创建字典

NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];

[dic1 setObject:self.beaconListArray forKey:@"ADDRESS"];

答案 1 :(得分:1)

使用此功能。

NSMutableDictionary * dic1 = [NSMutableDictionary new];
dic1[@"ADDRESS"] = self.beaconListArray;

答案 2 :(得分:0)

您可以尝试使用此代码  的字典

  NSMutableDictionary * dic1 = [NSMutableDictionary dictionary];

使用键

将字典添加到数组中
[dic1 setObject:self.beaconListArray forKey:@"ADDRESS"];

打印字典

 NSLog(@"dic1====: %@",dic1);