我收到此错误,但我不知道如何修复它
class GameView2: UIViewController {
@IBOutlet weak var resetButton: UIButton!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var player1ScoreLabel: UILabel!
@IBOutlet weak var player2ScoreLabel: UILabel!
var selectedPlayer: Int!
var currentPlayerMove: Int
var previousPlayerMove: Int!
var player1Score: Int
var player2Score: Int
答案 0 :(得分:4)
在swift中,必须初始化所有非可选的存储属性,无论是内联还是初始化。
在您的班级中,有6个可选属性(全部定义为隐式展开,以!
结尾的属性)和3个非可选属性(currentPlayerMove
,player1Score
和{{1} })。你应该内联初始化它们:
player2Score
或在初始化程序中,或使它们成为可选项,或使它们隐式展开选项:
var currentPlayerMove: Int = 0
var player1Score: Int = 0
var player2Score: Int = 0
并在使用之前初始化 - 一个好地方是var currentPlayerMove: Int!
var player1Score: Int!
var player2Score: Int!
:
viewDidLoad
一些指导原则: