我们说我的NSMutableArray如下所示。
(
{
blockNumber = 1;
boxId = 1;
boxNumber = 4536;
carId = 10;
carNumber = 10;
isDeleted = yes;
},
{
blockNumber = 2;
boxId = 2;
boxNumber = 4537;
carId = 10;
carNumber = 10;
isDeleted = yes;
},
{
blockNumber = 3;
boxId = 3;
boxNumber = 4538;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 4;
boxId = 4;
boxNumber = 4539;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 5;
boxId = 5;
boxNumber = 4540;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 6;
boxId = 6;
boxNumber = 4541;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 7;
boxId = 7;
boxNumber = 4542;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 8;
boxId = 8;
boxNumber = 4543;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 9;
boxId = 9;
boxNumber = 4544;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 10;
boxId = 10;
boxNumber = 4545;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 11;
boxId = 11;
boxNumber = 4546;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 12;
boxId = 12;
boxNumber = 4547;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 13;
boxId = 13;
boxNumber = 4548;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 14;
boxId = 14;
boxNumber = 4549;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 15;
boxId = 15;
boxNumber = 4550;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 16;
boxId = 16;
boxNumber = 4551;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 17;
boxId = 17;
boxNumber = 4552;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 18;
boxId = 18;
boxNumber = 4553;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 19;
boxId = 19;
boxNumber = 4554;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 20;
boxId = 20;
boxNumber = 4555;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 21;
boxId = 21;
boxNumber = 4556;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 22;
boxId = 22;
boxNumber = 4557;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 23;
boxId = 23;
boxNumber = 4558;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 24;
boxId = 24;
boxNumber = 4559;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 25;
boxId = 25;
boxNumber = 4560;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 26;
boxId = 26;
boxNumber = 4561;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 27;
boxId = 27;
boxNumber = 4562;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 28;
boxId = 28;
boxNumber = 4563;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 29;
boxId = 29;
boxNumber = 4564;
carId = 10;
carNumber = 10;
isDeleted = no;
},
{
blockNumber = 30;
boxId = 30;
boxNumber = 4565;
carId = 10;
carNumber = 10;
isDeleted = yes;
}
)
我想要的是我的号码isDeleted = yes
。
在我的情况下,它是3。
我知道我可以通过下面的循环来做到这一点。
for (NSMutableDictionary* aDict in carBoxesArray) {
if ([aDict[@"isDeleted"] isEqualToString:@"yes"]) {
findTotalYes++;
}
}
然而,我正在寻找任何其他方式(可能是一个班轮),它会给我我想要的东西。
特别是我不想使用循环(因为我可以在数组中有更多的记录,这需要花费一些时间进行迭代)
答案 0 :(得分:4)
您可以使用NSPredicate
NSArray *totalYes = [carBoxesArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(isDeleted == %@)", @"yes"]];
NSLog(@"Found Total YES: %d",totalYes.count);
答案 1 :(得分:0)
您可以在后台线程中使用循环并在主线程中使用一些加载器。在这种情况下,当您进行循环时,显示不会冻结。
答案 2 :(得分:0)
您也可以使用NSCountedSet
:
NSCountedSet *countset = [NSCountedSet setWithArray:[carBoxesArray valueForKey:@"isDeleted"]];
NSInteger count = [countset countForObject:@"yes"];
当你要求一个班轮时,将上面的两行合并为一个:
NSInteger count = [[NSCountedSet setWithArray:[carBoxesArray valueForKey:@"isDeleted"]] countForObject:@"yes"];
答案 3 :(得分:0)
你也可以使用枚举,
如下所示
__block int count = 0;
[carBoxesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([[obj objectForKey:@"isDeleted"] isEqualToString:@"yes"])
count++;
}];
NSLog(@"%d",count);