但是,您可以考虑使用这种替代方法来执行搜索框:
let langDictionary: [String:String] = [
"af" : "Afrikaans",
"al" : "Albanian",
"ar" : "Arabic",
"az" : "Azerbaijani",
"bg" : "Bulgarian",
"ca" : "Catalan",
"cz" : "Czech",
"da" : "Danish",
"de" : "German",
"el" : "Greek",
"en" : "English",
"eu" : "Basque",
"fa" : "Persian (Farsi)",
"fi" : "Finnish",
"fr" : "French",
"gl" : "Galician",
"he" : "Hebrew",
"hi" : "Hindi",
"hr" : "Croatian",
"hu" : "Hungarian",
"id" : "Indonesian",
"it" : "Italian",
"ja" : "Japanese",
"kr" : "Korean",
"la" : "Latvian",
"lt" : "Lithuanian",
"mk" : "Macedonian",
"no" : "Norwegian",
"nl" : "Dutch",
"pl" : "Polish",
"pt" : "Portuguese",
"pt_br" : "Portuguese (Brazil)",
"ro" : "Romanian",
"ru" : "Russian",
"se" : "Swedish",
"sk" : "Slovak",
"sl" : "Slovenian",
"es" : "Spanish",
"sr" : "Serbian",
"th" : "Thai",
"tr" : "Turkish",
"uk" : "Ukrainian",
"vi" : "Vietnamese",
"zh_cn" : "Chinese Simplified",
"zh_tw" : "Chinese Traditional",
"zu" : "Zulu"]
struct ContentView: View {
@State var searchQuery = ""
@State var langArr = Array(langDictionary.values.sorted {$0 < $1})
var body: some View {
VStack (spacing: 30) {
TextField("Search", text: $searchQuery).padding(10)
.overlay(RoundedRectangle(cornerRadius: 15).stroke(Color.blue, lineWidth: 1))
.foregroundColor(.blue)
ScrollView {
VStack(spacing: 10) {
// move the search system here
ForEach(langArr.filter{self.searchFor($0)}.sorted(by: { $0 < $1 }), id: \.self) { lang in
Text(lang).padding(10)
}
}
}
}.padding(10)
}
private func searchFor(_ txt: String) -> Bool {
return (txt.lowercased(with: .current).hasPrefix(searchQuery.trim().lowercased(with: .current)) || searchQuery.trim().isEmpty)
}
}