gpt4 book ai didi

swift - 可能没有解决方案的奇怪的 Swift TextField 故障?

转载 作者:行者123 更新时间:2023-12-04 13:35:25 24 4
gpt4 key购买 nike

所以我有一个相当大的文件来定义搜索栏的 View 。我刚刚花了最后两个小时从文件中删除所有多余/不必要的代码。我遇到的错误是,当我在搜索栏中快速输入时,并不是每个按下的键都被注册,所以它最终会出现一些乱码。似乎 ObservedObjects、状态变量、绑定(bind)变量以及我删除的普通变量和代码越多,文本字段工作得越快越好。
在此链接中可以看到我遇到的故障:https://youtu.be/42sjhDxSKBw
作为引用,我输入的是“你好堆栈溢出,这是一个快速输入的测试”......如果我输入速度较慢,它就会出现。
在下面的示例中,我删除了所有变量,因此运行非常顺利。有没有人有任何使用 SwiftUI TextFields 的经验来展示这种在发生很多事情时不注册每个键的奇怪行为?文本字段的 View (最简单的分解形式,没有所有不同的变量和内容,如下所示):

import SwiftUI
import Mapbox
import MapboxGeocoder

struct SearchBar: View {



var VModel : ViewModel
@Binding var searchedText: String


var body: some View {

let binding = Binding<String>(get: {
self.searchedText
}, set: {
self.searchText = $0
self.searchedText = self.searchText
self.VModel.findResults(address: self.searchedText)
if self.VModel.searchResults.count >= 0 {
self.showResults = true
self.showMoreDetails = false
} else {
self.showResults = false
}
}
)


return VStack {
HStack {
TextField("Search", text: binding, onEditingChanged: { isEditing in
print("we are not editing the text field")
}, onCommit: {
print("pressed enter")
if self.VModel.searchResults.first != nil {
self.annotation.addNextAnnotation(address: self.rowText(result: self.VModel.searchResults.first!).label)
self.searchedText = "\(self.rowText(result: self.VModel.searchResults.first!).label)"
}
})
}
.foregroundColor(Color(.white))
.background(Color.gray)
}
}
}
ViewModel 类如下所示:
import SwiftUI
import CoreLocation
import Mapbox
import MapboxGeocoder

class ViewModel: ObservableObject {

@ObservedObject var locationManager = LocationManager()
@Published var lat: Double?
@Published var lon: Double?
@Published var location: CLLocationCoordinate2D?
@Published var name: CLPlacemark?
@Published var searchResults: [GeocodedPlacemark] = []

func findResults(address: String) {
let geocoder = Geocoder(accessToken: "pk.eyJ1Ijoibmlja2JyaW5zbWFkZSIsImEiOiJjazh4Y2dzcW4wbnJyM2ZtY2V1d20yOW4wIn0.LY1H3cf7Uz4BhAUz6JmMww")
let foptions = ForwardGeocodeOptions(query: address)
foptions.maximumResultCount = 10
geocoder.geocode(foptions) { (placemarks, attribution ,error) in
guard let placemarks = placemarks else {
return
}
self.searchResults = []
for placemark in placemarks {
self.searchResults.append(placemark)
}
}
}
}
在用于显示搜索结果的函数中,我有以下使用 searchResults 的代码块:
ForEach(self.VModel.searchResults, id: \.self) { result in
Button(action: {
self.annotation.addNextAnnotation(address: self.rowText(result: result).label)
self.showResults = false
self.searchedText = self.rowText(result: result).label
}, label: {
self.rowText(result: result).view.font(.system(size: 13))

}).listRowBackground(Color.gray)
}

最佳答案

尝试如下(未测试,因为 env 无法复制)

import Combine

class ViewModel: ObservableObject {

@ObservedObject var locationManager = LocationManager()
@Published var lat: Double?
@Published var lon: Double?
@Published var location: CLLocationCoordinate2D?
@Published var name: CLPlacemark?
@Published var searchResults: [GeocodedPlacemark] = []

private let searchValue = CurrentValueSubject<String, Never>("")
private var cancellable: AnyCancellable?

func findResults(address: String) {
if nil == cancellable {
cancellable = self.searchValue
.debounce(for: 0.5, scheduler: DispatchQueue.main)
.flatMap { newValue in
Future<[GeocodedPlacemark], Never> { promise in
let geocoder = Geocoder(accessToken: "pk.eyJ1Ijoibmlja2JyaW5zbWFkZSIsImEiOiJjazh4Y2dzcW4wbnJyM2ZtY2V1d20yOW4wIn0.LY1H3cf7Uz4BhAUz6JmMww")
let foptions = ForwardGeocodeOptions(query: address)
foptions.maximumResultCount = 10
geocoder.geocode(foptions) { (placemarks, attribution ,error) in
guard let placemarks = placemarks else {
return
}
promise(.success(placemarks))
}
}
}
.receive(on: DispatchQueue.main)
.sink(receiveValue: { placemarks in
self.searchResults = placemarks
})
}
self.searchValue.send(address)
}
}

关于swift - 可能没有解决方案的奇怪的 Swift TextField 故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62420962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com