Swift认为Structure中的变量是可选的

时间:2016-10-10 11:53:11

标签: ios swift firebase firebase-storage

我基于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. ...是我的单身人士。

  1. 但是,它会将"postImageUrl"设置为Firebase,如
  2.   

    "可选(https://firebasestorage.googleapis.com/v0/b/"

    我不明白为什么,因为我没有选项。图像来自imagePicker可选吗?

2 个答案:

答案 0 :(得分:0)

尝试: -

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())

        }

答案 1 :(得分:0)

我认为使用if let解包并测试块的所有必需前提条件时,swift效果最佳。

在此示例中,您需要一个未包装的图像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())     
}