我刚刚更新了Swift 3.0语法和XCode 8.0。我在更新后将所有内容恢复原状后遇到了一些麻烦但是我无法解决的一件事是写作时数据(特别是图像)到数据库我收到以下错误:
Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(setValue:) Cannot store object of type _SwiftValue at fileURL. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'
这是我目前的写作过程。您将提供的任何帮助将不胜感激!
let roomRef = FIRDatabase.database().reference()
class Data {
static let dataService = Data()
fileprivate var _BASE_REF = roomRef
fileprivate var _ROOM_REF_ = roomRef.child("rooms")
fileprivate var _BASE_REF2 = roomRef
fileprivate var _ROOM_REF_2 = roomRef.child("contents")
var BASE_REF: FIRDatabaseReference {
return _BASE_REF
}
var ROOM_REF: FIRDatabaseReference {
return _ROOM_REF_
}
var storageRef: FIRStorageReference {
return FIRStorage.storage().reference()
}
var fileURL: String!
func createNewPost(_ user: FIRUser, title: String, author: String, date: String, story: String, data: Foundation.Data) {
let filePath = "\(user.uid)/\(Int(Date.timeIntervalSinceReferenceDate))"
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"
storageRef.child(filePath).put(data, metadata: metaData) { (metaData, error) in
if let error = error {
print("Error Uploading: \(error.localizedDescription)")
return
}
//creating the URL
self.fileURL = metaData!.downloadURLs![0].absoluteString
if let user = FIRAuth.auth()?.currentUser {
let idRoom = self.BASE_REF.child("rooms").childByAutoId()
idRoom.setValue(["title": title, "author": author, "date": date, "story": story, "ThumbnailURLFromStorage": self.storageRef.child(metaData!.path!).description, "fileURL": self.fileURL])
}
}
}
func fetchData(_ callback: @escaping (Room) -> ()) {
Data.dataService.ROOM_REF.observe(.childAdded, with: { (snapshot) in
let room = Room(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callback(room)
})
}
}
答案 0 :(得分:3)
经过一段时间的游戏,我能够解决这个问题。在新的Swift 3中,Firebase希望您对所写的内容非常具体。此外,在此过程中不可能有任何选项。
只需将String(self.fileURL)
添加到setValue函数的末尾即可解决此问题。希望这可以帮助别人!请评论进一步解释。