Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on UIImage, help me please !

Hello😉, I am a beginner in swift, and I am french, but it does not matter, because Google translation exists, so you can answer me in English!


So let's start: I try to develop an application by following a tutorial, because as I said at the beginning, I have just started to learn the swift. I'm doing a chat, and I'm having a mistake, which I've never had before. I will quote it: "Thread 1: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0)".

It is located in the viewDidLoad (), when I want to attribute to my "imageProfile" of type "UIImage" the value of an image located well in my Assets.xcassets folder. For more information, my image is called Family.


Thank you in advance for answering me as soon as possible! N PN

My code of LoginController.swift :

/
/
/
/
/
/
/
import UIKit
import Firebase
class LoginController: UIViewController, UITextFieldDelegate {


    @IBOutlet weak var imageProfile: UIImageView!
    @IBOutlet weak var loginRegisterSegmentedControl: UISegmentedControl!
    @IBOutlet weak var container: UIView!
    @IBOutlet weak var textFieldNames: UITextField!
    @IBOutlet weak var textFieldEmail: UITextField!
    @IBOutlet weak var textFieldPassword: UITextField!
    @IBOutlet weak var buttonLoginRegister: UIButton!

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

    var inputsContainerViewHeightAnchor: NSLayoutConstraint?

    func setupContainer() {

    }

    var imageFamily: UIImage =  imageLiteral(resourceName: "Family")

    override func viewDidLoad() {
        super.viewDidLoad()

        self.imageProfile.image = UIImage(named: "Family")

        self.container.layer.cornerRadius = 5
        loginRegisterSegmentedControl.selectedSegmentIndex = 1
    }



    @IBAction func loginRegisterSegmentedControlAction(_ sender: UISegmentedControl) {

        let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex)
        buttonLoginRegister.setTitle(title, for: .normal)
        print(loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex) as! String)

    }

    @IBAction func actionButtonRegister(_ sender: UIButton) {

        if let email = textFieldEmail.text, let password = textFieldPassword.text, let names = textFieldNames.text {

            Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in
     
                if error != nil {
         
                    print("erreur")
                    return
                }
         
                else{
         
                    print("enregistrement réussi")
         
                }
     
                guard let uid = user?.uid else {
         
                    return
         
                }
     
                let ref = Database.database().reference(fromURL: "https:/
                let usersReference = ref.child("utilisateurs").child(uid)
                let values = ["noms": names, "email": email]
                usersReference.updateChildValues(values, withCompletionBlock: { (error2, ref) in
         
                    if error2 != nil {
             
                        print("erreur2")
                        return
             
                    }
         
                })
     
            }

        }

        print("'buttonRegister' a été pressé")

    }

}


My code of ViewController.swift :

/
/
/
/
/
/
/
import UIKit
import Firebase
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        /
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
}


My Main.storyboard :

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If you need more info, ask me.

Etes vous sûr que l'erreur se produit ligne 36 ?


Pour s'en assurer, ajouter des log :

   override func viewDidLoad() {
        super.viewDidLoad()
        print("Before UIImage")
        self.imageProfile.image = UIImage(named: "Family")
        print("After UIImage")
        self.container.layer.cornerRadius = 5
        loginRegisterSegmentedControl.selectedSegmentIndex = 1
    }


QU'est ce que vous obtenez exactement dans les print sur la console ?


Le fichier Family est il bien de type png ?

When you find runtime crash like EXC_BAD_INSTRUCTION, you should check the output of the debug console.


I guess it shows: fatal error: unexpectedly found nil while unwrapping an Optional value

If so, check your storyboard if the UIImageView is connected to the @IBOutlet `imageProfile`.

(Also check your post after you submitted it. No info about your storyboard.)

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on UIImage, help me please !
 
 
Q