问题
So for the second last block(newImageRef.putData), I can define downloadURL as String?, but then I get __SwiftValue error at the last block (newImageRef.putData1).
And even defining downloadURL as a string seems dangerous because looking at the documentation https://firebase.google.com/docs/reference/swift/firebasestorage/api/reference/Classes/StorageMetadata it seems to be used as a URL integral to the function.
Other than these terminal error, there is a yellow error that warns Value 'downloadURL' was defined but never used; consider replacing with boolean test
func save() {
let uid = Auth.auth().currentUser!.uid
let newPostRef = Database.database().reference().child("people").child(uid).child("PhotoPosts")
let newPostRef1 = Database.database().reference().child("people").child(uid).child("PhotoPosts1")
let newPostKey = newPostRef.key
if let imageData = image.jpegData(compressionQuality: 0.001){
let imageStorageRef = Storage.storage().reference().child("images").child(uid)
let newImageRef = imageStorageRef.child(newPostRef.key!)
let newImageRef1 = imageStorageRef.child(newPostRef1.key!)
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)
}
})
Below is what I had it at before but changed due to pod updates
newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString
newPostRef.setValue(self.imageDownloadURL as Any)
})
来源:https://stackoverflow.com/questions/61594804/cannot-store-nsurl-and-swiftvalue-can-only-store-objects-of-type-nsnumber-ns