[英]Swift thinks that variable in Structure is optional
I made simple post making project based on Firebase. 我做了一个简单的基于Firebase的后期制作项目。 I save post into Firebase like this:
我将帖子保存到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. ...
is singleton I have. currentUser.generalDetails. ...
是我的单身。
"postImageUrl"
into Firebase like "postImageUrl"
为Firebase "Optional( https://firebasestorage.googleapis.com/v0/b/ "
“可选( https://firebasestorage.googleapis.com/v0/b/ ”
I do not understand why, since I do not have optionals. 我不明白为什么,因为我没有可选的。 Is image from imagePicker optional?
来自imagePicker的图像是可选的吗?
Try:- 尝试:-
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())
}
I think swift works best when using if let
to unwrap and test all needed preconditions for a block. 我想使用的时候迅速的效果最好
if let
拆开包装和测试所需的所有先决条件块。
In this example you need an unwrapped image URL as a string that is not empty. 在此示例中,您需要一个未包装的图像URL作为不为空的字符串。
It's possible to set all of this up. 可以进行所有设置。
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.
}
}
}
Luckily, this can all be done on one line. 幸运的是,这一切都可以在一行上完成。
if let imageUrl = metadata?.downloadURL()?.absoluteString, !imageUrl.isEmpty {
}
Placed in context, this gives the following. 放在上下文中,这给出了以下内容。
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")
}
}
Finally, we can use a guard
statement to exit early. 最后,我们可以使用
guard
声明提前退出。 This reduces the level of indent in the main line of code. 这减少了代码主行中的缩进级别。
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.