xcode Swift 中的 If else 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28331082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If else Statement in Swift
提问by Coder
I'm a newbie at swift, so how do I add an extra else if statement saying greater than 56 & less than 65 to my button?
我是 swift 的新手,那么我如何向我的按钮添加一个额外的 else if 语句,表示大于 56 且小于 65?
The button work fine, it just need one more additional statement added to finished up my coding.
该按钮工作正常,它只需要添加一个额外的语句来完成我的编码。
@IBAction func Submit4Button(sender: AnyObject) {
let a: Float? = (EnterH4Dimension.text as NSString).floatValue
if a > 65 {
self.DimensionLabel.text = " Cameron H4 With Guide Pins."
yesButton.hidden = false
noButton.hidden = false
emailButton.hidden = true
DimensionLabel.hidden = false
self.view.endEditing(true)
}
else if EnterH4Dimension.text == "" {
noButton.hidden = true
yesButton.hidden = true
var alert = UIAlertController(title: "Dimension", message: "Missing Data Input.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
else {
self.DimensionLabel.text = "Dimensions meet guideline"
noButton.hidden = true
yesButton.hidden = true
DimensionLabel.hidden = false
shallowDimButton.hidden = true
emailButton.hidden = false
self.view.endEditing(true)
}
}
回答by Jér?me
If you are referring to a
:
如果您指的是a
:
You can try the following:
您可以尝试以下操作:
else if a > 56 && a < 65 {}
Depending on whether or not you want to include 56 and 65 you can use <=
& >=
根据您是否要包括 56 和 65,您可以使用<=
&>=
Note that all your else if
statements should be place before the final else
statement
请注意,您的所有else if
陈述都应放在最终else
陈述之前