喜歡它在標題中說,我試圖改變標籤文本點擊一個按鈕。錯誤出現在線self.playerChoice.text = "You: Rock"
Swift:標籤文本 - >「致命錯誤:意外地發現零,同時展開一個可選值」
import UIKit
class ViewController: UIViewController {
var player : Int = Int()
@IBOutlet weak var readyLabel: UILabel!
@IBAction func noButton(sender: AnyObject) {
exit(0)
}
// ---------------------------------------------------------
@IBOutlet var computerChoice: UILabel!
@IBOutlet var playerChoice: UILabel!
@IBOutlet var score: UILabel!
// Variables -------------------------------------------------
let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"
//------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// ----------------------------------------------------------------
@IBAction func rockButton(rockbut: UIButton) {
player = 0
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Rock"
}
@IBAction func paperButton(paperbut: UIButton) {
player = 1
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Paper"
}
@IBAction func scissorsButton(scissorsbut: UIButton) {
player = 2
var ai = arc4random_uniform(3)
self.playerChoice.text = "You: Scissors"
}
}
「playerChoice」是否已初始化?你有連接IB的插座嗎? – gagarwal 2014-11-24 20:46:25
應該是,我採取了添加標籤 – artie711 2014-11-24 21:02:55