我正在尝试按照教程创建一个Tic Tac Toe应用程序,但是我收到错误," Swift Dynamic Cast失败"。
我的代码是:
class ViewController: UIViewController
{
var activePlayer = 1
var gameActive = true
//1 - Circle, 2 = Cross
var gameState = [0,0,0,0,0,0,0,0,0]
var winningCombinations = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
@IBOutlet weak var button: UIButton!
@IBOutlet weak var gameOverLabel: UILabel!
@IBAction func gameOverPressed(sender: AnyObject) {
activePlayer = 1
gameActive = true
//1 - Circle, 2 = Cross
gameState = [0,0,0,0,0,0,0,0,0]
var abutton: UIButton
for var i = 0; i < 9; i++ {
abutton = view.viewWithTag(i) as UIButton!
abutton.setImage(nil, forState: .Normal)
}
gameOverLabel.hidden = true
gameOverButton.hidden = true
gameOverLabel.center = CGPointMake(gameOverLabel.center.x - 4000, gameOverLabel.center.y)
gameOverButton.center = CGPointMake(gameOverButton.center.x - 4000, gameOverButton.center.y)
}
@IBOutlet weak var gameOverButton: UIButton!
@IBAction func buttonPressed(sender: AnyObject) {
if gameState[sender.tag] == 0 && gameActive == true{
var image = UIImage()
gameState[sender.tag] = activePlayer
if activePlayer == 1{
image = UIImage(named: "circle.png")!
sender.setImage(image, forState: .Normal)
activePlayer = 2
}else if activePlayer == 2{
image = UIImage(named: "cross.png")!
sender.setImage(image, forState: .Normal)
activePlayer = 1
}
sender.setImage(image, forState: .Normal)
for combination in winningCombinations {
if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]{
var labelText = "Compiler has won"
if gameState[combination[0]] == 1{
labelText = "Programmer has Won"
}
gameOverLabel.hidden = false
gameOverButton.hidden = false
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.gameOverLabel.center = CGPointMake(self.gameOverLabel.center.x + 4000, self.gameOverLabel.center.y)
self.gameOverButton.center = CGPointMake(self.gameOverButton.center.x + 4000, self.gameOverButton.center.y)
})
gameActive = false
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
gameOverLabel.hidden = true
gameOverButton.hidden = true
gameOverLabel.center = CGPointMake(gameOverLabel.center.x - 4000, gameOverLabel.center.y)
gameOverButton.center = CGPointMake(gameOverButton.center.x - 4000, gameOverButton.center.y)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
}
}
我尝试运行此程序时得到的错误是:
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x10860f620: pushq %rbp
0x10860f621: movq %rsp, %rbp
0x10860f624: pushq %rbx
0x10860f625: pushq %rax
0x10860f626: movq %rsi, %rcx
0x10860f629: movq %rdi, %rbx
0x10860f62c: xorl %eax, %eax
0x10860f62e: testq %rbx, %rbx
0x10860f631: je 0x10860f64c ; swift_dynamicCastObjCClassUnconditional + 44
0x10860f633: movq 0x82756(%rip), %rsi ; "isKindOfClass:"
0x10860f63a: movq %rbx, %rdi
0x10860f63d: movq %rcx, %rdx
0x10860f640: callq 0x1086121ca ; symbol stub for: objc_msgSend
0x10860f645: testb %al, %al
0x10860f647: movq %rbx, %rax
0x10860f64a: je 0x10860f653 ; swift_dynamicCastObjCClassUnconditional + 51
0x10860f64c: addq $0x8, %rsp
0x10860f650: popq %rbx
0x10860f651: popq %rbp
0x10860f652: retq
0x10860f653: leaq 0xcdc8(%rip), %rax ; "Swift dynamic cast failed"
0x10860f65a: movq %rax, 0x8ae57(%rip) ; gCRAnnotations + 8
0x10860f661: int3
0x10860f662: nopw %cs:(%rax,%rax)
我是编程新手,所以我很欣赏简单的答案。我该如何解决这个问题?
答案 0 :(得分:0)
您确定标签0-9的所有视图都是UIButtons吗?似乎不太可能,因为任何视图的默认标记都是0。
for var i = 0; i < 9; i++ {
abutton = view.viewWithTag(i) as UIButton!
abutton.setImage(nil, forState: .Normal)
}
我想它是在你的故事板上抓取UIView或其他一些视图并试图将它投射到UIButton然后崩溃。