线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)出了什么问题?

时间:2017-06-12 20:40:46

标签: ios swift3 optional exc-bad-instruction

我得到了这个sigabrt错误:线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0),如视图中所示加载

该代码负责关注以下用户  在从搜索视图到用户配置文件视图的segue期间发生sigabrt错误,我可以在该视图中关注或取消关注该用户

var loggedinuser : FIRUser?
var otheruser :  NSDictionary?
var  loggedinuserdata : NSDictionary?
var databaseref : FIRDatabaseReference!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var followorunfollow: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    self.databaseref = FIRDatabase.database().reference()

    databaseref?.child("users").child(self.loggedinuser!.uid).observe(.value, with: { (snapshot) in :Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 

        self.loggedinuserdata?.setValue(self.loggedinuser!.uid, forKey: "uid" )
    }) { (error) in
        print(error.localizedDescription)
    }



    databaseref?.child("users").child(self.otheruser?["uid"] as! String).observe(.value, with: { (snapshot) in

        let uid =  self.otheruser?["uid"] as! String
        self.otheruser = snapshot.value as? NSDictionary
        self.otheruser?.setValue(uid, forKey: "uid")

    }) { (error) in
        print(error.localizedDescription)
    }

    databaseref?.child("following").child(self.loggedinuser!.uid).child(self.otheruser?["uid"] as! String).observe(.value, with: { (snapshot) in

        if(snapshot.exists())
        {
            self.followorunfollow.setTitle("follow", for : UIControlState())
        }
        else
        {
            self.followorunfollow.setTitle("unfollow", for : UIControlState())

        }



    }) { (error) in
        print(error.localizedDescription)
    }


    self.name.text = self.otheruser?["name"] as? String


}

@IBAction func didtapfollow(_ sender: Any) {
    let followerref = "followers/\(self.otheruser?["uid"] as! String)/\(self.loggedinuserdata?["uid"] as! String)"
    let followingref =  "following/" + (self.loggedinuserdata?["uid"] as! String) + "/" + (self.otheruser?["uid"] as! String )

    if(self.followorunfollow.titleLabel?.text == "follow")
    {

        let followerdata = ["name" : self.loggedinuserdata?["name"] as! String, "pc " :" \(self.loggedinuserdata?["pc"])"]

        let followingdata = ["name" : self.otheruser?["name"] as! String, "pc ":" \(self.otheruser?["pc"])"]


        let childupdates = [followerref: followerdata,
                            followingref:followingdata]


        databaseref?.updateChildValues(childupdates)

        let followerscount:Int?
        let followingcount:Int?

        if(self.otheruser?["followerscount"]  == nil )
        {
            followerscount = 1
        }
        else {
            followerscount =  self.otheruser?["followerscount"] as! Int + 1
        }
        if(self.loggedinuserdata?["followingcount"] == nil)
        {
            followingcount = 1
        }

        else {
            followingcount =  self.loggedinuserdata?["followingcount"] as! Int -  1
        }

        databaseref?.child("users").child(self.loggedinuserdata?["uid"]  as! String).child("followingcount").setValue(followingcount)
        databaseref?.child("users").child(self.otheruser?["uid"]  as! String).child("followerscount").setValue(followerscount)

    }


    else
    {

        databaseref?.child("users").child(self.loggedinuserdata?["uid"]  as! String).child("followingcount").setValue(self.loggedinuserdata!["followingcount"] as! Int - 1)

        databaseref?.child("users").child(self.otheruser?["uid"]  as! String).child("followerscount").setValue(self.otheruser!["followerscount"] as! Int - 1 )



        _ = "followers/\(self.otheruser?["uid"] as! String)/\(self.loggedinuserdata?["uid"] as! String )"

        let followingref =  "following/" + (self.loggedinuserdata?["uid"] as! String) + "/" + (self.otheruser?["uid"] as! String )



        let childupdates = [followingref: NSNull(),
                            followerref:NSNull()]


        databaseref?.updateChildValues(childupdates)
    }


}

}

0 个答案:

没有答案