swiftexc-bad-instruction

exc_bad_instruction when setting UIView center to cgpoint


I've just started learning Swift, and I've been trying to move a UIView in response to a touch. I'm getting an exc_bad_instruction error.

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
}

Solution

  • exc_bad_instruction usually is a hint for a null pointer. Make sure Person is not nil.

    Also make sure to name your properties to have a lower case letter at the beginning - in this case this would be person. When using Person you run the risk to call the Type not the Propery.

    If you need further help, please edit your post and add further code samples of your project - specifically of the Class Person and the whole UIViewController where you use it.