嘿,我是Firebase的新手,我从数据库获取数据时出现问题,我在这一行上收到错误:
let snapshotValue = snapshot.value as! [String: AnyObject]
你能帮我解决这个问题吗?
错误说:
Could not cast value of type '__NSCFString' (0x1a8e00d50) to 'NSDictionary' (0x1a8e01c78).
事件控制器:
init(snapshot: FIRDataSnapshot) {
key = snapshot.key
let snapshotValue = snapshot.value as! [String: AnyObject]
name = snapshotValue["name"] as! String
addedByUser = snapshotValue["addedByUser"] as! String
startTime = snapshotValue["startTime"] as! Double
endPickerTime = snapshotValue["endPickerTime"] as! Double
longtitude = snapshotValue["longtitude"] as! Double
latitude = snapshotValue["latitude"] as! Double
eventImageStringUrl = snapshotValue["eventImageStringUrl"] as! String
eventId = snapshotValue["eventId"] as! String
eventDescription = snapshotValue["eventDescription"] as! String
completed = snapshotValue["completed"] as! Bool
ref = snapshot.ref
}
从数据库中获取数据:
override func viewDidLoad() {
super.viewDidLoad()
databaseHandle = databaseRef.child("events").observe(.childAdded, with: { (snapshot) in
var newItems: [Event] = []
for item in snapshot.children {
let event = Event(snapshot: item as! FIRDataSnapshot)
newItems.append(event)
}
})
数据库方案:
答案 0 :(得分:2)
将.childAdded
更改为.value
中的.observe
。