Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing a string to an array in objective-C

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"];
like image 365
Sjakelien Avatar asked Jun 18 '09 10:06

Sjakelien


People also ask

Can we compare string with array?

Compare text in character arrays and string arrays in different ways. You can compare string arrays and character vectors with relational operators and with the strcmp function. You can sort string arrays using the sort function, just as you would sort arrays of any other type.

How do you check if two strings are equal in Objective C?

To compare two strings equality, use isEqualToString: . BOOL result = [firstString isEqualToString:secondString]; To compare with the empty string ( @"" ), better use length .

What does string Compare do?

compare() is a public member function of string class. It compares the value of the string object (or a substring) to the sequence of characters specified by its arguments. The compare() can process more than one argument for each string so that one can specify a substring by its index and by its length.


2 Answers

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
    }
}
like image 138
Peter N Lewis Avatar answered Oct 05 '22 07:10

Peter N Lewis


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

like image 29
JamesSugrue Avatar answered Oct 05 '22 06:10

JamesSugrue