首页 > 解决方案 > 无法推断复杂的闭包返回类型;添加显式类型以消除歧义 [SWIFTUI]

问题描述

今天我试图在我的@state变量 currentConfigurations 中设置一个值。我使用 anAPI来填充该变量,问题是当我尝试使用onAppear()方法进行此操作时出现此错误:

无法推断复杂的闭包返回类型;添加显式类型以消除歧义

我不知道如何“投射”我的对象以避免这种错误

struct PreferencesView: View {
    let settingsService = SettingsService()
    @State private var currentConfigurations : Settings? = nil
    @State private var childIsPresented = false

    var body: some View {
        ZStack{
            VStack(spacing:0){

             HStack{

                Text("notificationsConfiguration")
                Spacer()
                Button(action:{
                    self.childIsPresented = true
                }){
                    Image(systemName: "chevron.right")
                }




            }
            .padding()
            .sheet(isPresented: self.$childIsPresented){
                NotificationsView(isPresented: self.$childIsPresented, currentConfigurations: self.$currentConfigurations)
            }

                Spacer()
            }


        }.onAppear(){
            let defaults = UserDefaults.standard
            let userId = defaults.integer(forKey: "userId")
            self.settingsService.getSettings(id: userId){(settings) in
                self.currentConfigurations = settings
            }
        }
    }
}

这是我的getSettings方法

func getSettings(id: Int, completionHandler: @escaping (Settings)->Void){
    var settingsData: Settings! = nil
    AF.request(SettingsRouter.getById(id: id)).responseJSON{ response in
        switch(response.result){
        case .success(let response):
            print(response)
            let dict = (response as? [String : Any])!
            if let json = dict["data"] as? [String: Any] {

                if let jsonData = try? JSONSerialization.data(withJSONObject: json , options: .prettyPrinted)
                {
                    do {
                        let jsonString = String(data: jsonData, encoding: String.Encoding.utf8)!
                        print(jsonString)
                        settingsData = try JSONDecoder().decode(Settings.self, from: jsonData)
                        print("Object Converted:")
                    } catch {
                        print("Parsing Failed: ", error.localizedDescription)
                    }
                }
            }else{
                print("Credenciales Incorrectas")
            }
            completionHandler(settingsData)

        case .failure(let error):
            print(error.localizedDescription)
            completionHandler(settingsData)
        }
    }
}

标签: bindingswiftuistateswift5.2

解决方案


推荐阅读