Swift 3 Firebase数据检索错误:无法转换类型' __ NSCFString' (0x1a8e00d50)到' NSDictionary' (0x1a8e01c78)

时间:2017-05-17 09:46:45

标签: 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) {
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)
    }


  })

数据库方案:

enter image description here

1 个答案:

答案 0 :(得分:2)

.childAdded更改为.value中的.observe