您不能将@state转换为发布者,但可以改用ObservableObject。
import SwiftUI final class SearchStore: ObservableObject { @Published var query: String = "" func fetch() { $query .map { URL(string: $0) } .flatMap { URLSession.shared.dataTaskPublisher(for: $0) } .sink { print($0) } } } struct ContentView: View { @ObservedObject var store = SearchStore() var body: some View { VStack { TextField("type something...", text: $store.query) Button("search") { self.store.fetch() } } } }