我的NSDictionary在我的IBAction中已为零

时间:2018-10-08 01:48:09

标签: ios swift firebase firebase-realtime-database

我有一个显示用户创建的事件的表视图。当您单击其中一个时,会将您带到显示事件详细信息的其他页面。

我正在使用Firebase,并将postI D从表视图传递到详细视图,并且所有信息都正确地在NSDictionary中传递。

但是,当我尝试访问NSDictionary中的viewDidLoad并访问IBAction时,它告诉我NSDictionarynil。当我签入viewDidLoad时不是nil

一路走来,我对编程和学习还是很陌生,但是我已经坚持了一段时间,不知道出什么问题或如何解决

这是我的代码

import UIKit
import Firebase

class BeehiveViewViewController: UIViewController {



    @IBOutlet weak var eventImage: UIImageView!
    @IBOutlet weak var eventName: UILabel!
    @IBOutlet weak var location: UILabel!
    @IBOutlet weak var eventDate: UILabel!
    @IBOutlet weak var eventHost: UILabel!
    @IBOutlet weak var members: UILabel!
    @IBOutlet weak var joinButton: roundButton!


    var beehiveID: NSDictionary?
    var ref = Database.database().reference()


    override func viewDidLoad() {
        super.viewDidLoad()

        view.setGradientBackground(colourOne: primaryColor, colourTwo: secondaryColor)

        let uid = Auth.auth().currentUser?.uid
        ref.child("users").child(uid!).child(self.beehiveID?["pid"] as! String).observe(.value) { (snapshot) in

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

        let imageURL = self.beehiveID!["imageDownloadURL"] as! String

        let url = URL(string: imageURL)
        DispatchQueue.global(qos: .background).async {

            let data = NSData(contentsOf: url!)
            DispatchQueue.main.async {

            self.eventImage.image = UIImage(data: data! as Data)



            }
        }

        self.eventName.text = self.beehiveID?["eventName"] as? String
        self.eventDate.text = self.beehiveID?["eventDate"] as? String
        self.eventHost.text = self.beehiveID?["beehiveHost"] as? String
        self.location.text = self.beehiveID?["location"] as? String

        let uidd = Auth.auth().currentUser?.uid
        Database.database().reference().child("users").child(uidd!).child("Posts").child(self.beehiveID?["pid"] as! String).child("Members").observe(.value) { (snapshot) in

            let memberCount = snapshot.childrenCount
            self.members.text = "\(memberCount)"
        }

        let userID = Auth.auth().currentUser?.uid
        Database.database().reference().child("users").child(userID!).child("Posts").child(self.beehiveID?["pid"] as! String).observe(.value) { (snapshot) in

            print(snapshot)

            if (snapshot.exists()){

                self.joinButton.setTitle("Remove Beehive", for: .normal)
            }

            else{

                self.joinButton.setTitle("Join Beehive", for: .normal)
            }
        }



}

    @IBAction func buttonPressed(_ sender: Any) {

        if joinButton.titleLabel?.text == "Remove Beehive"{

            let uid = Auth.auth().currentUser?.uid
            let dbref = ref.child("users").child(uid!).child("Posts").child(beehiveID?["pid"] as! String)

//error is the line above that beehiveID?["pid"] is nil

            dbref.removeValue()


            navigationController?.popViewController(animated: true)

        }

        if joinButton.titleLabel?.text == "Join Beehive"{

            let uid = Auth.auth().currentUser?.uid
            let dbref = Database.database().reference().child("users").child(uid!).child("Posts").child("Members")
            Database.database().reference().child("users").child(uid!).child("Name").observe(.value) { (nameSnapshot) in
                let memberName = nameSnapshot.value as! String
                let userObject = [memberName: uid]
                dbref.updateChildValues(userObject as! [AnyHashable : String])
            }

        }
    }

}

1 个答案:

答案 0 :(得分:0)

我假设您正在从以前的控制器传递beeHive的值,因为您尚未初始化或在任何地方获得它的值:-

尝试在viewDidLoad结束之前设置一个断点,以仔细检查字典在该块处是否为空

self.beehiveID = snapshot.value as? NSDictionary

尝试使用“ if let”或“ guard”检查一下快照的值是否为nil,因为您可能只是向NSDictionary分配了nil值。另外,由于您使用可选参数来分配每个值,因此它不会返回异常,而只会为每个属性分配nil值

尝试一下,让我知道。很高兴提供帮助!