Your code is working fine. Its awful code, but it works fine, // nothing is called for any section, and // calls something section for every mismatch in the array. I suspect the problem is that you expect that // there will be nothing section that will be executed once if there is no match, and // do something section to execute once if there is any match , what's wrong. 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 ) {
In addition, you clearly do not understand macros, and when you should and should not use them (as a rule, you should never use them, with very few exceptions). The macro in the text expression has been replaced with your code. This means that the creation and initialization of the array occurs every time , you use ALPHA_ARRAY. It's horrible.
Basically, never use #define again (except for constants) until you get a much deeper understanding of what you are doing. In this case, you will create an array as described by taebot:
NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
Further, if you are developing a modern platform (10.5 or iPhone), you can use Fast Enumeration, which is much easier and more understandable 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 ) {
And finally, you should read the NSArray and NSString documentation to find out what you can do for free, and then you will find methods like containsObject that KiwiBastard pointed out, and you can rewrite your procedure as:
-(void) findRedundant: (NSString *) aString { NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; if ( [alphaArray containsObject: aString] ) { // do found } else { // do not found } }