0

I am using code to check if a word is in my array, if it is I want it to submit it and I have the code for it. If it isn't I want it to pop up a screen. now this all works, the only thing is the screen pops up 2 times, because there are 2 words in my array. here is the code to explain it a little better.

NSArray *searchContacts = [NSArray arrayWithObjects:
                           @"CADEAU",
                           @"KADERZ",nil];
NSString *myContact = labelsText.text;

for (NSString *contact in searchContacts) {
    if ([contact isEqualToString:myContact]) {

this is where I put in my words, CADEAU & KADERZ in this case. When I put one of these words into labelsText.text it does exactly what I want. but for the else statement if the labels text.text word is not CADEAU or KADERZ, it pop ups a screen:

else {
 UIAlertView *alert = [[UIAlertView alloc]

This screen will pup up 2 times now, so i'll have to press dismiss 2 times, how would I fix this to just have to press dismiss one time no mather how many words are in the array?

4 Answers 4

5

It would be more efficient to use an NSSet, but even if you use an NSArray, you can simply call containsObject: instead of looping through the collection yourself.

if (![searchContacts containsObject:myContact]) {
   //show alert...
}
Sign up to request clarification or add additional context in comments.

4 Comments

oke is there any difference? I need to put in 12.5k words so any important thing I need to worry?
If all you want to do is determine whether a name is present in your array, then this is a good way to do it.
If really all you want to do is to check whether some name is on your list of valid names and the order of that list is not important, you should use an NSSet or NSMutableSet instead of an NSArray. It is much faster for membership testing.
For a large collection, a set is definitely going to be faster.
3

Put a break; after the code showing your alert:

for (NSString *contact in searchContacts) {
    if ([contact isEqualToString:myContact]) {
        // do something
    } else { 
        // show screen
        break;
    }
}

This will 'break' out of the loop.

1 Comment

Hold on, are you sure this is what you want? If you put in KADERZ, it's going to show the screen and fail, which I don't think is what you intended. If you really want to do it this way, see Matt's answer below. But omz's answer (using NSSet) is probably the best.
0

I think you want something like this:

    BOOL contactFound = NO;
    for (NSString *contact in array)
    {
        if ([contact isEqualToString:myContact])
        {
            contactFound = YES;
            break;
        }
    }

    if (!contactFound)
        UIAlertView *alert = [[UIAlertView alloc]...

Comments

0

Use a break after your UIAlertView.

For example:

for (NSString *contact in searchContacts) {
        if ([contact isEqualToString:myContact]) {
            //do what you want to do      
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert show];
            break; //leave for()
        }
    }

Or use that:

searchContacts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([contact isEqualToString:myContact]) {
        //do what you want to do      
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] init];
        [alert show];

        *stop = YES; //stop enumeration
    }
}

1 Comment

The is the same as the accepted answer, and it's wrong for the same reason: It will only accept the first value in searchContacts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.