0

this is a code for a program that behaves like this : When the user presses the button , the UIImage called " The Character" goes up (It changes its y axis). But when I run the program , it doesn't crash but when I press the button called "characterGoesUp" , the program crashes and I get the following in the debugger : "lldb".Why? Code :

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var TheCharacter: UIImageView!

@IBAction func characterGoesUp(_ sender: Any) {
    let xPos = TheCharacter.frame.origin.x

    let yPos = TheCharacter.frame.origin.y - 225

    let heightCharacter = TheCharacter.frame.size.height
    let widthCharacter = TheCharacter.frame.size.width

    UIView.animate(withDuration: 1.0, animations: {
        self.TheCharacter.frame = CGRect(x: xPos, y: yPos, width: widthCharacter, height: heightCharacter)
    }) { (finished) in

    }

}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}


}

Xcode version : 9.3

EDIT: The error is this : "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

1 Answers1

0

Your problem is that the imageView TheCharacter is not properly connected to IB , and because of !

@IBOutlet weak var TheCharacter: UIImageView!

in declaration it crashes as it's nil

//

Open the assistant editor and drag from the image to the ViewController's imageView

enter image description here

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87