Swift - NSArray元素无法匹配Swift数组元素类型

时间:2017-09-02 15:14:29

标签: swift firebase nsarray

我从firebase节点获取数据。但是在每个节点内部都有一个子节点(称为productList) See picture here

这段代码是我从firebase填充currentStores数组的地方。

let storesRef : DatabaseReference = dbRef.child("Services")

        storesRef.observe(DataEventType.childAdded, with: {(DataSnapshot) in

            if (DataSnapshot.value as? [String: AnyObject]) != nil {
                let dictionary = DataSnapshot.value as? [String: AnyObject]

                let store = Stores(storeName: dictionary?["name"] as! String,
                                   id: dictionary?["id"] as! String,
                                   type: dictionary?["type"] as! Int,
                                   averageScore: dictionary?["averageScore"] as! Float,
                                   avatarUrl: dictionary?["avatarUrl"] as! String,
                                   waitingTimeMin: dictionary?["waitingTimeMin"] as! Int,
                                   waitingTimeMax: dictionary?["waitingTimeMax"] as! Int,
                                   isService: dictionary?["service"] as! Bool,
                                   products: dictionary?["productList"] as! [Product])

                self.currentStores.append(store)
                self.hideLoading()
                DispatchQueue.main.async {
                    self.mainTableView.reloadData()
                }
            }
        })

这是商店的构造函数:

 var products = [Product]()

init(storeName: String, id: String, type: Int, averageScore: Float, avatarUrl: String, waitingTimeMin: Int, waitingTimeMax: Int, isService: Bool, products: [Product] = [Product]() ) {
        self.storeName = storeName
        self.id = id
        self.type = type
        self.averageScore = averageScore
        self.avatarUrl = avatarUrl
        self.waitingTimeMin = waitingTimeMin
        self.waitingTimeMax = waitingTimeMax
        self.isService = isService
        self.products = products
    }

然后,当我从store.productList填充单元格数据时,我收到错误:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellReuse = "productCell"

        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuse, for: indexPath) as! ProductTableViewCell

        //// THIS LINE: (NSArray element failed to match the Swift Array Element type)
       cell.productNameLabel.text = store.products[indexPath.row].name

        return cell
    }

0 个答案:

没有答案