Iphone 从NSMutableArray收集特定对象的计数
嘿,小伙子们,姑娘们, 我想知道如何在数组中找到特定类型对象的对象计数。 例如,我在NSMutableArray中的随机位置有6个“云”,在这个NSMutableArray中还有4个“龙” 如何收集整数6 我的想法大致如下:Iphone 从NSMutableArray收集特定对象的计数,iphone,objective-c,nsmutablearray,nsarray,enumeration,Iphone,Objective C,Nsmutablearray,Nsarray,Enumeration,嘿,小伙子们,姑娘们, 我想知道如何在数组中找到特定类型对象的对象计数。 例如,我在NSMutableArray中的随机位置有6个“云”,在这个NSMutableArray中还有4个“龙” 如何收集整数6 我的想法大致如下: int z = [[SomeClass *clouds in _somearray] count]; 如蒙协助,将不胜感激。 Thnx, 奥利弗 result是您要查找的计数 result是要查找的计数如果要查找对象的特定实例出现的次数,可以执行以下操作: NSCount
int z = [[SomeClass *clouds in _somearray] count];
如蒙协助,将不胜感激。
Thnx,
奥利弗
result
是您要查找的计数
result
是要查找的计数如果要查找对象的特定实例出现的次数,可以执行以下操作:
NSCountedSet *counts = [NSCountedSet setWithArray:myArrayOfObjects];
NSUInteger count = [counts countForObject:myObject];
否则,您只需手动循环数组并计数。如果要查找对象的特定实例出现的次数,可以执行以下操作:
NSCountedSet *counts = [NSCountedSet setWithArray:myArrayOfObjects];
NSUInteger count = [counts countForObject:myObject];
否则,您只需手动循环数组并计数。另一种方法是使用块:
Class cloadClass = NSClassFromString(@"Cloud");
NSArray *a = /* you array with clouds and dragons */;
NSIndexSet *clouds = [a indexesOfObjectsPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:cloadClass];
}];
// now we can count clouds
NSLog(@"%d", [clouds count]);
// but also we now can return our clouds immediately and
NSLog(@"%@", [a objectsAtIndexes:clouds]);
另一种方法是使用块:
Class cloadClass = NSClassFromString(@"Cloud");
NSArray *a = /* you array with clouds and dragons */;
NSIndexSet *clouds = [a indexesOfObjectsPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:cloadClass];
}];
// now we can count clouds
NSLog(@"%d", [clouds count]);
// but also we now can return our clouds immediately and
NSLog(@"%@", [a objectsAtIndexes:clouds]);