[英]Swift thinks that variable in Structure is optional
我做了一个简单的基于Firebase的后期制作项目。 我将帖子保存到Firebase中,如下所示:
let data = UIImageJPEGRepresentation(newPostImageView.image!, 0.5)//Take photo from imageview
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"//Define post image path
let postId = "\(currentUser.generalDetails.uid)\(NSUUID().uuidString)"//Generate postId
let imagePath = "postImages\(postId)/postPic.jpg"
storageRef.child(imagePath).put(data!, metadata: metadata) { (metadata, error) in
if error == nil {
let postRef = self.databaseRef.child("posts").childByAutoId()
let post = Post(postImageUrl: String(describing: metadata?.downloadURL()), profileImageUrl: self.currentUser.generalDetails.profileImageURL, postId: postId, content: self.newPostTextView.text, username: self.currentUser.generalDetails.userName)
postRef.setValue(post.toAnyObject())
}else {
print(error!.localizedDescription)
}
}
dismiss(animated: true, completion: nil)
currentUser.generalDetails. ...
currentUser.generalDetails. ...
是我的单身。
"postImageUrl"
为Firebase 我不明白为什么,因为我没有可选的。 来自imagePicker的图像是可选的吗?
尝试:-
let metaURLString = "\(metaData!.downloadURL())"
if metaURLString != "" {
let post = Post(postImageUrl: metaURLString, profileImageUrl: self.currentUser.generalDetails.profileImageURL, postId: postId, content: self.newPostTextView.text, username: self.currentUser.generalDetails.userName)
postRef.setValue(post.toAnyObject())
}
我想使用的时候迅速的效果最好if let
拆开包装和测试所需的所有先决条件块。
在此示例中,您需要一个未包装的图像URL作为不为空的字符串。
可以进行所有设置。
if let downloadUrl = metadata?.downloadURL() { // Unwrap the URL.
if let imageUrl = downloadUrl.absoluteString { // URL as a string.
if !imageUrl.isEmpty { // URL must not be empty.
}
}
}
幸运的是,这一切都可以在一行上完成。
if let imageUrl = metadata?.downloadURL()?.absoluteString, !imageUrl.isEmpty {
}
放在上下文中,这给出了以下内容。
storageRef.child(imagePath).put(data!, metadata: metadata) { (metadata, error) in
if let imageUrl = metadata?.downloadURL()?.absoluteString, !imageUrl.isEmpty {
let postRef = self.databaseRef.child("posts").childByAutoId()
let post = Post(postImageUrl: imageUrl,
profileImageUrl: self.currentUser.generalDetails.profileImageURL,
postId: postId,
content: self.newPostTextView.text,
username: self.currentUser.generalDetails.userName)
postRef.setValue(post.toAnyObject())
} else if error != nil {
print(error!.localizedDescription)
} else {
print("Unknown error")
}
}
最后,我们可以使用guard
声明提前退出。 这减少了代码主行中的缩进级别。
storageRef.child(imagePath).put(data!, metadata: metadata) { (metadata, error) in
guard let imageUrl = metadata?.downloadURL()?.absoluteString, !imageUrl.isEmpty else {
print(error?.localizedDescription ?? "Unknown error")
return
}
let postRef = self.databaseRef.child("posts").childByAutoId()
let post = Post(postImageUrl: imageUrl,
profileImageUrl: self.currentUser.generalDetails.profileImageURL,
postId: postId,
content: self.newPostTextView.text,
username: self.currentUser.generalDetails.userName)
postRef.setValue(post.toAnyObject())
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.