奇怪的Swift 2.0 if / else语句错误

时间:2015-10-04 17:24:21

标签: if-statement swift2

此代码在swift版本1中运行得非常好,但是当我更新到swift 2.0时它给了我一个错误:

import UIKit
class ViewController: UIViewController {
    @IBOutlet var inpute: UITextField!
    @IBOutlet var output: UILabel!

    @IBAction func button(sender: AnyObject) {
       print(inpute.text)
       let randomnum = arc4random_uniform(6)
       let guessblank = Int(inpute.text!)
       if guessblank != nil {
            if Int(randomnum) == guessblank {
               output.text = "you got it correct"
            }
            else { output.text = "Wrong number"
            }
        // here is where the error is. It tells me "Expected                 
       // expression" and "missing condition in if statement" 
       else { output.text = "please input a number"

            }
         }
     }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 个答案:

答案 0 :(得分:0)

你有一个不合时宜的支架 - 你的第一个"否则"声明完成了内部"如果",所以它需要一个闭合支撑,并且在你的第二个&#34之后需要一个闭合支撑;否则"。假设这是您想要的逻辑,以下代码没有错误:

@IBOutlet var inpute: UITextField!
@IBOutlet var output: UILabel!

@IBAction func button(sender: AnyObject) {
    print(inpute.text)
    let randomnum = arc4random_uniform(6)
    let guessblank = Int(inpute.text!)
    if guessblank != nil {
        if Int(randomnum) == guessblank {
            output.text = "you got it correct"
        }
        else { output.text = "Wrong number"
        }
    }
    else { output.text = "please input a number"
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}