我们可以退出一个函数(不是从应用程序)或者甚至使用跳转到特定行吗?
Can we exit from a function (Not from application) or even use a jump to certain line?
我正在寻找如何在函数中间退出或跳转到函数的末尾。
我有一个函数,在这个函数中有几个验证需要完成。对于每次验证,我都使用 alertView。
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
这里有额外的代码
再次验证
if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
此处添加代码
函数结束
}
如果我 运行 应用程序并且我有 spacing_CenterToCenter < 2 和 spacing_HorizontalStemRebars < 2,我将收到以下错误:
2015-12-15 20:08:02.684 Retaining-Wall[3845:162311] Warning: Attempt to present on while a presentation is in progress!
如果我只有 spacing_CenterToCenter < 2 或只有 spacing_HorizontalStemRebars < 2,应用程序将 运行 没有错误。
所以,我想使用命令退出或跳转到函数的末尾,这将在每个验证中。这样函数只会做一次验证,如果失败它会跳到函数的末尾。
有什么建议吗?
您可以使用 return
,如果您没有 return 类型,它会退出函数。
或者你可以使用 guard
guard spacing_CenterToCenter < 2 else{
// Show alert
return
}
好吧,如果您正在寻找任何 "one" 条件足以显示错误的情况,只需使用 else if 而不是多个 if's
func checking(){
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
else if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
}
如果你不看这样的东西,你可以让你的检查函数 return 成为一个 bool 并在 main 函数中跟踪你的 bool return。
在检查函数中:
func checking() -> Bool {
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
//return a false if this get triggered after showing the error
return false;
}
if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
//return a false if this get triggered after showing the error
return false;
}
//if it has reached this return statement means nothing went wrong and return a true.
return true;
}
现在在主函数中:
var checkReturns = checking();
if (!checkReturns) {
//your "false" condition processing
}
//your "true" coondition processing
我正在寻找如何在函数中间退出或跳转到函数的末尾。
我有一个函数,在这个函数中有几个验证需要完成。对于每次验证,我都使用 alertView。
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
这里有额外的代码
再次验证
if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
此处添加代码
函数结束
}
如果我 运行 应用程序并且我有 spacing_CenterToCenter < 2 和 spacing_HorizontalStemRebars < 2,我将收到以下错误:
2015-12-15 20:08:02.684 Retaining-Wall[3845:162311] Warning: Attempt to present on while a presentation is in progress!
如果我只有 spacing_CenterToCenter < 2 或只有 spacing_HorizontalStemRebars < 2,应用程序将 运行 没有错误。 所以,我想使用命令退出或跳转到函数的末尾,这将在每个验证中。这样函数只会做一次验证,如果失败它会跳到函数的末尾。
有什么建议吗?
您可以使用 return
,如果您没有 return 类型,它会退出函数。
或者你可以使用 guard
guard spacing_CenterToCenter < 2 else{
// Show alert
return
}
好吧,如果您正在寻找任何 "one" 条件足以显示错误的情况,只需使用 else if 而不是多个 if's
func checking(){
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
else if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
}
}
如果你不看这样的东西,你可以让你的检查函数 return 成为一个 bool 并在 main 函数中跟踪你的 bool return。
在检查函数中:
func checking() -> Bool {
if spacing_CenterToCenter < 2 {
let alertView = UIAlertController(title: "Stem Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Stem Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
//return a false if this get triggered after showing the error
return false;
}
if spacing_HorizontalStemRebars < 2 {
let alertView = UIAlertController(title: "Temp/Shrink Rebars Spacing C/C",
message: "Spacing of Rebars C/C < 2 inches, you must increase the Diameter of Temperature and Shrinkage Rebars", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Change now", style: .Default, handler: {
(action: UIAlertAction!) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})
alertView.addAction(OKAction)
self.presentViewController(alertView, animated: false, completion: nil)
//return a false if this get triggered after showing the error
return false;
}
//if it has reached this return statement means nothing went wrong and return a true.
return true;
}
现在在主函数中:
var checkReturns = checking();
if (!checkReturns) {
//your "false" condition processing
}
//your "true" coondition processing