将空文本发送到服务 SwiftUI
Send emptyText to Service SwiftUI
如果按钮值 .isCancel
为真,我想将 searchText
中的空文本发送到 AuthorService()
。这样我的作者搜索结果就会被清理。
import SwiftUI
import Combine
struct AuthorSearchBar: View {
@StateObject var service = AuthorService()
@Binding var isCancel: Bool
@State var emptyText = ""
var body: some View {
VStack(spacing: 0) {
HStack {
Image(systemName: "magnifyingglass")
if isCancel == false{
TextField("Search", text: $service.searchText)
} else if isCancel {
TextField("Search", text: $service.searchText = "")
//Cannot assign to property: '$service' is immutable
//Cannot assign value of type 'String' to type 'Binding<String>'
//Cannot convert value of type '()' to expected argument type 'Binding<String>'
}
.padding()
.onReceive(Just( self.isCancel ? emptyText : service.searchText), perform: { _ in
service.fetchData()
})
}
}
}
我的服务 fetchData()
获取 searchText 参数如下。
所以如果 .isCancel 为真,我想将“”发送到 fetchData。
class AuthorService: ObservableObject {
@Published var searchText : String = ""
func fetchData() {
let parameters = ["index": "authors", "query": "\(searchText)" ]
好吧,我仍然不确定我是否理解流程,但让我们试试...
有一些假设(在评论中提到),方法可能如下(原始代码不可测试,所以答案只是在这里准备 - 可能是错别字,适应是你的,但想法应该很清楚):
struct AuthorSearchBar: View {
@StateObject var service = AuthorService()
@Binding var isCancel: Bool
var body: some View {
VStack(spacing: 0) {
HStack {
Image(systemName: "magnifyingglass")
TextField("Search", text: $service.searchText)
.padding()
}
.onReceive(Just(service.searchText)) { text in
if !text.isEmpty { // assuming search only for existed text
service.fetchData(for: text)
}
}
.onReceive(Just(self.isCancel)) { canceled in
if canceled {
// assuming needed to send on explicit cancel
service.fetchData(for: "")
}
}
}
}
}
// pass what should be sent via argument instead of use property
func fetchData(for text: String) {
let parameters = ["index": "authors", "query": "\(text)" ]
...
}
备选方案: fetchData
没有变化,但在那种情况下,无法将输入的空字符串和取消时的大小写分开(尽管它不是清楚是否重要)。
HStack {
Image(systemName: "magnifyingglass")
TextField("Search", text: $service.searchText)
.padding()
}
.onReceive(Just(service.searchText)) { text in
service.fetchData()
}
.onReceive(Just(self.isCancel)) { canceled in
if canceled {
service.searchText = "" // << this activates above onReceive
}
}
如果按钮值 .isCancel
为真,我想将 searchText
中的空文本发送到 AuthorService()
。这样我的作者搜索结果就会被清理。
import SwiftUI
import Combine
struct AuthorSearchBar: View {
@StateObject var service = AuthorService()
@Binding var isCancel: Bool
@State var emptyText = ""
var body: some View {
VStack(spacing: 0) {
HStack {
Image(systemName: "magnifyingglass")
if isCancel == false{
TextField("Search", text: $service.searchText)
} else if isCancel {
TextField("Search", text: $service.searchText = "")
//Cannot assign to property: '$service' is immutable
//Cannot assign value of type 'String' to type 'Binding<String>'
//Cannot convert value of type '()' to expected argument type 'Binding<String>'
}
.padding()
.onReceive(Just( self.isCancel ? emptyText : service.searchText), perform: { _ in
service.fetchData()
})
}
}
}
我的服务 fetchData()
获取 searchText 参数如下。
所以如果 .isCancel 为真,我想将“”发送到 fetchData。
class AuthorService: ObservableObject {
@Published var searchText : String = ""
func fetchData() {
let parameters = ["index": "authors", "query": "\(searchText)" ]
好吧,我仍然不确定我是否理解流程,但让我们试试...
有一些假设(在评论中提到),方法可能如下(原始代码不可测试,所以答案只是在这里准备 - 可能是错别字,适应是你的,但想法应该很清楚):
struct AuthorSearchBar: View {
@StateObject var service = AuthorService()
@Binding var isCancel: Bool
var body: some View {
VStack(spacing: 0) {
HStack {
Image(systemName: "magnifyingglass")
TextField("Search", text: $service.searchText)
.padding()
}
.onReceive(Just(service.searchText)) { text in
if !text.isEmpty { // assuming search only for existed text
service.fetchData(for: text)
}
}
.onReceive(Just(self.isCancel)) { canceled in
if canceled {
// assuming needed to send on explicit cancel
service.fetchData(for: "")
}
}
}
}
}
// pass what should be sent via argument instead of use property
func fetchData(for text: String) {
let parameters = ["index": "authors", "query": "\(text)" ]
...
}
备选方案: fetchData
没有变化,但在那种情况下,无法将输入的空字符串和取消时的大小写分开(尽管它不是清楚是否重要)。
HStack {
Image(systemName: "magnifyingglass")
TextField("Search", text: $service.searchText)
.padding()
}
.onReceive(Just(service.searchText)) { text in
service.fetchData()
}
.onReceive(Just(self.isCancel)) { canceled in
if canceled {
service.searchText = "" // << this activates above onReceive
}
}