0

我收到了这个 sigabrt 错误:视图中显示的线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 确实加载了

该代码负责关注未关注的用户 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)
    }


}

}

4

0 回答 0