我有来自服务器的数据,例如
名称 地址 电话
有没有办法让我创建一个像以下的NSDictionary:
(
name=a_name,
address=a_address,
telephone=a_telephone
),
(
name=b_name,
address=b_address,
telephone=b_telephone
)
然后,我将这个字典插入另一个字典。
所以基本上我会有一本下面的词典;
(
User=me,
Details
(
name=a_name,
address=a_address,
telephone=a_telephone
),
(
name=b_name,
address=b_address,
telephone=b_telephone
)
)
我对数组和字典之间的使用感到困惑。
我有ff:code:
[test addObject:dictionary];
test是ns可变数组
这是一个数组,但是,我觉得如何将它添加到字典中很奇怪,因为没有方法可以添加数组值和只是字符串的键。
所以问题可能会拖延到如何将数组对象添加到NSDictionary。
感谢。
答案 0 :(得分:2)
试试这个:
NSMutableArray *arr = [[NSMutableArray alloc]init];
while (response) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"name_i" forKey:@"name"];
[dict setObject:@"address_i" forKey:@"address"];
[dict setObject:@"telephone_i" forKey:@"telephone"];
[arr addObject:dict];
}
还可以将Array添加到Dictionary
中NSMutableDictionary *totalDict = [[NSMutableDictionary alloc]init];
[totalDict setObject:@"Me" forKey:@"User"];
[totalDict setObject:arr forKey:@"Details"];
答案 1 :(得分:0)
NSArray *dictionary = @[
@{
@"Source_lat": @"28.6287",
@"Source_lon": @"77.3208",
@"Dest_lat": @"28.628454",
@"Dest_lon": @"77.376945",
@"S_address": @"Vaishali,Delhi",
},
@{
@"Source_lat": @"28.628454",
@"Source_lon": @"77.376945",
@"Dest_lat": @"28.5529",
@"Dest_lon": @"77.3367",
@"S_address": @"Noida Sec 63",
},
@{
@"Source_lat": @"28.5529",
@"Source_lon": @"77.3367",
@"Dest_lat": @"28.6276",
@"Dest_lon": @"77.2784",
@"S_address": @"Noida Sec 44",
},
@{
@"Source_lat": @"28.6276",
@"Source_lon": @"77.2784",
@"Dest_lat": @"28.6287",
@"Dest_lon": @"77.3208",
@"S_address": @"Laxmi Nagar,Delhi",
},
];
可能对你有帮助
答案 2 :(得分:-1)
您应创建NSMutableDictionary
,然后使用setObject:ForKey
添加NSArray
。
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:test forKey:@"test"];
答案 3 :(得分:-1)
你可以有一个字典数组。
NSMutableArray *arr = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
while (response) {
[dict setObject:@"name_i" forKey:@"name"];
[dict setObject:@"address_i" forKey:@"address"];
[dict setObject:@"telephone_i" forKey:@"telephone"];
[arr addObject:dict];
[dict removeAllObjects];
}
如果您愿意,也可以使用词典。
NSMutableDictionary *totalDict = [[NSMutableDictionary alloc]init];
[totalDict setObject:dict forKey:@"i"];