2016-10-01 57 views
0

我刚刚开始学习Swift,并且一直试图移动UIView以响应触摸。我收到一个exc_bad_instruction错误。将UIView中心设置为cgpoint时的exc_bad_指令

var location = CGPoint(x: 0, y:0) 
@IBOutlet weak var person: UIView! 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch : UITouch! = touches.first 

    location = touch.location(in: self.view) 

    person.center = location 
} 
+0

哪一行抛出错误? –

+0

@ Mr.UB第6行 –

+0

'center'的类型是什么,'print(location)'的输出是什么? –

回答

0

exc_bad_instruction通常是空指针的提示。确保Person不是零。

此外,请确保命名您的属性在开头有一个小写字母 - 在这种情况下,这将是person。当使用Person时,您有冒险将类型称为“不合格”的风险。

如果您需要进一步的帮助,请编辑您的帖子,并添加您的项目的更多代码示例 - 特别是类Person和整个UIViewController您使用它的地方。

0

变量位置应已被定义为一个明确CGPoint:

var location : CGPoint = CGPoint(x:0, y:0)