致命错误:在从Firebase检索数据时解包可选值时意外发现nil

时间:2017-07-01 05:27:01

标签: firebase swift3 firebase-realtime-database

你好所有m从firebase检索数据但是我得到的错误我不知道从哪里来的coz打印出来给我的数据但是当它回到标签时它给我错误。希望得到帮助。谢谢。

Database Snapshot

代码:

import UIKit
import FirebaseDatabase
import Firebase
import FirebaseAuth

class ProfileViewController: UIPageViewController 
{
    @IBOutlet weak var profileimageview: UIImageView!
    @IBOutlet weak var usernameText: UILabel!
    @IBOutlet weak var displaynameText: UILabel!

    var databaseref:DatabaseReference!

    override func viewDidLoad() {
       super.viewDidLoad()

       databaseref = Database.database().reference()

       if let userID = Auth.auth().currentUser?.uid {
        databaseref.child("profile").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
            let dictionary = snapshot.value as? NSDictionary

            let username = dictionary?["username"] as? String ?? "username"
            print(username)
            if let profileImageURL = dictionary?["photo"] as? String{

                let url = URL(string: profileImageURL)

                URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
                    if error != nil{
                        print(error!)
                        return
                    }
                    DispatchQueue.main.async {
                        self.profileimageview.image = UIImage(data: data!)
                    }
                }).resume()
            }
            self.usernameText.text = username
            self.displaynameText.text = username

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

}

}

这是我的控制台:**注意 youandme是我打印出的价值**

youandme
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

中收到此错误
self.usernameText.text = username

self.displaynameText.text = username

0 个答案:

没有答案