请求URL并分析警报中的结果

时间:2020-06-26 21:48:25

标签: swift swiftui alamofire swifty-json

我已将变量设置为全局变量,但是在得到响应后,我仍无法弄清楚如何显示警报。不知道我是否必须创建函数。一切都会有所帮助。

代码:https://hastebin.com/izurihadib.swift

$url = "https://www.google.com/";
$headers = get_header($url);

print_r($headers["0"]);

1 个答案:

答案 0 :(得分:0)

这是解决问题所需的更改。

struct ContentView: View {
    @State private var showingAlert = false
    @State private var str1 = ""
    @State private var fullStr = ""
    var body: some View {
        TabView {
            Button(action: {
                self.showingAlert = true
                AF.request("https://api-quiz.hype.space/shows/now").responseJSON { response in
                    switch response.result {
                    case .success(let value):
                        //...
                    case .failure(let error):
                        self.str1 = "Error"
                        self.fullStr = "\(error)"
                        self.showingAlert = true
                    }
                }
            }) {
                Text("Get Info")
            }
            .alert(isPresented: $showingAlert) {
                Alert(title: Text(str1), message: Text(fullStr), dismissButton: .default(Text("OK")))
            }
            //...
        }
    }
}