如何修复Xcode中的“'viewbusdata'类型的值没有下标”错误

时间:2019-08-04 10:01:42

标签: swift firebase firebase-realtime-database

我正在尝试在表格视图中添加搜索功能。我正在从Firebase调用数据,但在filterecontent函数上收到错误 错误消息是“'viewbusdata'类型的值没有下标”

这是我的Firebase结构 firebase structure

register.styl

here is the error message

searchcontroller.searchResultsUpdater = self
searchcontroller.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tbv.tableHeaderView = searchcontroller.searchBar
tbv.dataSource = self
tbv.delegate = self
ref = Database.database().reference()

ref.child("BusData").queryOrdered(byChild: "BusData").observe(.childAdded, with: {(snapshot)in

    if snapshot.childrenCount>0{
        self.viewdata.removeAll()

        for BusData in snapshot.children.allObjects as! [DataSnapshot]{
            let BusDataobject = BusData.value as? [String: AnyObject]
            let Deperture1 = BusDataobject?["Deperture"]
            let Arrival1 = BusDataobject?["Arrival"]
            let Time1 = BusDataobject?["Time"]
            let Price1 = BusDataobject?["Price"]
            let id1 = BusDataobject?["id"]

            let BusData = viewbusdata(id1: id1 as! String?, Deperture1: Deperture1 as! String?, Arrival1: Arrival1 as! String?, Time1: Time1 as! String?, Price1: Price1 as! String?)

            self.viewdata.append(BusData)
        }
        //self.tbv.reloadData()
    }

    self.tbv.insertRows(at: [IndexPath(row: self.viewdata.count-1, section: 0)], with: UITableViewRowAnimation.automatic)

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if searchcontroller.isActive && searchcontroller.searchBar.text !=
        ""{
        return filteruser.count
    }
    return self.viewdata.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tbv.dequeueReusableCell(withIdentifier: "viewcell2", for: indexPath) as! userbusTableViewCell

    let BusData : viewbusdata

    if searchcontroller.isActive && searchcontroller.searchBar.text !=
        ""{
        BusData = filteruser[indexPath.row]
    }
    else
    {
        BusData = self.viewdata[indexPath.row]
    }

    cell.label1.text = BusData.Deperture1
    cell.label2.text = BusData.Arrival1
    cell.label3.text = BusData.Time1
    cell.label4.text = BusData.Price1

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "showdata", sender: nil)
}

func updateSearchResults(for searchController: UISearchController) {
    filterecontent(searchText: self.searchcontroller.searchBar.text!)
}

1 个答案:

答案 0 :(得分:0)

首先,您的代码很难阅读。请遵守命名约定,变量和属性名称以小写字母(busData开头,结构以大写字母(Viewbusdata)开头。

发生错误是因为busData是一个结构,而不是一个字典。您可以使用点表示法获取值。

func filterecontent(searchText: String)
{
    self.filteruser = self.viewdata.filter { busData in

        guard let deperture = busData.deperture else { return false }
        return deperture.lowercased().contains(searchText.lowercased())

    }
    tbv.reloadData()
}

或者稍微 swiftier

func filterecontent(searchText: String)
{
    self.filteruser = self.viewdata.filter { $0.deperture?.range(of: searchText, options: .caseInsensitive) != nil }
    tbv.reloadData()
}

另一个错误的做法是这种毫无意义的 force-unwrap-an-optional-to-an-optional as! String?。不要那样做。

如果结果是可选的,则写as? String,否则写as! String