【问题标题】:Checking if two loops returns a value检查两个循环是否返回一个值
【发布时间】:2015-07-08 19:20:14
【问题描述】:

我想检查两个 for 循环是否返回匹配项,如果它们什么也没发生,但如果它们不执行,我希望打印一条消息,说明“找不到匹配项”之类的信息。像这样的:

if (loop1 == 0 && loop2 == 0) {
    NSLog(@"A match could not be found, please check your spelling");
};

因此,我的问题是,如何描述循环以便检查它们给出的值?

这是循环:

    for (SMADoc *aSearch in docs) {
        if ([search isEqualToString:[aSearch leadAuthour]]) {
            //Open the file that is represented by UrlToDoc for that specific object
            [[NSWorkspace sharedWorkspace] openFile:[aSearch urlToDoc]];
        }
    }
} else {
    //The string starts with a number and should be converted to an int and then the array of the numbers should be searched through
    int number = atoi(searchC);
    NSNumber *sNumber = [NSNumber numberWithInt:number];

    for (SMADoc *nSearch in docs) {
        if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
            [[NSWorkspace sharedWorkspace] openFile:[nSearch urlToDoc]];
        }
    }

}

提前致谢!

【问题讨论】:

  • 这真是令人困惑。您似乎错过了一些if 声明。 loop1loop2 是什么?您到底想在这里和哪里检查什么?
  • @rmaddy loop1loop2 只是为了描述我的想法,上面的代码不是完整的程序,只是我发现相关的部分。实际上,我自己设法解决了这个问题,并将解决方案作为对@vadian 答案的评论发布在下面。不过谢谢你的帮助!

标签: objective-c loops for-loop


【解决方案1】:

由于您的自定义类符合 KVC,您可以使用 NSPredicate 同时执行检查,而无需任何循环。

表达式的意思是:如果docs包含一个docNumbernumberleadAuthoursearch的对象,则跳过错误信息

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"docNumber == %i || leadAuthour == %@", number, search];
  if [[docs filteredArrayUsingPredicate:predicate] count] == 0 {
      NSLog(@"A match could not be found, please check your spelling");
  }

【讨论】:

  • 非常感谢您的回答,但实际上我设法以另一种方式解决了问题;每次[search isEqualToString:[aSearch leadAuthour]][sNumber isEqualToNumber:[nSearch docNumber]]不匹配时,“nt”都会增加一(我为每次检查使用不同的int变量)。如果 int 等于 docs 中的文档数,我知道没有匹配项,并且可以打印出一条消息!
【解决方案2】:

我解决了!我首先声明两个整数,每个循环一个,然后检查每次迭代是否找不到匹配项,即:

![search isEqualToString:[aSearch leadAuthour]]

如果这是真的,int 加一。然后,遍历数组后,如果int等于数组中的对象数,我就知道没有对象匹配,我可以打印出“找不到匹配项,请检查你的拼写” “-信息。这是它在代码中的样子:

int i = 0;
int j = 0;    

for (SMADoc *aSearch in docs) {
    if ([search isEqualToString:[aSearch leadAuthour]]) {
        //Open the file that is represented by UrlToDoc for that specific object
        [[NSWorkspace sharedWorkspace] openFile:[aSearch urlToDoc]];
    }
  if(![search isEqualToString:[aSearch leadAuthour]]){
    i++;
}
    if(i == [docs count]) {
  NSLog(@"A match could not be found, please check your spelling");
}
  }
} else {
//The string starts with a number and should be converted to an int and then the array of the numbers should be searched through
int number = atoi(searchC);
NSNumber *sNumber = [NSNumber numberWithInt:number];

for (SMADoc *nSearch in docs) {
    if ([sNumber isEqualToNumber:[nSearch docNumber]]) {
        [[NSWorkspace sharedWorkspace] openFile:[nSearch urlToDoc]];
    }
if(![search isEqualToString:[aSearch docNumber]]){
j++;
  }
if(j == [docs count]) {
  NSLog(@"A match could not be found, please check your spelling");
    }

}

我不知道它是否可以回答您自己的问题,但我这样做的目的是为可能提出相同问题的人提供答案。

【讨论】:

  • 您发布的代码无法编译。您不能在 int 上调用方法。
  • @rmaddy 感谢您的关注!我在这台电脑上没有可用的代码,所以我只是根据我的记忆编写它,但一定会尽快研究它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2017-02-19
  • 2021-12-12
  • 2021-05-15
相关资源
最近更新 更多