gpt4 book ai didi

swift - 下标使用不明确(Swift 3)

转载 作者:行者123 更新时间:2023-11-28 11:02:29 25 4
gpt4 key购买 nike

对于此 Firebase 数据拉取,我在以下代码中错误地使用了下标,但我无法弄清楚我做错了什么。我得到一个错误:let uniqueID = each.value["Unique ID Event Number"] as! 下标使用不明确! Int 行。

// Log user in
if let user = FIRAuth.auth()?.currentUser {

let uid = user.uid
// values for vars sevenDaysAgo and oneDayAgo set here

...

let historyRef = self.ref.child("historyForFeedbackLoop/\(uid)")
historyRef.queryOrdered(byChild: "Unix Date").queryStarting(atValue: sevenDaysAgo).queryEnding(atValue: oneDayAgo).observeSingleEvent(of: .value, with: { snapshot in

if (snapshot.value is NSNull) {
print("user data not found")
}
else {

if let snapDict = snapshot.value as? [String:AnyObject] {

for each in snapDict {

// Save the IDs to array.
let uniqueID = each.value["Unique ID Event Number"] as! Int
self.arrayOfUserSearchHistoryIDs.append(uniqueID)
}

}
else{
print("SnapDict is null")
}
}
})
}

我尝试应用从中学到的知识 post ,但我无法弄清楚我错过了什么,因为我认为我是在让编译器知道它是什么类型的字典,其中包含“as?[String:AnyObject]”

任何想法或想法将不胜感激!

最佳答案

我处理数据的首选方式是尽可能晚地解包FIRDataSnapshot

ref!.observe(.value, with: { (snapshot) in
for child in snapshot.children {
let msg = child as! FIRDataSnapshot
print("\(msg.key): \(msg.value!)")
let val = msg.value! as! [String:Any]
print("\(val["name"]!): \(val["message"]!)")
}
})

关于swift - 下标使用不明确(Swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090406/

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