通过大数组过滤 Swift
Filter through large Array Swift
我正在构建一个旅行计划应用程序,让用户可以使用选择器从本地 json 文件中 select 一个城市。
json 文件有 115K 个项目,如果我尝试过滤这么多项目,UI 会冻结。关于如何提高效率的任何建议。
如有任何帮助,我们将不胜感激。
import SwiftUI
struct AddTripView: View {
let cities = Bundle.main.decode([Destination].self, from: "cities.json")
@State private var selectedCity = 0
@State var searchText = ""
var body: some View {
Form {
Section {
Picker(selection: $selectedCity, label: Text("Select city")) {
SearchBar(text: $searchText)
ForEach(cities.filter {searchText.isEmpty ? true : [=11=].city.lowercased().hasPrefix(searchText.lowercased())}, id: \.self) {
Text([=11=].city)
}
}
}
}.navigationBarTitle("Create trip")
}
}
struct AddTripView_Previews: PreviewProvider {
static var previews: some View {
AddTripView()
}
}
JSON 例子
[{
"id": 1,
"city": "Zaranj",
"country": "Afghanistan"
},
{
"id": 2,
"city": "Taloqan",
"country": "Afghanistan"
},
{
"id": 3,
"city": "Shīnḏanḏ",
"country": "Afghanistan"
},
{
"id": 4,
"city": "Shibirghān",
"country": "Afghanistan"
},
{
"id": 5,
"city": "Shahrak",
"country": "Afghanistan"
}]
您真的需要 Picker 吗?如果不能,你可以试试这个例子。
让我们知道 UI 是否仍在挣扎。
struct AddTripView: View {
@State private var cities = [Destination]()
@State private var selectedCity: Destination?
@State private var searchQuery: String = ""
var body: some View {
VStack {
Text("\(cities.count)")
HStack {
TextField("city search", text: $searchQuery).padding(5)
.overlay(RoundedRectangle(cornerRadius: 15).stroke(Color.blue, lineWidth: 1))
.foregroundColor(.blue)
.frame(width: 160)
Button(action: {searchQuery = ""}) {
Image(systemName: "xmark.circle").font(.title)
}
}.padding(20)
Divider()
Text("Select city")
ScrollView {
LazyVStack (alignment: .leading) {
ForEach(cities.filter{self.searchFor([=10=].city)}.sorted(by: { [=10=].city < .city })) { city in
Text("\(city.city) \(city.country)")
.onTapGesture {
selectedCity = city
print("---> selectedCity: \(selectedCity?.city)")
}
}
}
}.onAppear() {
cities = Bundle.main.decode([Destination].self, from: "cities.json")
}
}
}
private func searchFor(_ txt: String) -> Bool {
return (txt.lowercased(with: .current).hasPrefix(searchQuery.lowercased(with: .current)) || searchQuery.isEmpty)
}
}
struct Destination: Codable, Identifiable, Equatable {
var id: Int
var city: String
var country: String
}
我正在构建一个旅行计划应用程序,让用户可以使用选择器从本地 json 文件中 select 一个城市。
json 文件有 115K 个项目,如果我尝试过滤这么多项目,UI 会冻结。关于如何提高效率的任何建议。
如有任何帮助,我们将不胜感激。
import SwiftUI
struct AddTripView: View {
let cities = Bundle.main.decode([Destination].self, from: "cities.json")
@State private var selectedCity = 0
@State var searchText = ""
var body: some View {
Form {
Section {
Picker(selection: $selectedCity, label: Text("Select city")) {
SearchBar(text: $searchText)
ForEach(cities.filter {searchText.isEmpty ? true : [=11=].city.lowercased().hasPrefix(searchText.lowercased())}, id: \.self) {
Text([=11=].city)
}
}
}
}.navigationBarTitle("Create trip")
}
}
struct AddTripView_Previews: PreviewProvider {
static var previews: some View {
AddTripView()
}
}
JSON 例子
[{
"id": 1,
"city": "Zaranj",
"country": "Afghanistan"
},
{
"id": 2,
"city": "Taloqan",
"country": "Afghanistan"
},
{
"id": 3,
"city": "Shīnḏanḏ",
"country": "Afghanistan"
},
{
"id": 4,
"city": "Shibirghān",
"country": "Afghanistan"
},
{
"id": 5,
"city": "Shahrak",
"country": "Afghanistan"
}]
您真的需要 Picker 吗?如果不能,你可以试试这个例子。 让我们知道 UI 是否仍在挣扎。
struct AddTripView: View {
@State private var cities = [Destination]()
@State private var selectedCity: Destination?
@State private var searchQuery: String = ""
var body: some View {
VStack {
Text("\(cities.count)")
HStack {
TextField("city search", text: $searchQuery).padding(5)
.overlay(RoundedRectangle(cornerRadius: 15).stroke(Color.blue, lineWidth: 1))
.foregroundColor(.blue)
.frame(width: 160)
Button(action: {searchQuery = ""}) {
Image(systemName: "xmark.circle").font(.title)
}
}.padding(20)
Divider()
Text("Select city")
ScrollView {
LazyVStack (alignment: .leading) {
ForEach(cities.filter{self.searchFor([=10=].city)}.sorted(by: { [=10=].city < .city })) { city in
Text("\(city.city) \(city.country)")
.onTapGesture {
selectedCity = city
print("---> selectedCity: \(selectedCity?.city)")
}
}
}
}.onAppear() {
cities = Bundle.main.decode([Destination].self, from: "cities.json")
}
}
}
private func searchFor(_ txt: String) -> Bool {
return (txt.lowercased(with: .current).hasPrefix(searchQuery.lowercased(with: .current)) || searchQuery.isEmpty)
}
}
struct Destination: Codable, Identifiable, Equatable {
var id: Int
var city: String
var country: String
}