簡體   English   中英

Swift認為Structure中的變量是可選的

[英]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. ...是我的單身。

  1. 但是,它將"postImageUrl"為Firebase

“可選( https://firebasestorage.googleapis.com/v0/b/

我不明白為什么,因為我沒有可選的。 來自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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM