0

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

   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";
  }

例如:一个数组中的checka0-checka14,这里的问题是checka2和checka6不可用,我是xcode中的新蜜蜂,请帮我找回

4

3 回答 3

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]];
  }
 ...
}
于 2013-06-11T08:51:38.340 回答
0

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

[contryArray valueForKey:@"checka0"];

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

于 2013-06-11T08:52:13.763 回答
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]];

    }
}
于 2013-06-11T08:57:57.810 回答