gpt4 book ai didi

ios - Swift : property 'self.circle1' not initialized at super. 初始化调用出错?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:38 25 4
gpt4 key购买 nike

我是编程新手,正在尝试用 Swift 编写一个井字游戏应用程序,但这个错误使我无法运行该应用程序:属性“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.
}


}

最佳答案

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

@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 似乎已经解决了这个问题,并自动在代码编辑器中给出“点击修复”错误按钮。)

关于ios - Swift : property 'self.circle1' not initialized at super. 初始化调用出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com