0

这个问题听起来很奇怪,但我得到了一系列字典作为解析结果。

像这样的东西:

parsed content: (
        {
        "name" = "John";
        "lastname" = "Doe";
        "foo" = "bar";
    }

对于创建值数组的最佳方法有什么建议?

4

3 回答 3

3

像这样?

- (void)flattenDictionary:(NSDictionary *)d intoKeys:(NSMutableArray *)keys andValues:(NSMutableArray *)values {

    for (id key in [d allKeys]) {
        [keys addObject:key];
        [values addObject:[d valueForKey:key]];
    }
}

- (void)flattenDictionaries:(NSArray *)arrayOfDictionaries {

    NSMutableArray *keys = [NSMutableArray array];
    NSMutableArray *values = [NSMutableArray array];

    for (NSDictionary *d in arrayOfDictionaries) {
        [self flattenDictionary intoKeys:keys andValues:values];
    }

    NSLog(@"now we have these values %@", values);
    NSLog(@"corresponding to these keys %@", keys);
}
于 2012-09-09T19:27:28.597 回答
2

您可以通过以下方式获取值:

NSArray *values = dictionary.allValues;

或者,循环遍历它:

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
    NSLog(@"%@ = %@", key, object);
}];
于 2012-09-09T18:51:29.243 回答
1

要通过它们循环并创建一个数组。

于 2012-09-09T18:45:37.903 回答