Ios Swift 3 Firebase数据检索错误:无法转换类型为'__NSCFString';(0x1a8e00d50)至';NSDictionary';(0x1a8e01c78)
嘿,我是Firebase的新手,从数据库获取数据时遇到问题,我在这一行遇到错误:Ios Swift 3 Firebase数据检索错误:无法转换类型为'__NSCFString';(0x1a8e00d50)至';NSDictionary';(0x1a8e01c78),ios,xcode,firebase,swift3,firebase-realtime-database,Ios,Xcode,Firebase,Swift3,Firebase Realtime Database,嘿,我是Firebase的新手,从数据库获取数据时遇到问题,我在这一行遇到错误: let snapshotValue = snapshot.value as! [String: AnyObject] 你能帮我解决这个问题吗 错误显示: Could not cast value of type '__NSCFString' (0x1a8e00d50) to 'NSDictionary' (0x1a8e01c78). init(snapshot: FIRDataSnapshot)
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
}
事件控制器:
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)
}
})
数据库方案:
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
}
更改
.child将添加到中的.value
。观察 编译器说,snapshot.value
是一个字符串。请尝试更改.childAdded to.value in.observe。它有帮助吗?似乎FIRDataSnapshot值
是String
@VladPulichev谢谢,你解决了我的问题