English 中文(简体)
2. 如果在阵列中字数有效,则检查
原标题:Checking if word is valid in array

我正在使用守则来检查一下,如果我想要提交的话,如果我有这个词,那么我就会检查一下。 如果说不了,我就希望它登上屏幕。 现在,所有这一切都奏效,唯一一件事是屏幕上扬2次,因为我的阵列有2个字。 在这里,《法典》对它的解释稍有改进。

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]

这一屏幕现在将翻两番,这样一来,两度不得不施压,我为什么要这样做,仅仅不得不迫使人们一度不解阵列中有多少字?

最佳回答

Put a break; after the code showing your alert:

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

这将打破僵局。

问题回答

使用<条码>NSSet更为有效,但即使你使用<条码>NSArray/code>,你也可简单地打电话<条码>。 目标:,而不是通过收集雨水进行处理。

if (![searchContacts containsObject:myContact]) {
   //show alert...
}

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]...

Use a break after your UIAlertView.

例如:

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()
        }
    }

或者使用:

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
    }
}




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签