字典值检索

时间:2013-06-11 08:46:07

标签: iphone ios nsarray nsdictionary

在我的应用程序中,字典值包含以下内容。如何检索每个内容并将其存储为新数组

   contryArray(
    {
    checka0 = Thailand;
    checka1 = Brazil;
    checka10 = Marocco;
    checka11 = Thailand;
    checka12 = Jordan;
    checka13 = Colombia;
    checka14 = Kuwait;
    checka3 = Mexico;
    checka4 = "Saoudi Arabia";
    checka5 = Chili;
    checka7 = Australia;
    checka8 = Malta;
    checka9 = "South Africa";
    checkb0 = havana;
    checkb1 = Santos;
    checkb10 = Casablanca;
    checkb11 = Bangkok;
    checkb12 = Aqaba;
    checkb13 = Havana;
    checkb14 = Shuwaikh;
    checkb3 = Veracruz;
    checkb4 = Jeddah;
    checkb5 = "San Antonio";
    checkb7 = Maersk;
    checkb8 = Maraxklokk;
    checkb9 = Durban;
    checkc0 = 1;
    checkc1 = "0.7";
    checkc10 = 1;
    checkc11 = "1.2";
    checkc12 = 1;
    checkc13 = "1.4";
    checkc14 = 1;
    checkc3 = "0.9";
    checkc4 = "0.8";
    checkc5 = "0.9";
    checkc7 = "2.7";
    checkc8 = "0.8";
    checkc9 = "0.9";
  }

对于ex:checka0-checka14在一个数组中,这里的问题是checka2和checka6不可用,我是新的蜜蜂在xcode,请帮我检索

3 个答案:

答案 0 :(得分:2)

你的问题不明确。可能你需要获取字典中的所有键并将每个项目与其中的特定单词分开(checka / checkb / checkc)。

如果是这样的话, 您将使用以下方式获取所有密钥:

NSArray *dictKeys = [yourDictionary allKeys];

您可以通过多种方式实现此目标,其中之一是:

将其存储在同一个数组中:

for (NSString *key in [yourDictionary allKeys])
{
    [yourArray addObject:[yourDictionary objectForKey:key]];
}

将其存储在单独的数组中:

for (NSString *key in [yourDictionary allKeys])
{
  if([key rangeOfString:@"checka"].location != NSNotFound)
  {
    //add checka to first array
    [yourFirstArray addObject:[yourDictionary objectForKey:key]];
  }
  else if([key rangeOfString:@"checkb"].location != NSNotFound)
  {
    //add checkb to second array
    [yourSecondArray addObject:[yourDictionary objectForKey:key]];
  }
 ...
}

答案 1 :(得分:0)

如果contryArray是您的字典,则按以下方式访问

[contryArray valueForKey:@"checka0"];

继续将此值添加到另一个数组

答案 2 :(得分:0)

您可以使用以下代码:

- (void)filterDictionary:(NSDictionary *)dict 
{
    NSArray *allKeys = [dict allKeys];
    NSMutableArray *allValues = [[NSMutableArray alloc]init];
    for(id key in allKeys)
    {
        id value = [dict valueForKey:key];

        [allValues addObject:[value stringValue]];

    }
}