2017-05-14 32 views
-2

我有,當我從下載火力這裏圖像的問題,下載圖像是我的代碼:問題「意外發現無」與火力迅速

func getuser(){ 

FIRDatabase.database().reference().child("users").child(userID!).child("credentials").observeSingleEvent(of: .value, with: { (snapshot) in 

     let value = snapshot.value as? NSDictionary 
     let name = value?["name"] as! String 
     let email = value?["email"] as! String 
     let profilePicLink = value?["profilePicLink"] as? String ?? "" 


     Variables.userName = name as String 
     Variables.userEmail = email as String 
     self.username.text = Variables.userName 
     print(profilePicLink) 
     // Create a storage reference from the URL 
     let storageRef = self.storage.reference(forURL: profilePicLink) 
     // Download the data, assuming a max size of 1MB (you can change this as necessary) 
     storageRef.data(withMaxSize: 1 * 1000 * 1000) { (data, error) -> Void in 

      let pic = UIImage(data: data!) 
      self.img.image = pic 

     } 



    }) 


} 

我得到這個錯誤:

==> this the link as printed https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a 
fatal error: unexpectedly found nil while unwrapping an Optional value 

是我得到的是該行中的錯誤

let storageRef = self.storage.reference(forURL: profilePicLink) 
下面

是我的快照讀數:

snap (credentials) { 
    email = "[email protected]"; 
    mobile = 50955514; 
    name = Bilal; 
    profilePicLink = "https://firebasestorage.googleapis.com/v0/b/eswitch-72b56.appspot.com/o/usersProfilePics%2FKYe6fIQReNM8Oog4ELOdRLsC99J3?alt=media&token=6f2392bc-d35b-4ebc-b2c1-2dc34bc4b95a"; 
    role = user; 
} 

感謝

這裏是解決方案:

該行添加到FUNC viewDidLoad中()

storage = FIRStorage.storage() 
+0

你確定'self.storage'被初始化嗎? –

+0

爲什麼如果profilePicLink變量是可選的或者不是self.storage.reference,那麼這很重要?也許self.storage沒有定義,或者問題在其他地方。 – shubhamagg

+0

哇我怎麼錯過了!謝謝!我將storage = FIRStorage.storage()添加到func viewDidLoad() –

回答

0

你需要確保self.storage被初始化。

storage = FIRStorage.storage()