gpt4 book ai didi

swift - 无法将 NSTaggedPointerString 类型的值转换为 NSDictionary

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:41 24 4
gpt4 key购买 nike

我正在尝试为我的结构分配 Firebase 值:var productsArray = [Product]() 但是我有一个小错误:

Could not cast value of type 'NSTaggedPointerString' to 'NSDictionary'.

我知道我不能直接分配它们,所以这就是我这样转换的原因:

self.snusProductTitle = (snapshot.value! as! NSDictionary)["Products"] as! String

和转换:

func toAnyObject() -> [String: Any] {

return ["Products": snusProductTitle as Any]
}

像这样我追加:

let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observeSingleEvent(of: .childAdded, with: { (posts) in
self.productsArray.removeAll()
var newPostsArray = [Product]()
for post in posts.children {
print(posts.value)//Look below image
let newPost = Product(snapshot: post as! FIRDataSnapshot)
newPostsArray.insert(newPost, at: 0)
}

self.productsArray = newPostsArray
self.tableView.reloadData()

}) { (error) in
print(error.localizedDescription)
}

这是最小的产品结构:

class Product: NSObject {
var snusProductTitle: String!


var ref: FIRDatabaseReference?

init(snusProductTitle: String) {
self.snusProductTitle = snusProductTitle
self.ref = FIRDatabase.database().reference()
}



init(snapshot: FIRDataSnapshot){
self.snusProductTitle = (snapshot.value! as! NSDictionary)["Products"] as! String
}

func toAnyObject() -> [String: Any] {
return ["Products": snusProductTitle as Any]
}
}

enter image description here

谷歌搜索时,我找不到任何解决方案。

最佳答案

.childAdded 一次给出 FIRDataSnapshot ...所以不需要为此循环...您只需要在您的结构中传递当前的 child 。

 self.productsArray.removeAll()
var newPostsArray = [Product]()

let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand").queryEqual(toValue: brandName)
ref.observe(FIRDataEventType.childAdded, with: { (posts) in

let newPost = Product(snapshot: posts as! FIRDataSnapshot)
newPostsArray.insert(newPost, at: 0)

self.productsArray = newPostsArray
self.tableView.reloadData()

}) { (error) in
print(error.localizedDescription)
}

关于swift - 无法将 NSTaggedPointerString 类型的值转换为 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459460/

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