Reputation: 1873
I have an array with inside strings of numbers only and letters only, i.e.:
"10.1", "15.7", "9.3", "RED", "7.7", "BLUE", "9.0"
the [array count] returns the numbers of all strings in the array (in this examples 7).
How can I get the count of the strings which contain numbers only?
Thanks,
Max
Upvotes: 0
Views: 144
Reputation: 8513
Using blocks, this creates a set of array indexes for all strings in the array that contain a decimal digit.
[[items indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
return (BOOL) [obj rangeOfCharacterFromSet: [NSCharacterSet decimalDigitCharacterSet]].length;
}] count]
Upvotes: 1
Reputation: 25632
In your example, all elements are strings, though I think you forgot the @ at the beginning.
Basically you need to iterate over the contents of the array and see if they match what you are looking for:
NSUInteger count = 0;
for (id object in array) {
if ([object .... ])
count++;
}
You might have a look at the new block-based api:
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate
and just look at the count of the returned value. With large arrays I'd prefer to iterate myself, though, if you don't have need for the index set apart from the count.
Upvotes: 1