我有一个包含4个字典的数组,其键为@“首选项”,如下所示
(
{
preference = Nose;
},
{
preference = "Heart rate";
},
{
preference = Glucose;
},
{
preference = Food;
}
)
现在我想为这些字典值检索一个数组,比如“
(
Nose, Heart rate, Glucose, Food
)
我怎么得到它...提前致谢
答案 0 :(得分:23)
单行:
NSArray *resultingArray = [arrayOfDictionaries valueForKeyPath:@"preference"];
答案 1 :(得分:9)
试一试:
NSArray *result = [dictionaryObject valueForKeyPath:@"preference"];
它会解决你的问题
答案 2 :(得分:2)
做这样的事情:
NSMutableArray *collectedValues = [NSMutableArray arrayWithCapacity:array.count];
for (NSDictionary *dict in array) {
NSString *value = [dict objectForKey:@"preference"];
if (value) {
[collectedValues addObject:value];
}
}
答案 3 :(得分:2)
尝试使用此代码:
myArray是你的第一个阵列
mySecondArray是你最后拥有的数组
NSMutableArray *mySecondArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [myArray count] ; i++)
{
NSDictionary *tempDict = [myArray objectForIndex:i];
[mySecondArray addObject:[tempDict objectForKey@"preference"]];
}