2015-10-04 70 views
-3

這個代碼在迅速版1完全正常,但是當我更新,以迅速2.0它給了我一個錯誤:奇怪的雨燕2.0 if/else語句錯誤

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. 
    } 
} 
+2

該代碼不會在Swift 1中編譯,所以不要聲稱它做了。 – matt

回答

0

你有一個括號外的地方 - 你的第一個「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. 
}