gpt4 book ai didi

ios - swift 火力基地 : Cannot store object of type _SwiftValue at fileURL

转载 作者:搜寻专家 更新时间:2023-11-01 06:37:20 26 4
gpt4 key购买 nike

我最近刚刚更新到 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)
})
}
}

最佳答案

经过一番尝试后,我终于弄明白了。在新的 Swift 3 中,Firebase 希望您对所写的内容非常具体。此外,在此过程中不能有任何可选类型。

只需将 String(self.fileURL) 添加到 setValue 函数的末尾就足以解决这个问题。希望这可以帮助其他人!请发表评论以获得进一步的解释。

关于ios - swift 火力基地 : Cannot store object of type _SwiftValue at fileURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39950864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com