出现错误时,如何将URL转换为NSString。只能存储NSNumber, NSString, NSDictionary类型的对象?

问题描述 投票:0回答:1

我的代码太老了,不适合用元数据进行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)
                                                    }
                                                  })
swift firebase firebase-realtime-database nsstring firebase-storage
1个回答
0
投票

你可以从APi的文档中看到 下载网址 它产生了 网址 对象。 它有一个 绝对字符串 属性,你可以添加到Firebase。

let f1: [String: Any] = [
    (keyToPost) : downloadURL.absoluteString
]
© www.soinside.com 2019 - 2024. All rights reserved.