gpt4 book ai didi

iphone - 返回 objectForKey 的数量

转载 作者:行者123 更新时间:2023-11-28 19:12:12 26 4
gpt4 key购买 nike

我有一部分代码返回 numberOfRowsInSection。

代码

for (NSDictionary *consoleDictionary in [self arrayFromJSON]) {
if ([[consoleDictionary objectForKey:@"model"] isEqualToString:@"PlayStation 3"]) {
NSLog(@"%@", consoleDictionary);
}
}

输出

2013-02-03 22:37:08.468 PageControl01[5782:c07] {
console = PlayStation;
game = "007 Legends";
id = 1;
model = "PlayStation 3";
publisher = "Electronic Arts";
}
2013-02-03 22:37:08.478 PageControl01[5782:c07] {
console = PlayStation;
game = "Ace Combat: Assault Horizon";
id = 2;
model = "PlayStation 3";
publisher = Namco;
}

这显然是正确的,因为它记录了所有 "PlayStation 3" 模型。但是,这不是我需要的。我想记录 "PlayStation 3" 的数量。所以我稍微调整了代码,然后是:

for (NSDictionary *consoleDictionary in [self arrayFromJSON]) {
if ([[consoleDictionary objectForKey:@"model"] isEqualToString:@"PlayStation 3"]) {
NSLog(@"%d", [consoleDictionary count]);
}
}

输出

2013-02-03 22:39:43.605 PageControl01[5816:c07] 5
2013-02-03 22:39:43.605 PageControl01[5816:c07] 5

这个很近,但又很近。它不应记录数字 5,而应记录数字 2,因为只有 2 "PlayStation 3" .

请帮忙。

最佳答案

您不需要明确地遍历数组。

NSIndexSet *is = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:@"model"] isEqualToString:@"PlayStation 3"];
}];
int numOfPS3s = is.count;

关于iphone - 返回 objectForKey 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14673960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com