如何正确上载用户profileImage,尝试更新用户profileImage时出现错误。
如何解决此错误?
线程1:致命错误:展开一个可选值时意外发现为零'
**URLSession.shared.dataTask(with: url!) { (data, response, error) in**
这是完整代码
DatabaseRef.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? "username"
let dict: Dictionary<AnyHashable, Any>? = ["photo": "url of photo"]
if let profileImageUrl = dict?["photo"] as? String {
let url = URL(string: profileImageUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil{
print(error!)
return
}
您应始终避免强行打开包装。相反,您可以检查url
是否不为零,并且仅在url具有某些值时才发送请求。