("1$Indore","2$Lyallpur","3$Sagan","4$Bhopal","5$Reva","6$Santa","7$Dar","8$Chinaware","9$Gwalior","10$Jain","11$Morena","12$West Nimar","13$Chatterer")
我希望这个字符串数组以键值形式排列,例如来自“$”的哈希映射,并选择像UIPicker
中的Indore一样的值,并得到它的键,如1. pl z帮助我...但是plz更合理的是因为从服务器获取的字符串我不能在我的代码键中自写使用是1并且值是Indore。
答案 0 :(得分:0)
NSDictionary
可以用作哈希映射,语法非常简单。使用NSDictionary
声明@{ }
文字,并且可以包含以逗号分隔的键值对列表。这些键值对声明为key: value
。
例如:
NSDictionary *dict = @{ @"1": @"Indore", @"2":@"Lyallpur", ... };
编辑:要将原始问题中的字符串数组(我们称之为 values )转换为NSDictionary,您可以这样做:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *value in values) {
NSArray *keyValuePair = [value componentsSeparatedByString:@"$"];
[dict setObject:keyValuePair[1] forKey:keyValuePair[0]];
}
答案 1 :(得分:0)
使用下面的NSDictionary: -
//Add how many elements you want to add on your array
NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"$Indore", nil] forKeys:[NSArray arrayWithObjects:@"1", nil]];
NSString *yourStr=[yourDict objectForKey:@"1"];
NSLog(@"%@",yourStr);