我是 Swift 新手 我编写了简单的 api 连接器来从服务器获取信息,但我不知道如何从服务器获取数据并在应用程序中使用它
var account: DriverAccount = DriverAccount()
ApiConnectController.instance().makeAPICall(url: driverAuthUrlEndpoint,
params:paramsDictionary,
withAutorization: false,
method: .POST,
success: { (data, response, error, json) in
self.account = try! DriverAccount(json: json!) // Error Closure cannot implicitly capture a mutating self parameter
print("OK")
},
failure: { (data, response, error, json) in
print("NOT")
})
如何将数据存入 self.account?
class DriverAccount {
var callSign: String?
var dispPhone: String?
var isLoggedin:Bool
init() {
self.callSign = ""
self.dispPhone = ""
self.isLoggedin = false
}
init(isLoggedIn logedIn: Bool, withCallSign callsign: String, andDispPhone dispPhone: String) {
self.callSign = callsign
self.dispPhone = dispPhone
self.isLoggedin = logedIn
}
}
最佳答案
我认为,您的 DriverAccount 是一个结构。结构就是值。因此,复制值以便在闭包中重用,并且 self.account 是闭包中的 let-value 。如果你想改变闭包中的局部变量,请使用 class。 并且,如果只使用 self..,您可以生成保留循环。 - 使用 ([weak self] in) 来避免它。 https://stackoverflow.com/a/26598603/5769826
关于swift - 如何快速从闭包中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387143/