0

I new in swift and I wrote simple api connector to get info from server, but I do not know how to get my data from server and use it in app

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")
        })

How to get data into 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
}

}

Stremovskyy
  • 452
  • 7
  • 18
  • Your error is due to driver account being a struct. And you already initialize it before the closure. Maybe you can change the driver account to have a method to fill itself instead of reinit it. So you could do self.account.fill(with: Json) – Marco Pappalardo Sep 24 '17 at 07:05
  • Yes it was a struct, change it to class `code` 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 } } `code` But nothing changed (( – Stremovskyy Sep 24 '17 at 07:09

1 Answers1

0

i think, your DriverAccount is a struct. Structs are values. So, values are copied for reuse in closures and self.account is a let-value in your closure. If you want to change local var in closure, use class. And, you can produce retain-cicle if use just a self.. - use ([weak self] in) to avoid it. https://stackoverflow.com/a/26598603/5769826

Ihor M.
  • 184
  • 10
  • Yes it was a struct, change it to class, Added into post. But Closure cannot implicitly capture a mutating self parameter Error on same place ( – Stremovskyy Sep 24 '17 at 07:15