如何从NSDictionary获取值并将其移至NSArray

时间:2017-02-16 04:30:56

标签: ios objective-c dictionary nsarray

我确信这个问题在这里被问过几十次,但我似乎找不到任何与我需要的东西相近的东西。我有一个问题,这是正确的。我需要从NSDictionary中获取值并将其放入NSArray

    NSDictionary *dictionary = @{@"ID1":@{
                                   @"mainPhone": @"<number>",
                                   @"Address": @"<address>",
                                   @"City": @"<Tulsa, OK>",
                                   @"Date": @"1-1-2017",
                                   @"Person1": @{
                                               @"First": @"<FirstName>",
                                               @"Last":  @"<LastName>",
                                               @"Phone": @"<number>",
                                               @"Email": @"email@gmail.com",
                                               @"Birth": @"<date>",
                                               @"ID":    @"12345",
                                   },
                                   @"Person2": @{
                                               @"First": @"<FirstName>",
                                               @"Last":  @"<LastName>",
                                               @"Phone": @"<number>",
                                               @"Email": @"email@gmail.com",
                                               @"Birth": @"<date>",
                                               @"ID":    @"12345",
                              },
                              @"ID2":@{
                                   @"mainPhone": @"<number>",
                                   @"Address": @"<address>",
                                   @"City": @"<Tulsa, OK>",
                                   @"Date": @"1-1-2017",
                                   @"Person3": @{
                                               @"First": @"<FirstName>",
                                               @"Last":  @"<LastName>",
                                               @"Phone": @"<number>",
                                               @"Email": @"email@gmail.com",
                                               @"Birth": @"<date>",
                                               @"ID":    @"12345",
                                   },
                                   @"Person4": @{
                                               @"First": @"<FirstName>",
                                               @"Last":  @"<LastName>",
                                               @"Phone": @"<number>",
                                               @"Email": @"email@gmail.com",
                                               @"Birth": @"<date>",
                                               @"ID":    @"12345",
                              },
              };

在上面的示例词典中,我需要在第3层字典下获取密钥First并将其加载到数组中。标记为IDPerson的词典将被分配单独的ID号,这将是唯一不会是静态的键。提前谢谢。

1 个答案:

答案 0 :(得分:0)

为此你可以循环遍历字典,然后第一个idKey然后访问该字典并通过循环访问它的详细信息。

for (NSString *key in dictionary) {
    NSDictionary *idDic = [dictionary objectForKey:key];
    for (NSString *idDicKey in idDic) {
        if ([[idDic objectForKey:idDicKey] isKindOfClass:[NSDictionary class]]) {
            NSString *firstName = [dictionary valueForKeyPath:[NSString stringWithFormat:@"%@.%@.First",key, idDicKey]];
            NSLog(@"%@",firstName);
        }
    }
}