Swift中的错误:property' self.circle1'没有在super.init电话上初始化?

时间:2014-06-24 21:51:34

标签: ios swift

我是编程的新手,我正在尝试在Swift中编写一个井字游戏应用程序,这个错误阻止我运行app:属性'self.circle1'未在super.init调用时初始化。

该应用尚未完成但我没有看到现有代码中的错误位置。请再次详细解释我的建议,我是一个初学者。谢谢! 我的代码:

import UIKit

class ViewController: UIViewController {

    //Initializing all imgs as variables:

    @IBOutlet strong var circle1: UIImageView
    @IBOutlet strong var circle2: UIImageView
    @IBOutlet strong var circle3: UIImageView
    @IBOutlet strong var circle4: UIImageView
    @IBOutlet strong var circle5: UIImageView
    @IBOutlet strong var circle6: UIImageView
    @IBOutlet strong var circle7: UIImageView
    @IBOutlet strong var circle8: UIImageView
    @IBOutlet strong var circle9: UIImageView

    @IBOutlet strong var circleBtn1: UIButton
    @IBOutlet strong var circleBtn2: UIButton
    @IBOutlet strong var circleBtn3: UIButton
    @IBOutlet strong var circleBtn4: UIButton
    @IBOutlet strong var circleBtn5: UIButton
    @IBOutlet strong var circleBtn6: UIButton
    @IBOutlet strong var circleBtn7: UIButton
    @IBOutlet strong var circleBtn8: UIButton
    @IBOutlet strong var circleBtn9: UIButton

    @IBOutlet strong var resetBtn: UIButton
    @IBOutlet strong var ResultsLabel: UILabel

    //Creating variables:

    var plays = Dictionary<Int,Int>()
    var done = false
    var aiDeciding = false

    //Function that says what to do if a UIButton is clicked:

    @IBAction func UIbuttonClicked(sender:UIButton) {
        ResultsLabel.hidden = true
        if !plays[sender.tag] && !aiDeciding && !done {
        setImgforCircle(sender.tag, player:1)

        }

    //Executing these functions:

        checkforWin()
        aiTurn()

    }

    //Function that sets an X or O in the proper circle depending on player:

    func setImgforCircle(spot:Int, player:Int) {
        var playerMark = player == 1 ? "x" : "o"
        plays[spot] = player
        switch spot {
        case 1:
            circle1.image = UIImage(named: playerMark)
        case 2:
            circle2.image = UIImage(named: playerMark)
        case 3:
            circle3.image = UIImage(named: playerMark)
        case 4:
            circle4.image = UIImage(named: playerMark)
        case 5:
            circle5.image = UIImage(named: playerMark)
        case 6:
            circle6.image = UIImage(named: playerMark)
        case 7:
            circle7.image = UIImage(named: playerMark)
        case 8:
            circle8.image = UIImage(named: playerMark)
        case 9:
            circle9.image = UIImage(named: playerMark)

        default:
            circle2.image = UIImage(named: playerMark)
        }

    }

    //Function that sees if certain paths of circles have the same mark

    func checkforWin() {
    var whoWon = ["Lost":0, "Won":1]
        for (key,value) in whoWon {
            if ((plays[1] == value && plays[2] == value && plays[3] == value) || //across top row
            (plays[4] == value && plays[5] == value && plays[6] == value) || //across middle row
            (plays[7] == value && plays[8] == value && plays[9] == value) || //across bottom row
            (plays[1] == value && plays[4] == value && plays[7] == value) || //down left column
            (plays[2] == value && plays[5] == value && plays[8] == value) || //down middle column
            (plays[3] == value && plays[6] == value && plays[9] == value) || //down right column
            (plays[1] == value && plays[5] == value && plays[9] == value) || //diagonal going right
            (plays[7] == value && plays[5] == value && plays[3] == value)) { //diagonal going left

                ResultsLabel.hidden = false
                ResultsLabel.text = "You \(key)!"
            }

        }

    }
    func aiTurn() {

    }


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


}

4 个答案:

答案 0 :(得分:3)

只需在声明的末尾添加问号或感叹号:

@IBOutlet strong var circle1: UIImageView?

使用Swift,在调用super.init之前必须初始化每个变量。在这种情况下,在加载Storyboard或nib之前,不会初始化变量,并且这将在调用类init方法之后进行初始化。

circle1变量的类型;除了是UIImageView之外;将是一个可选项,因此需要在其声明中指明。因此,您需要打开变量以使用问号或感叹号访问其值。

let prax = circle1?.image

if circle1 {   // now you're sure the object exists
   let prax = circle1!.image
}

(Xcode6 Beta-4似乎解决了这个问题,并在代码编辑器中自动提供“单击修复”错误按钮。)

答案 1 :(得分:2)

您缺少在类中声明的可选变量的初始化,这必须在调用super.init()之前完成

init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
  self.circle1 = UIImageView()
  super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

答案 2 :(得分:2)

此行中的@IBOutlet

@IBOutlet strong var circle1: UIImageView

提示UIImageView circle1(以及所有其他UIImageViews)被放置在storyboard或xib文件中。故事板需要从那里定义的元素到ViewController代码中的对象(circle1 var)建立连接。

要使此连接打开故事板,请打开助理编辑器(xcode右上角有三个按钮,第二个是助理编辑器)。你现在应该让两个编辑器彼此相邻。其中一个是故事板打开,另一个是ViewController文件。现在用鼠标从故事板中的UIImageView按Ctrl键拖动到ViewController中的circle1变量。对每个元素重复,错误应该消失。

答案 3 :(得分:0)

对于swift的更新版本,UIButtonClicked函数应该如下所示...

    @IBAction func UIButtonClicked(sender:UIButton) {
            ticTacLabel.hidden = true
            if plays[sender.tag] == nil && !aiDeciding && !done {
                setImageForSpot(sender.tag, player:1)
            }

希望这有帮助!