Firebase获取用户数据 - Swift

时间:2017-10-22 17:15:52

标签: ios swift firebase

我想要获取数据,我已多次这样做但由于某种原因它无法正常工作,我认为没有理由说明原因。

我获取数据的代码

func fetchCurrentUserInfo() {

    let currentUser = Auth.auth().currentUser!
    let currentUserRef = dataBaseRef.child("users").child(currentUser.uid)

    currentUserRef.observeSingleEvent(of: .value, with: { (snapshot) in

        let user = UserClass(snapshot: snapshot)
        self.downloadImageFromFirebase(urlString: user.photoURL!)
        self.userName.text = user.getFullName()

        //SD Cache
        self.userProfileImage.sd_setImage(with: URL(string: user.photoURL!), placeholderImage: UIImage(named: "UserImageTemplate"))

    }) { (error) in
        let alert = SCLAlertView()
        _ = alert.showError("OOPS!", subTitle: error.localizedDescription)
    }
}

我的用户类文件

struct UserClass {

        var firstName: String?
        var email: String?
        var photoURL: String?
        var uid: String?
        var ref: DatabaseReference!
        var key: String?
        var lastName: String?
        var accountType: String?

        init(snapshot: DataSnapshot){

            key = snapshot.key
            ref = snapshot.ref
            accountType = (snapshot.value! as! NSDictionary)["Account Type"] as? String
            firstName = (snapshot.value! as! NSDictionary)["First Name"] as? String
            email = (snapshot.value! as! NSDictionary)["email"] as? String
            uid = (snapshot.value! as! NSDictionary)["uid"] as? String
            photoURL = (snapshot.value! as! NSDictionary)["photoURL"] as? String
            lastName = (snapshot.value! as! NSDictionary)["Last Name"] as? String
        }

        func getFullName() -> String {
            return ("\(firstName!) \(lastName!)")
        }

    }
}

我已将读取和写入规则设置为true。当我调用fetchCurrentUserInfo函数时,它返回第一个名字,姓氏,密钥等的所有数据,但它返回照片URL,UID或电子邮件为nil,给我错误:致命错误:unexpectedly found nil while unwrapping an Optional value

1 个答案:

答案 0 :(得分:2)

您的崩溃日志说明了一切。您试图通过as!强制转换来强制打开可选变量。确保您的snapshot

中包含有效值

此外,更好的模型会这样,这更安全:

    struct UserClass {

        var firstName: String!
        var email: String!
        var photoURL: String!
        var uid: String!
        var ref: DatabaseReference!
        var key: String!
        var lastName: String!
        var accountType: String!

        init?(snapshot: DataSnapshot?) {

            guard let value = snapshot?.value as? [String: AnyObject],
                let accountType = value["Account Type"] as? String,
                let firstName = value["First Name"] as? String,
                let email = value["email"] as? String,
                let uid = value["uid"] as? String,
                let photoURL = value["photoURL"] as? String,
                let lastName = value["Last Name"] as? String else {
                    return nil
            }

            self.key = snapshot?.key
            self.ref = snapshot?.ref
            self.accountType = accountType
            self.firstName = firstName
            self.email = email
            self.uid = uid
            self.photoURL = photoURL
            self.lastName = lastName
        }

        func getFullName() -> String {
            return ("\(firstName!) \(lastName!)")
        }

    }