Check if NSMutableArray contains text from textfield

I want to check if the text inserted in a textfield is also in my NSMutableArray. So let's say my NSMutableArray has these objects: "Hey, Hello, No, Yes". Then when a user enters the text: "Hello" i want there to appear a UIAlertView. I now have the following:

for (int slt = 0; slt < [zouten count]; slt++) {
        if (zout.text = [zouten objectAtIndex:slt]) {

            alert = [[UIAlertView alloc]initWithTitle:@"Goedzo!" message:[NSString stringWithFormat:@"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        }
    }

    [alert show];

But somehow the message appears with every word. what am i doing wrong?

Answers


When you compare like this:

if (zout.text = [zouten objectAtIndex:slt])

you are actually assigning instead of comparing so it will be TRUE always.Therefore instead of using =, you should compare like this:

if ([zout.text isEqualToString:[zouten objectAtIndex:slt]])

Your code should be:

for (int slt = 0; slt < [zouten count]; slt++) {
        if ([zout.text isEqualToString:[zouten objectAtIndex:slt]]) {
            alert = [[UIAlertView alloc]initWithTitle:@"Goedzo!" message:[NSString stringWithFormat:@"Je hebt een zout gevonden"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
           [alert show];
           [alert release];
            break;
        }
    }

You can use isEqualToString method to compare strings.

if([zout.text isEqualToString:[zouten objectAtIndex:slt]])

Alternatively you could make your code more concise by converting to a set and checking for the entry, e.g.

NSSet *set = [NSSet setWithArray: zouten];
if([set containsObject:zout.text]) {
   ...
}

Need Your Help

Download office document without the web server trying to render it

c# httpwebrequest content-type mime-types

I'm trying to download an InfoPath template that's hosted on SharePoint. If I hit the url in internet explorer it asks me where to save it and I get the correct file on my disk. If I try to do this

Animate background-position from outside of element to inside

jquery html background jquery-animate

I'm having issues animating a background image from OUTSIDE of an element to the INSIDE.