简体   繁体   English

从 firebase 调用数据时,void 函数中出现意外的非空返回值

[英]Unexpected non-void return value in void function while calling data from firebase

I am trying to call data from firebase that changes the image view to be that of what is stored in the database.我正在尝试从 firebase 调用数据,该数据将图像视图更改为存储在数据库中的视图。 when I call my code, I see当我调用我的代码时,我看到

let image: UIImageView = {
    let uid = Auth.auth().currentUser?.uid
    Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String : Any] else {
            return }
        let user = MyUser(dictionary: dictionary as [String : AnyObject])
        let profileImageView = UIImageView()
        profileImageView.translatesAutoresizingMaskIntoConstraints = false
        profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 30
        profileImageView.clipsToBounds = true
        profileImageView.image = UIImage(named: "app")
        profileImageView.image=#imageLiteral(resourceName: "user")
        profileImageView.loadImageUsingCacheWithUrlString(user.profileImageUrl!)
        return profileImageView
    }, withCancel: { (err) in
        print("attempting to load information")
    })
    //  return profileImageView
}()

where the commented out return function is, I am getting the error Use of unresolved identifier 'profileImageView';注释掉的返回函数在哪里,我收到错误 Use of unresolved identifier 'profileImageView'; did you mean 'provideImageData'?你的意思是'provideImageData'? why?为什么? I would assume that since the return is within the closures, it would know what profileImageView is.我会假设由于返回在闭包内,它会知道 profileImageView 是什么。

Separate the image view and the real time database listener:分离图像视图和实时数据库监听器:

let imageView: UIImageView {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 30
        imageView.clipsToBounds = true
        return imageView
}

let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let dictionary = snapshot.value as? [String : Any] else {
            return }
        let user = MyUser(dictionary: dictionary as [String : AnyObject])
        // Set the image view's image
        imageView.image = // ...
    }, withCancel: { (err) in
        print("attempting to load information")
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM