How do you convert URL to NSString when you get error: Can only store objects of type NSNumber, NSString, NSDictionary?

How do you convert URL to NSString when you get error: Can only store objects of type NSNumber, NSString, NSDictionary?

我的代码对于带有元数据的 pod 更新来说太旧了:

 let newImageRef = imageStorageRef.child(newPostRef.key!)
        let newImageRef1 = imageStorageRef.child(newPostRef1.key!)


        newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
            self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString
            newPostRef.setValue(self.imageDownloadURL as Any)


        })



        newImageRef1.putData(imageData).observe(.success, handler: {(snapshot) in
            self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString

            let keyToPost = Database.database().reference().child("people").child(uid).childByAutoId().key
            let f1: [String: Any] = [(keyToPost) : self.imageDownloadURL as Any]

            newPostRef1.updateChildValues(f1)

                           })

有人提出了以下修复建议,但它产生了一个错误:只能存储类型为 NSNumber、NSString、NSDictionary 的对象。现在我打算更改设置值并将子值更新为 NSString。

 newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
              newImageRef.downloadURL { (url, error) in
                guard let downloadURL = url else {
                  return
                }
                newPostRef.setValue(url)
              }
            })



            newImageRef1.putData(imageData).observe(.success, handler: {(snapshot) in
                                                    newImageRef1.downloadURL { (url, error) in
                                                      guard let downloadURL = url else {
                                                        return
                                                      }
                                                      let keyToPost = Database.database().reference().child("people").child(uid).childByAutoId().key ?? "I am a default value"
                                                      let f1: [String: Any] = [(keyToPost) : newImageRef1.downloadURL as Any]

                                                      newPostRef1.updateChildValues(f1)
                                                    }
                                                  })

您可以从 downloadURL that it yields a URL object. That has an absoluteString 属性 的 APi 文档中看到您可以添加到 Firebase。

let f1: [String: Any] = [
    (keyToPost) : downloadURL.absoluteString
]