If statement 怪异的Swift 2.0 if/else语句错误

If statement 怪异的Swift 2.0 if/else语句错误,if-statement,swift2,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)

该代码在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.
    }
}

你有一个大括号不合适——你的第一个“else”语句完成了内部的“if”,因此它需要一个右大括号,在第二个“else”之后少一个右大括号。假设这是您想要的逻辑,则以下代码没有错误:

@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.
}

该代码也不会在Swift 1中编译,所以不要声称它编译了。