I am trying to learn network request using Alamofire in SwiftUI. I am trying to fetch data from a URL and display it on my simulator screen. I have used a button on tap of which , the fetched data will get displayed. The problem is on tap of the button nothing is getting printed.
I tried using debugPrint() and print() turn by turn, but nothing is getting printed , neither on simulator screen nor in console. How do i achieve the same, here is what i tried -->
import SwiftUI
import Alamofire
struct ContentView: View {
@State var tapped : Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Button(action: read, label: {
Text("Let's Begin!")
})
.padding()
}
}
}
func read() {
print ("%%%%%%%%%%%%%%%")
AF.request("https://httpbin.org/get").response { response in
debugPrint("Response : \(response)")
}
}
#Preview {
ContentView()
}
Usually you would bounce it over to a task, like this: