Osp*_*pho 1 iphone enumeration objective-c nsmutablearray nsarray
嘿伙计们,我想知道如何在数组中找到特定类型对象的对象数.例如,我在随机位置的NSMutableArray中有6个'云',我在这个NSMutableArray中也有4个'龙'.
我怎样才能收集整数6?
我正在思考以下问题:
int z = [[SomeClass *clouds in _somearray] count];
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.Thnx,奥利弗.
小智 6
另一种方法是使用块:
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]);
Run Code Online (Sandbox Code Playgroud)
int result = 0;
for (NSObject *object in _somearray) {
if ([object isKindOfClass:[SomeClass class]])
result++;
}
Run Code Online (Sandbox Code Playgroud)
result 是你要找的伯爵
| 归档时间: |
|
| 查看次数: |
455 次 |
| 最近记录: |