无法为类型'用户'调用初始值设定项。使用类型'的参数列表(快照:(DataSnapshot))Swift 3

时间:2017-07-25 15:16:50

标签: ios firebase swift3

我更新Firebase pod后出现此错误:

无法为类型'用户'调用初始值设定项使用类型'(快照:(DataSnapshot))'

的参数列表

这是我的代码enter image description here

任何解决这个问题的想法...... ???

func loadUserInfo(){

    let userRef = dataBAseRef.child("users/\(Auth.auth().currentUser!.uid)")
    userRef.observe(.value, with: { (snapshot) in

        let user = User(snapshot: snapshot)
        self.usernameLabel.text = user.username
        self.userCountry.text = user.country!
        self.userBiographyTextView.text = user.biography!

        let imageURL = user.photoURL!

        self.storageRef.reference(forURL: imageURL).data(withMaxSize: 1 * 1024 * 1024, completion: { (imageData, error) in
            if error == nil {
                DispatchQueue.main.async {
                    if let data = imageData {

                        self.userImageView.image = UIImage(data: data)
                    }
                }
            } else {
                print(error!.localizedDescription)
            }
        })

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

在我将所有内容更新到firebase 4.0.4后,我收到了错误,这是整个代码:

import Foundation
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth



struct AuthService {
    var dataBAseRef: DatabaseReference! {
        return Database.database().reference()
    }
    var storageRef: StorageReference! {
        return Storage.storage().reference()
    }


    // 1- creating the signup function

    func signUp(username:String,email:String,country:String,password:String,biography: String, pictureData:NSData!) {
        Auth.auth().createUser(withEmail: email, password: password, completion: {(user, error) in
            if error == nil {

                self.setUserInfo(user: user, username: username, country: country, password: password, biography: biography, pictureData: pictureData)
            } else {
                print(error?.localizedDescription as Any)
            }


        })
    }

    // 2- create the set user info function

    private func setUserInfo(user: User!, username: String, country: String, password: String, biography: String, pictureData: NSData!)
    {

        let imagePath = "profileImage\(user.uid)/userPic.jpg"

        let imageRef = storageRef.child(imagePath)

        let metaData = StorageMetadata()
        metaData.contentType = "image/jpeg"

        imageRef.putData(pictureData as Data, metadata: metaData) {(newMetaData, error) in

            if error == nil {

                let changeRequest = user.profileChangeRequest()
                changeRequest.displayName = username

                if let photoURL = newMetaData!.downloadURL() {
                    changeRequest.photoURL = photoURL
                }

                changeRequest.commitChanges(completion: { (eroor) in
                    if error == nil {
                        self.saveUserInfo(user: user, username: username, country: country, password: password, biography: biography )




                    } else {
                        print(error?.localizedDescription as Any)
                    }

                })

            } else {
                print(error?.localizedDescription as Any)
            }





        }


    }

    // 3- save the User info in Firebase

    private func saveUserInfo(user: User!, username: String, country: String, password: String, biography: String)
    {
        let userInfo  = ["Email": user.email!, "username": username, "country": country,"biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)]

        let userRef = dataBAseRef.child("users").child(user.uid)
        userRef.setValue(userInfo)  { (error, ref) in
            if error == nil {
                print("user info saved successfully")
                self.logIn(email: user.email!, password: password)


            } else {
                print(error?.localizedDescription as Any)
            }


        }



    }



    // logging the user in function

    func logIn(email: String, password: String) {
        Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
            if error == nil {
                if let user = user {
                    print("\(user.displayName!) has logged in successfully")

                    let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDel.logUser()
                }



            }
            else {
                print(error?.localizedDescription as Any)

            }

        })
    }





}

现在我有2个错误:

here is the errors

我尝试将profileChangeRequest()更新为createProfileChangeRequest(),但没有帮助

这是User class:

import Foundation
import Firebase
import FirebaseDatabase

struct User {
    var username: String!
    var email: String?
    var country: String?
    var biography: String?
    var photoURL: String!
    var uid: String!
    var ref: DatabaseReference?
    var key: String?


    init(snapshot: DataSnapshot) {
         key = snapshot.key
         ref = snapshot.ref
         username = (snapshot.value! as! NSDictionary)["username"] as! String
         email = (snapshot.value! as! NSDictionary)["email"] as? String
         country = (snapshot.value! as! NSDictionary)["country"] as? String
         uid = (snapshot.value! as! NSDictionary)["uid"] as! String
         biography = (snapshot.value! as! NSDictionary)["biography"] as? String
        photoURL = (snapshot.value! as! NSDictionary)["photoURL"] as! String
     }  
}

1 个答案:

答案 0 :(得分:1)

我认为您的用户类与他们在4.x SDK中从FIRUser更改为User的{​​{3}}类发生了冲突。尝试将您的用户类重命名为LocalUser,并查看是否有帮助,如果有,则会出现问题