Your code appears to work fine. Its terrible code, but it works fine, the // do nothing section is called for any match and the // do something section is called for each mismatch in the array. I suspect the problem is that you are expecting the // do nothing section to be executed once if there is no match, and // do something section to be executed once if there is any match, which is not the case. You probably want:
-(void) findRedundant: (NSString *) aString {
#define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil]
BOOL found = NO;
NSUInteger f;
for (f = 0; f < [ALPHA_ARRAY count]; f++) {
NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f];
if ([aString isEqualToString:stringFromArray]) {
found = YES;
break;
}
}
if ( found ) {
// do found
} else {
// do not found
}
}
Also, you clearly do not understand macros and when you should and should not use them (generally, you should never use them, with very few exceptions). The macro is textually substitued in to your code. That means the array creation and initialization is happening every time you use ALPHA_ARRAY. This is terrible.
Basically, never use #define again (except for constants) until you have a much deeper grasp of what you're doing. In this case, you would create the array as taebot described:
NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
Next, if you are developing for a modern platform (10.5 or iPhone), you can use Fast Enumeration which is much easier and clearer to read:
-(void) findRedundant: (NSString *) aString {
NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
BOOL found = NO;
for ( NSString* stringFromArray in alphaArray ) {
if ([aString isEqualToString:stringFromArray]) {
found = YES;
break;
}
}
if ( found ) {
// do found
} else {
// do not found
}
}
And finally, you should go read through the documentation on NSArray and NSString to see what you can do for free, and then you'll find methods like containsObject that KiwiBastard pointed out, and you can rewrite your routine as:
-(void) findRedundant: (NSString *) aString {
NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
if ( [alphaArray containsObject: aString] ) {
// do found
} else {
// do not found
}
}