我将数据存储在Firebase实时数据库中,如下所示:
{
trivia: {
"0": "long text",
"1": "long text",
"2": "long text",
}
}
但是当我从数据库中导出json时,它呈现为如下数组:
{
trivia: ["long text", "long text", "long text"]
}
现在该怎么办?以下是我的快速代码,但出现此错误-“无法将类型'__NSArrayM'(0x106630b68)的值强制转换为'NSString'(0x104b802a8)。”
class TriviaViewController: UICollectionViewController {
var triviaList = [String]()
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
setupNavBar()
setupCollectionView()
fetchTriviaFromFirebase()
}
func fetchTriviaFromFirebase() {
ref = Database.database().reference()
ref?.child("trivia").observeSingleEvent(of: .value, with: { (data) in
self.triviaList.append(data.value as! String)
self.collectionView?.reloadData()
})
}
// Other functions - colectionview methds and other methods are below
}
答案 0 :(得分:0)
guard let value = data.value as? NSDictionary else { return }
guard let obj = value.allValues as? [String] else { return }
self.triviaList = obj
self.collectionView?.reloadData()