views:

1179

answers:

3

Dear all, here's a very basic question, that I'm sure you will be able to answer quickly. Please don't laugh at my ignorance.

I have a string, that I want to compare to an array of strings. Only if the string is not part of the array, I want to perform an operation. I tried the following code, that doesn't work. I do understand why, but I just can't think of a way to do it correctly.

Please help me out of my misery.

Thanks in advance

Sjakelien

-(void) findRedundant: (NSString *) aString {
#define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil]   
    NSUInteger f;
    for (f = 0; f < [ALPHA_ARRAY count]; f++) 
    {
     NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f];
     if ([aString isEqualToString:stringFromArray]) {
      // do nothing

     } else {
      //do something
     }

    }

}



[self findRedundant:@"D"];
+4  A: 

I'm not sure why your code above isn't working, but have you tried:

if ([ALPHA_ARRAY containsObject:aString])

which will return YES if aString exists otherwise NO

KiwiBastard
Thank you so much. It was the "containsObject" that I didn't know the existence of. I will review the rest of my code to get rid of lots of cumbersome 'for' loops!
Sjakelien
+1  A: 

The #define looks odd to me. I think that each time you use ALPHA_ARRAY a different NSArray instance will be created. It would be cleaner to use the containsObject: method on NSArray:

NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
if (![alphaArray containsObject:aString]) {
    // do something
}
teabot
+1  A: 

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
    }
}
Peter N Lewis
Thanks Peter. While the useful answer was already given by KiwiBastard, your thoroughness actually taught me more than I was asking for. In my defense: The Alpha Array I kind of improvised to make this example understandable. In my real code I just use an existing array.
Sjakelien