I'm trying to use "searchText" as a parameter of a function to make an API call but after I type the name of the city and press search nothing happens. The API works fine with the initial value "Toronto" when the app first launches. Any suggestion ?
Code Block
Code Block import SwiftUI import UIKit func fetchWeather(cityName: String) { let urlString = "\(weatherURL)&query=\(cityName)" getWeatherData(with: urlString) } @State private var searchText = "Toronto" var body: some View { VStack { SearchBar(text: $searchText) }.onAppear() { self.fetchWeather(cityName: self.searchText) } } } struct SearchBar: UIViewRepresentable { @Binding var text: String class Coordinator: NSObject, UISearchBarDelegate { @Binding var text: String init(text: Binding<String>) { _text = text } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { text = searchText } } func makeCoordinator() -> SearchBar.Coordinator { return Coordinator(text: $text) } func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar { let searchBar = UISearchBar(frame: .zero) searchBar.delegate = context.coordinator searchBar.searchBarStyle = .minimal return searchBar } func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) { uiView.text = text } }
Code Block