无法将类型NSTaggedPointerString的值强制转换为NSDictionary [英] Can not cast value of type NSTaggedPointerString to NSDictionary

查看:114
本文介绍了无法将类型NSTaggedPointerString的值强制转换为NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to assign Firebase values to my struct: var productsArray = [Product]() however I have a little error:

无法将类型'NSTaggedPointerString'的值强制转换为 "NSDictionary".

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

我知道我不能直接分配它们,所以这就是为什么我要像这样投射:

I know that I can't assign them directly so that is why I am casting like this:

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]
    }
}

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

While googling, I couldn't find any solution.

推荐答案

.childAdded一次提供FIRDataSnapshot ...因此无需为此循环..您只需要在结构中传递当前子级

.childAdded gives FIRDataSnapshot at a time ... so no need to loop for this .. you just need to pass the current child in your structure.

 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)
    }

这篇关于无法将类型NSTaggedPointerString的值强制转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆