-1

("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")

我想将此字符串数组以键值形式排列,如“$”中的哈希映射,并选择像 Indore 这样的值UIPicker并导致其键如 1。请帮助我......但请更合理,因为这个字符串是从服务器获取的我不能在我的代码中写自用键是 1,值是 Indore。

4

2 回答 2

0

ANSDictionary可以用作哈希映射,语法非常简单。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]];
}
于 2013-10-15T11:34:53.557 回答
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);
于 2013-10-15T11:36:15.890 回答