在不是View的类中访问EnvironmentObject

时间:2020-06-26 19:52:27

标签: ios swift cocoa swiftui swift5

我正在尝试访问某个函数中的EnviormentObject,以完成“使用Apple登录”并将接收到的用户信息发送到EnviormentObject(帐户)以在视图内部进行访问。这是我所拥有的:

        if let authorizationCode = credentials.authorizationCode {
            let code = String(decoding: authorizationCode, as: UTF8.self)
            let parameters = ["code": code] as [String : Any]
            AF.request("http://192.168.86.243:3000/login/apple", method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
                @EnvironmentObject var account: Account
                do {
                    let res = try JSON(data: response.data!)
                    print(res)
                    // Setting user object
                    account.user["id"] = res["user"]["id"].stringValue
                    account.user["email"] = res["user"]["email"].stringValue
                    account.user["name"] = res["user"]["name"].stringValue
                    account.token = res["token"].stringValue
                } catch {
                    account.error = true
                }
                
            }
        }

我也愿意采取任何变通方法来访问视图内部类中的变量,等等。

0 个答案:

没有答案