Ios 带有自定义类的搜索栏

Ios 带有自定义类的搜索栏,ios,uitableview,uisearchbar,Ios,Uitableview,Uisearchbar,我是iOS开发的初学者,当我试图在UITableView中使用UISearchBar时遇到了一个问题。 我不知道如何在使用搜索栏过滤数据的表视图中使用自定义类。拜托,你能解决这个问题吗?下面是自定义类: // Data.swift // SearchBarWithTableView class Food { var FoodName:String = "" var FoodQuantity:String = "" var FoodGroup:String = "" var FoodImag

我是iOS开发的初学者,当我试图在
UITableView
中使用
UISearchBar
时遇到了一个问题。 我不知道如何在使用搜索栏过滤数据的表视图中使用自定义类。拜托,你能解决这个问题吗?下面是自定义类:

//  Data.swift
//  SearchBarWithTableView

class Food {
var FoodName:String = ""
var FoodQuantity:String = ""
var FoodGroup:String = ""
var FoodImage:String = ""
var FoodCarbs:Int = 0
var FoodCalories:Int = 0

init(foodName:String,foodGroup:String,foodQuantity:String,foodImage:String,foodCarbs:Int,foodCalories:Int) {
    self.FoodName = foodName
    self.FoodQuantity = foodQuantity
    self.FoodGroup = foodGroup
    self.FoodImage = foodImage
    self.FoodCarbs = foodCarbs
    self.FoodCalories = foodCalories
}}
ViewController.swift

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{

    @IBOutlet weak var SearchBarTableView:UITableView!
    @IBOutlet weak var SearchBar:UISearchBar!




    var searchActive:Bool=false
    var filteredData=[Food]()

    let allData:[Food]=[Food(foodName: "Rice", foodGroup: "Beans", foodQuantity: "5 scope",foodImage:"1S.jpg", foodCarbs:15,foodCalories: 80),
                        Food(foodName: "Apple", foodGroup: "Frutie", foodQuantity: "One",foodImage:"S-2.jpg", foodCarbs: 15, foodCalories: 80),
                        Food(foodName: "ban 1",foodGroup: "Beans",foodQuantity:"One",foodImage:"3S.jpg",foodCarbs: 15,foodCalories: 80),
                        Food(foodName: "ban 2", foodGroup: "Beans", foodQuantity: "half pieace",foodImage:"4-S.jpg", foodCarbs: 25, foodCalories: 140)]



    override func viewDidLoad() {

        super.viewDidLoad()

        SearchBarTableView.delegate=self
        SearchBarTableView.dataSource=self
        SearchBar.delegate=self
        filteredData=allData
        print(filteredData.count)
        print("-----------")
        print(allData.count)
        SearchBar.returnKeyType=UIReturnKeyType.done
        definesPresentationContext=true
    }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell=tableView.dequeueReusableCell(withIdentifier: "cell")
                var Text:String
                if searchActive {
                    Text=filteredData[indexPath.row].FoodName
                }else{
                    Text=allData[indexPath.row].FoodName
                }
                cell?.textLabel?.text=filteredData[indexPath.row].FoodName
                return cell!
            }

        func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {`
            if SearchBar.text == nil || SearchBar.text == "" {
                searchActive=false
                view.endEditing(true)
                SearchBarTableView.reloadData()
            }else{
                searchActive=true
    /* here is the problem */
                 //   filteredData=allData.filter({$0 === searchBar.text!}) 
                }
            }
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            if searchActive {
                return filteredData.count
            }
            return allData.count
        }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
           if segue.identifier=="Go"{
            if let indexPath=SearchBarTableView.indexPathForSelectedRow{
                let destinationVC=segue.destination as! DetailsViewController
                destinationVC.namE=allData[indexPath.row].FoodName
                destinationVC.quntitY=allData[indexPath.row].FoodQuantity
                destinationVC.calorieS=allData[indexPath.row].FoodCalories
                destinationVC.carbS=allData[indexPath.row].FoodCarbs
            }
        }
      }
    }
使用上述方法

    func searchBar(_ searchBar: UISearchBar, textDidChange `searchText: String) {`
        if SearchBar.text == nil || SearchBar.text == "" {
            searchActive=false
            view.endEditing(true)
            SearchBarTableView.reloadData()
        }else{
            searchActive=true

                filteredData=allData.filter({$0.FoodName === searchBar.text!}) 
            SearchBarTableView.reloadData()


            }
        }
func updateSearchResults(for searchController: UISearchController) {
    if let searchText = searchController.searchBar.text, !searchText.isEmpty {
        self.filteredData = self.allData.filter{
            var found = false
            for str in ($0 as! NSDictionary) {
                if(str.key as! String == "foodName")
                {
                    var strin = str.value as! String;
                    strin = strin.lowercased();
                    let search = searchText.lowercased()
                    found = strin.contains(search);
                }                    
            }
            return found
        }
    }
    else {
        self.Array = response as! [Any]
    }
    tableView.reloadData()
}