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

2023-12-06

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

无法将“NSTaggedPointerString”类型的值转换为 'NS字典'。

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

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 gives FIRDataSnapshot一次......所以不需要为此循环......你只需要传递结构中的当前子元素。

 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)
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将 NSTaggedPointerString 类型的值转换为 NSDictionary 的相关文章

随机推荐