我对swiftui有一个了解性的问题。我想在Alamofire视图的开始处下载文件。如果无法下载,则应显示警报。不幸的是,我总是收到错误消息:“'AlertView'初始化程序的结果未使用”。
func download_file() {
print("FILE GEDOWNLOAD")
Alamofire.request("rubriken.json").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let status = response.response?.statusCode
{
switch(status)
{
case 200:
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
case 500:
print("The response failed")
default:
print("error with response status: \(status)")
}
}
}
}
struct MainView: View {
@EnvironmentObject var loginViewModel: LoginViewModel
@State private var selection = 0
var body: some View {
NavigationView {
List(0 ..< 30) { item in
Image(systemName: "photo")
VStack(alignment: .leading) {
Text("Apple Support")
Text("Passwort für die App")
.font(.subheadline)
.foregroundColor(.secondary)
}
}.navigationBarItems(trailing:
Button(action: {
self.loginViewModel.isLoggedIn = false
}) {
Text("Sperren")
})
.navigationBarTitle("Meine Passwörter")
} .onAppear() {
print("GESTARTET")
download_file()
}
}
}
查询应在View .onappear的构造过程中进行。