[英]SwiftUI Search is not working on iPhone 13 Pro Max iOS 16
我有一個包含國家列表的 jsonFile 並創建了一個名為countryFromJson()的函數來讀取 json 文件中的數據,問題是搜索在所有設備上都正常工作,但在iOS 16 的 iPhone 13 Pro Max 上搜索是不工作,我找不到它有什么問題,是設備問題還是因為我在下面寫的算法?
struct CountryListView: View {
@Binding var countryCode: String
@Binding var countryFlag: String
@Binding var countryName: String
@State private var searchText = ""
var body: some View {
HStack {
VStack {
SearchBar(text: $searchText)
List {
ForEach( countryFromJson().data.filter({ country in
let fullCountryName = country.countryName.hasPrefix(searchText)
let countryCode = country.dialCode
return fullCountryName || searchText == "" || countryCode.hasPrefix(searchText)
}), id: \.self) { country in
HStack {
Text(country.flag)
Text(country.countryName)
Spacer()
Text("+\(country.dialCode)")
.foregroundColor(.secondary)
}.background(Color.white)
.font(.system(size: 20))
.onTapGesture {
self.countryCode = "+\(country.dialCode)"
self.countryFlag = country.flag
let countryName = country.countryName
self.countryName = countryName
self.presentation.wrappedValue.dismiss()
}
}
}
}
}
}
}
任何幫助將不勝感激,我只想知道這有什么問題,因為搜索在我測試過的每台設備上都正常工作,只有裝有 iOS 16 的 iPhone 13 Pro Max 無法進行搜索。
我發現了可能導致問題與搜索相關的問題,解決方案很簡單:只需將這些數據轉換為小寫即可解決我的問題
let fullCountryName = country.countryName.lowercased().hasPrefix(searchText.lowercased())
let countryCode = country.dialCode.lowercased()
return fullCountryName || searchText == "" || countryCode.hasPrefix(searchText.lowercased())
現在我可以搜索大寫和小寫了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.