Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从Firebase存储中的文档中获取项目并使用SWIFT将其添加到视图中_Ios_Swift_Firebase - Fatal编程技术网

Ios 如何从Firebase存储中的文档中获取项目并使用SWIFT将其添加到视图中

Ios 如何从Firebase存储中的文档中获取项目并使用SWIFT将其添加到视图中,ios,swift,firebase,Ios,Swift,Firebase,我试图从Firestore文档(UserProfileVC)中获取变量“name”,并将其添加到标题(UserProfileHeader)中,但在获取所需格式的数据时遇到问题。目前,我收到下面UserProfileVC中“userReference.getDocument{(snapshot)in”行的错误消息。我尝试了几种不同的方法,但没有任何效果。我做错了什么,或者有没有更好的方法来组织数据?我是SWIFT的新手,所以我的解决方案可能太差了 错误消息: class User{ va

我试图从Firestore文档(UserProfileVC)中获取变量“name”,并将其添加到标题(UserProfileHeader)中,但在获取所需格式的数据时遇到问题。目前,我收到下面UserProfileVC中“userReference.getDocument{(snapshot)in”行的错误消息。我尝试了几种不同的方法,但没有任何效果。我做错了什么,或者有没有更好的方法来组织数据?我是SWIFT的新手,所以我的解决方案可能太差了

错误消息:

class User{

    var username: String!
    var name: String!
    var uid: String!

    init(uid: String, dictionary: Dictionary<String, AnyObject>) {
        self.uid = uid
        if let username = dictionary["username"] as? String {
            self.username = username
        }
        if let name = dictionary["name"] as? String {
            self.name = name
        }
    }
var user: User? {
        didSet {
            let fullName = user?.name
            nameLabel.text = fullName

            guard let profileImageUrl = user?.profileImageUrl else { return }
            profileImageView.loadImage(with: profileImageUrl)
        }
    }
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader

        header.delegate = self

        header.user = self.user
        navigationItem.title = user?.username

        return header
    }

func fetchCurrentUserData() {

    guard let currentUid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()
    let userReference = db.collection("profile_data").document(currentUid)

    userReference.getDocument { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        let uid = snapshot.key
        let user = User(uid: uid, dictionary: dictionary)
        self.user = user
        self.navigationItem.title = user.username
        self.collectionView?.reloadData()
    }
}  
上下文闭包类型“(DocumentSnapshot?,错误?)->Void” 2个参数,但在闭包体中使用了1个

模型-用户类:

class User{

    var username: String!
    var name: String!
    var uid: String!

    init(uid: String, dictionary: Dictionary<String, AnyObject>) {
        self.uid = uid
        if let username = dictionary["username"] as? String {
            self.username = username
        }
        if let name = dictionary["name"] as? String {
            self.name = name
        }
    }
var user: User? {
        didSet {
            let fullName = user?.name
            nameLabel.text = fullName

            guard let profileImageUrl = user?.profileImageUrl else { return }
            profileImageView.loadImage(with: profileImageUrl)
        }
    }
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader

        header.delegate = self

        header.user = self.user
        navigationItem.title = user?.username

        return header
    }

func fetchCurrentUserData() {

    guard let currentUid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()
    let userReference = db.collection("profile_data").document(currentUid)

    userReference.getDocument { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        let uid = snapshot.key
        let user = User(uid: uid, dictionary: dictionary)
        self.user = user
        self.navigationItem.title = user.username
        self.collectionView?.reloadData()
    }
}  
控制器-用户档案vc:

class User{

    var username: String!
    var name: String!
    var uid: String!

    init(uid: String, dictionary: Dictionary<String, AnyObject>) {
        self.uid = uid
        if let username = dictionary["username"] as? String {
            self.username = username
        }
        if let name = dictionary["name"] as? String {
            self.name = name
        }
    }
var user: User? {
        didSet {
            let fullName = user?.name
            nameLabel.text = fullName

            guard let profileImageUrl = user?.profileImageUrl else { return }
            profileImageView.loadImage(with: profileImageUrl)
        }
    }
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader

        header.delegate = self

        header.user = self.user
        navigationItem.title = user?.username

        return header
    }

func fetchCurrentUserData() {

    guard let currentUid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()
    let userReference = db.collection("profile_data").document(currentUid)

    userReference.getDocument { (snapshot) in
        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        let uid = snapshot.key
        let user = User(uid: uid, dictionary: dictionary)
        self.user = user
        self.navigationItem.title = user.username
        self.collectionView?.reloadData()
    }
}  
override func collectionView(collectionView:UICollectionView,ViewForSupplementElementOfKind-kind:String,位于indexPath:indexPath)->UICollectionReusableView{
让header=collectionView.dequeueReusableSupplementaryView(ofKind:kind,带ReuseIdentifier:headerIdentifier,for:indexPath)作为!UserProfileHeader
header.delegate=self
header.user=self.user
navigationItem.title=用户?.username
回流集管
}
func fetchCurrentUserData(){
guard let currentUid=Auth.Auth().currentUser?.uid else{return}
设db=Firestore.Firestore()
让userReference=db.collection(“profile\u data”).document(currentUid)
userReference.getDocument{(快照)位于
guard let dictionary=snapshot.value as?dictionary else{return}
让uid=snapshot.key
让user=user(uid:uid,dictionary:dictionary)
self.user=用户
self.navigationItem.title=user.username
self.collectionView?.reloadData()
}
}  

更新:在
fetchCurrentUserData
中修改整个代码,以与新的Firbase保持一致

在方法
fetchCurrentUserData
中,闭包
userReference.getDocument
有两个参数,而不是一个。只需修改这一参数,您就可以了,下面是如何:

func fetchCurrentUserData() {
    guard let currentUid = Auth.auth().currentUser?.uid else { return }
    let db = Firestore.firestore()
    let userReference = db.collection("profile_data").document(currentUid)

    userReference.getDocument { (document, error) in
        if let document = document, document.exists {
            let dictionary = document.data()
            let uid = snapshot.key
            let user = User(uid: uid, dictionary: dictionary)
            self.user = user
            self.navigationItem.title = user.username
            self.collectionView?.reloadData()
        }
    }
}

我以前也尝试过类似的方法,但得到的错误与解决方案产生的错误相同,即“表达式类型‘Dictionary’在没有更多上下文的情况下不明确”和“类型‘DocumentSnapshot’的值没有成员‘key’”。如何修复这些错误?请检查更新。我已对代码进行了一定程度的更新,但我不熟悉您的数据库结构,我也添加了文档链接。请检查以查看新的更改,您的代码似乎已过时。我同意这不是最佳格式。我最初尝试使用您在更新中发送的Firestore格式,但我无法使其工作。我已更新了fetchCurrentUserData的原始函数。我想我真的应该尝试更新我的User和UserProfileHeader视图。您对我应该如何做有什么建议吗?