我对Swift很新,我有以下问题。
我的GameViewController.swift中的这个按钮:
if ($handle = opendir('/path/to/your/files')) {
while (false !== ($entry = readdir($handle))) {
echo "<img src='$entry'/><br/>";
}
closedir($handle);
}
我的GameScene.swift中的这个函数:
var currentGame : GameScene!
currentGame = GameScene()
@IBAction func restartGame(sender: UIButton) {
currentGame.loadView()
}
在ScoreLabel.center =(self.view?.center)!
Xcode告诉我线程1 exc_bad_instruction(code = exc_i386_invop subcode = 0x0)
我一直在搜索此错误和相关主题,但没有成功。
答案对我来说不清楚或不相关。
有人可以解释一下我的代码中有什么问题吗?
似乎我的self.view不存在,我不明白为什么。
非常感谢!
答案 0 :(得分:0)
ScoreLabel.center = (self.view?.center)!
此时可能self.view
为零。你最后有一个!
,确保Swift你知道有一个中心的视图。由于它崩溃了,也许这种保证是基于一个不正确的假设。
名为loadView
的方法应该可以加载视图,对吗?文档说“将该层次结构的根视图分配给视图控制器的视图属性。”
所以这个loadView方法应该包含类似
的东西super.loadView()
让视图层次结构滚动。
答案 1 :(得分:0)
我找到了解决方案。我将ScoreLabel移动到了viewDidLoad方法。
视图肯定会在该方法中实例化,因此nil错误消失。
由于我无法识别的原因,我的loadView方法中的视图尚未加载,这就是导致nil错误的原因。
感谢您的帮助!