Search code examples
objective-cxcodecocoa-touchnsmutablearraynsarray

conditional nsarray count


I want a count on an array where a sub array meets a condition. I thought I could do this, but I can't.

NSLog(@"%d",[[_sections enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [[obj objectAtIndex:4] isEqualToString:@"1"];
        }] count]);

Solution

  • enumerateObjectsUsingBlock: doesn't return anything. I'd bet that code won't even compile (and, as your comment states, auto-completion doesn't work -- and it shouldn't).

    Use NSArray's indexesOfObjectsPassingTest: and take the count of the resulting NSIndexSet.

    Documented here.