I made some codes to count every parenthesis from some string,
Is there any simpler way?
NSCharacterSet *parenthesis = [NSCharacterSet characterSetWithCharactersInString:@"()"];
parenthesis = [parenthesis invertedSet];
NSArray *arr = [someString componentsSeparatedByCharactersInSet:parenthesis];
NSInteger count = 0;
for(int i=0;i<[arr count];i++) {
if([[arr objectAtIndex:i] isEqualToString:@")"])
count++;
}
NSLog(@"count is %d", count);
Since you need to walk through all characters in the string anyway, why not simply
This uses fewer classes, much easier to read, and uses less CPU to perform the calculations.