Swift 我们可以退出一个函数(而不是应用程序)或者甚至使用跳转到某一行吗? 我正在寻找如何在函数中间退出或者跳转到函数的结尾。
我有一个函数,在这个函数中有两个验证要做。对于每次验证,我都使用alertViewSwift 我们可以退出一个函数(而不是应用程序)或者甚至使用跳转到某一行吗? 我正在寻找如何在函数中间退出或者跳转到函数的结尾。,swift,exit,Swift,Exit,我有一个函数,在这个函数中有两个验证要做。对于每次验证,我都使用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 Diamete
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)
}
如果中心间距小于2{
让alertView=UIAlertController(标题:“钢筋间距C/C”,
消息:“钢筋间距C/C<2英寸,必须增加杆钢筋直径”,首选样式:。警告)
让OKAction=UIAlertAction(标题:“立即更改”,样式:。默认值,处理程序:{
(操作:UIAlertAction!)->在中无效
self.dismissViewControllerAnimated(真,完成:无)
})
alertView.addAction(OKAction)
self.presentViewController(alertView,动画:false,完成: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)
}
如果水平杆间距<2{
让alertView=UIAlertController(标题:“温度/收缩钢筋间距C/C”,
消息:“钢筋间距C/C<2英寸,必须增加温度和收缩钢筋的直径”,preferredStyle:。警报)
让OKAction=UIAlertAction(标题:“立即更改”,样式:。默认值,处理程序:{
(操作:UIAlertAction!)->在中无效
self.dismissViewControllerAnimated(真,完成:无)
})
alertView.addAction(OKAction)
self.presentViewController(alertView,动画:false,完成:nil)
}
这里有附加代码
功能结束
}
如果我运行应用程序,并且如果我的spacing_CenterToCenter<2和spacing_HorizontalStemRebars<2,我将得到以下错误:
2015-12-15 20:08:02.684挡土墙[3845:162311]警告:试图在演示过程中演示
如果只有间距CenterToCenter<2或只有间距水平杆<2,则应用程序将无错误运行。
因此,我认为应该使用一个命令exit或跳转到函数的末尾,这将在每次验证中进行。这样,函数将只执行一次验证,如果失败,它将跳转到函数结束
有什么建议吗?您可以使用
返回
,如果您没有返回类型,它将退出该功能。或者您可以使用
guard
guard spacing_CenterToCenter < 2 else{
// Show alert
return
}
guard space\u CenterToCenter<2 else{
//显示警惕
返回
}
您可以使用返回
,如果您没有返回类型,它将退出该功能。或者您可以使用
guard
guard spacing_CenterToCenter < 2 else{
// Show alert
return
}
guard space\u CenterToCenter<2 else{
//显示警惕
返回
}
如果您正在寻找任何“一个”条件足以显示错误的情况,只需使用else if而不是多个if
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)
}
如果你正在寻找一个任何“一个”条件都足以显示错误的情况,只需使用else if而不是多个if
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)
}