【问题标题】:Obj-C Stop program if conditions are met如果满足条件,则 Obj-C 停止程序
【发布时间】:2014-05-29 10:48:46
【问题描述】:
if (stash != 0) {
            for (i=1; i<=6; i++) {
                a[1][i]=a[1][i]/stash;
            }
        }
        else
        {
            NSLog (@"Matrix is Not Invertible");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Matrix is not invertible!" delegate:nil cancelButtonTitle:@"Review Input" otherButtonTitles:nil, nil];
            [alert show];

        }

如果变量“stash”为零,我想停止程序,但我不能使用break,因为它不在循环中,我想使用return,但它说void不应该返回任何值......我应该怎么做才能让它工作?感谢您的所有帮助....

【问题讨论】:

  • 您能否添加更多上下文。当您说“停止程序”时,您是指应用程序还是只是 for 循环?
  • 请显示完整的方法。

标签: objective-c xcode5 ios7.1


【解决方案1】:

我不知道您是否只需要退出该方法,但您可以使用:

return;

或者重新定义方法返回一个整数-(int)myMethod;然后返回0;

【讨论】:

  • 不,不要重新定义没有意义的方法,只需使用return;
  • 如果他想返回退出代码以知道它是否成功,您可能会重新定义它。我真的不知道他想要什么。
  • 那仍然是错误的,你不应该在 iOS 中使用 exit(),因为它看起来像是应用程序崩溃了。
  • 我的意思是方法的退出代码,这样他就可以判断它是否成功。不是 exit() 整个应用程序。由于我不知道这种方法在哪里使用,所以我无法确定什么是理想的。
【解决方案2】:
//Add return statement, returning nothing
return;



- (void)yourMethod {


  //your code


  //your declerations

  if (stash != 0) {
    for (i=1; i<=6; i++) {
      a[1][i]=a[1][i]/stash;
    }
  }
  else
  {
    NSLog (@"Matrix is Not Invertible");
    UI *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Matrix is not invertible!" delegate:nil cancelButtonTitle:@"Review Input" otherButtonTitles:nil, nil];
    [alert show];

    //Add return statement here, returning nothing
    return;
  }


   //other code in your method
   //your code

}

【讨论】:

  • @Zaph 整个方法的实现不知道。我已经更新了答案以便更好地理解(我的帖子之前丢失了)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多