Ios 如何在SwiftUI中使用搜索栏过滤对象数组?

Ios 如何在SwiftUI中使用搜索栏过滤对象数组?,ios,swiftui,Ios,Swiftui,我有一个对象数组,我想根据搜索文本筛选出列表。这就是我到目前为止所做的: CountryListView: func getFilteredData(data: [CountryStats], searchText: String) -> [CountryStats] { if searchText.isEmpty == false { return data.filter { $0.country == searchText } } else {

我有一个对象数组,我想根据搜索文本筛选出列表。这就是我到目前为止所做的:

CountryListView:

func getFilteredData(data: [CountryStats], searchText: String) -> [CountryStats] {
    if searchText.isEmpty == false {
        return data.filter { $0.country == searchText }
    }
    else {
        return data
    }
}


struct CountryMasterView: View {

    @Environment(\.colorScheme) var colorScheme
    @State private var searchText: String = ""
    var countryStats: [CountryStats]

    var body: some View {
        NavigationView {
            VStack {
                SearchBar(text: $searchText)
             List {
                ForEach(countryStats.filter {
                    self.searchText.isEmpty ? true : getFilteredData(data: countryStats, searchText: searchText)
                }, id: \.self) { stat in
                if stat.country as AnyObject !== "All" as AnyObject {

                    NavigationLink(destination: CountryDetailView(countryStats: stat)) {
                        HStack {
                            URLImage(URL(string: stat.flag ?? "")!) { proxy in
                            proxy.image
                                .resizable()
                                .scaledToFit()
                                .clipped()

                            }
                                .frame(width: 50.0, height: 35.0)

                            VStack(alignment: .leading) {
                                Text(stat.country)
                                    .font(.headline)
                                Text("Total cases: \(stat.cases)")
                                    .font(.subheadline)
                            }
                        }

                  }
                }

                }


            }.navigationBarTitle("Cases By Country")
        }
        }
    }
}
这是我的模型:

struct AllCountryStats: Identifiable, Decodable {
    let id = UUID()
    var data: [CountryStats]
    let message: String
}

struct CountryStats: Identifiable, Decodable, Hashable {
    let id = UUID()
    let active, cases: Int
    let country: String
    let deaths, recovered, todayCases, todayDeaths: Int
    let flag: String?
    let updated: Double
}
这是搜索视图:

struct SearchBar: UIViewRepresentable {
    @Binding var text: String

    class Coordinator: NSObject, UISearchBarDelegate {
        @Binding var text: String

        init(text: Binding<String>) {
            _text = text
        }

        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
        }

    }

    func makeCoordinator() -> SearchBar.Coordinator {
        return Coordinator(text: $text)
    }

    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        searchBar.searchBarStyle = .minimal
        return searchBar
    }

    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.text = text
    }

}
结构搜索栏:UIViewRepresentable{ @绑定变量文本:字符串 类协调器:NSObject,UISearchBarDelegate{ @绑定变量文本:字符串 init(文本:绑定){ _文本=文本 } func searchBar(searchBar:UISearchBar,textDidChange searchText:String){ 文本=搜索文本 } } func makeCoordinator()->SearchBar.Coordinator{ 退货协调员(文本:$text) } func makeUIView(上下文:UIViewRepresentableContext)->UISearchBar{ 让searchBar=UISearchBar(帧:.0) searchBar.delegate=context.coordinator searchBar.searchBarStyle=.minimal 返回搜索栏 } func updateUIView(uiView:UISearchBar,context:UIViewRepresentableContext){ uiView.text=文本 } } 所以,我想有一个搜索栏来过滤国家


我有没有遗漏什么,或者我做得不对?我得到错误
无法推断复杂的闭包返回类型;添加显式类型以消除歧义

您的代码存在一些问题

首先,我将stat.country替换为AnyObject!==作为任何对象的“全部”by
stat.country!=“全部”

然后,您在
ForEach
中的过滤器很奇怪。我使用
.contains
而不是字符串相等来简化和改进它。它可以在不键入全名的情况下搜索国家

以下是一个供您参考的工作机构:

var主体:一些视图{
导航视图{
VStack{
搜索栏(文本:$searchText)
名单{
ForEach(countryStats.filter{$0.country.contains(searchText)| | searchText.isEmpty}){stat in
如果统计国家!=“全部”{
导航链接(目的地:CountryDetailView(countryStats:stat)){
HStack{
URL图像(URL(字符串:stat.flag??)!{proxy in
代理映像
.可调整大小()
.scaledofit()
.clipped()
}
.框架(宽度:50.0,高度:35.0)
VStack(对齐:。前导){
文本(统计国家)
.font(.headline)
文本(“总案例:\(统计案例)”)
.font(.subheadline)
}
}
}
}
}
}.navigationBarTitle(“按国家分列的案例”)
}
}
}

我将stat.country添加为AnyObject的原因!==“All”作为AnyObject是因为我得到错误
无法将“String”类型的值转换为预期的参数类型“AnyObject?”。
不确定原因。但是搜索正在进行,谢谢@GunasaiGarapati可能是因为你写了
==而不是
=