在NSDictionary中获得价值

时间:2011-08-11 09:58:26

标签: objective-c ios nsdictionary

我希望从NSDictionary获得价值。我想根据Code键控制“Code”键,我会得到基准值。当我尝试不同的方法时,我收到错误消息。

错误消息

  

2011-08-11 12:45:21.549 AOK [6312:207] - [__ NSArrayM   getObjects:andKeys:]:无法识别的选择器发送到实例0x5e138b0   2011-08-11 12:45:21.550 AOK [6312:207] *由于终止应用程序   未捕获的异常'NSInvalidArgumentException',原因:' - [__ NSArrayM   getObjects:andKeys:]:发送到实例的无法识别的选择器   0x5e138b0'   * 首先调用堆栈:

的NSDictionary

(
        {
        Omschrijving = "ADDD";
        Ophaaldata =         {
            Datum =             (
                "2011-07-04",
                "2011-07-11",
                "2011-07-18",
                "2011-07-25"
            );
        };
    },
        {
        Omschrijving = "BBBB";
        Ophaaldata =         {
            Datum =             (
                "2011-07-05",
                "2011-07-12",
                "2011-07-19",
                "2011-07-26"
            );
        };
    },
        {
        Omschrijving = "KKKK";
        Ophaaldata =         {
            Datum =             (
                "2011-07-07",
                "2011-07-14",
                "2011-07-21",
                "2011-07-28"
            );
        };
    },
        {
        Omschrijving = "LLLLL";
        Ophaaldata =         {
            Datum =             (
                "2011-07-01",
                "2011-07-08",
                "2011-07-15",
                "2011-07-22",
                "2011-07-29"
            );
        };
    },
        {
        Omschrijving = "MMMMM";
        Ophaaldata =         {
            Datum =             (
                "2011-07-01",
                "2011-07-15",
                "2011-07-29"
            );
        };
    },
        {
        Code = POP;
        Omschrijving = "FFFF";
        Ophaaldata =         {
            Datum = "2011-07-12";
        };
    },
        {
        Code = 00;
        Omschrijving = "SSSS";
        Ophaaldata =         {
            Datum =             (
                "2011-07-14",
                "2011-07-27"
            );
        };
    },
        {
        Code = 01;
        Omschrijving = "CCCCC";
        Ophaaldata =         {
            Datum =             (
                "2011-07-06",
                "2011-07-20"
            );
        };
    }
)

3 个答案:

答案 0 :(得分:10)

你展示的是NSDiction的NSArray,每个都包含带有描述(Omschrijving)的NSString和带有一个键(Datum)的NSDictionary(Ophaaldata),这是一个日期数组。如果您将Objects:andKeys:发送给NSArray,它将无效。

以下是一些如何了解各个项目的示例:

NSArray          *dicts = ...; // wherever you get the array
  NSDictionary   *mijnDict = [dicts objectAtIndex: n];
    NSString     *omschrijving = [mijnDict objectForKey: @"Omschrijving"];
    NSDictionary *ophaaldata = [mijnDict objectForkey: @"Ophaaldata"];
      NSArray    *datum = [ophaaldata objectForkey: @"Datum"];
        NSString *eersteDatum = [datum objectAtIndex: 0];

这些只是您如何处理这些项目的示例。但这是您的输出显示的结构。无论如何,任何其他东西都是无用的,因为字典只能包含每个密钥一次。我把代码格式化了一点,所以你可以更好地看到结构。

答案 1 :(得分:2)

在响应期间记住行的值

{ .......... } indicates dictionary.
(............) indicates array.
Dictionary always have key with it like somename = "value in it", .......
Array has value seperated by comma like a , b , c .....
Now it is possible it may have array into dictionary like { somekey = ( a,b,c) } and vice versa

答案 2 :(得分:1)

reason: '-[__NSArrayM getObjects:andKeys:]: 

确定你有NSDictionary而不是NSArray吗?