简体   繁体   中英

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) Swift

I am trying to switch from one view to another but I get this error and I do not know why am i getting the error.

i used this function to go to next controller:

   func showChatControllerForGroup(_ user: [User], groupName: String) {

        let vc: GroupChatMessangingViewController = self.storyboard?.instantiateViewController(withIdentifier: "GroupChatMessangingViewController") as! GroupChatMessangingViewController
//it crashed above here

        vc.group?.members = user
        vc.group?.name = groupName
        self.navigationController?.pushViewController(vc, animated: true)

}

to call this, i call it at another view at:

var group: Group? {
    didSet {
        navigationItem.title = group?.name
    }
}

var chosenUser: [User] = []
let user = self.chosenUser

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Create", style: .plain, target: self, action: #selector(createGroup))

    // Do any additional setup after loading the view.
}

 @objc func createGroup(){

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionReveal
    transition.subtype = kCATransitionFromBottom
    navigationController?.view.layer.add(transition, forKey: nil)
    _ = navigationController?.popViewController(animated: true)
    let user = self.chosenUser
    self.nextVC.showChatControllerForGroup(user, groupName: groupNameTextField.text!) }

just in case if anyone needs to see my User class:

class User {
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
var lastMessage: String?
var timestamp: String? //reason why this exist was because in mainmenu, we appended users, not msssage

init() {

}

init(dictionary: [String: AnyObject]) {
    self.id = dictionary["id"] as? String
    self.name = dictionary["name"] as? String
    self.email = dictionary["email"] as? String
    self.profileImageUrl = dictionary["profileImageUrl"] as? String
}

}

as of my Group class:

class Group {
var id: String?
var name: String?
var members: [User]!
var groupImageUrl: String?
var lastMessage: String?
var timestamp: String?

init() {

}

init(dictionary: [String: AnyObject]) {
    self.id = dictionary["id"] as? String
    self.name = dictionary["name"] as? String
}}

Is there any reason why is it crashing?

My guess is you in

    let vc: GroupChatMessangingViewController = self.storyboard?.instantiateViewController(withIdentifier: "GroupChatMessangingViewController") as! GroupChatMessangingViewController

you are calling the wrong storyboard. Check and see if you are in the right storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM