简体   繁体   English

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

[英]How get a value from NSDictionary and move it to an NSArray

I'm sure this question has been asked dozens of times on here, but I can't seem to find anything even close to what I need. 我敢肯定,这个问题已经在这里问过数十遍了,但是我似乎找不到任何与我所需要的接近的东西。 I'm having a little bit of a problem getting this right. 我在解决此问题时遇到了一些问题。 I need to get a value out of an NSDictionary and place it into an 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",
                              },
              };

In my example dictionary above, I need to get the key First under the 3rd layer dictionary and load it into an array. 在上面的示例字典中,我需要在第三层字典下获取键First并将其加载到数组中。 The dictionaries marked ID and Person will be assigned separate ID numbers will be the only keys that will not be static. 标记有IDPerson的词典将被分配单独的ID号,这是唯一不是静态的键。 Thanks in advance. 提前致谢。

For this you can go loop through the dictionary and first idKey then access that dictionary and access its detail by loop through it. 为此,您可以循环浏览字典和第一个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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM