Search code examples
swiftstructuremutating-function

Mutating function inside structure


I'm using Swift 4, I have a structure that I initialize with default values. I made a function inside that is supposed to read a JSON and change those default values with what it gets but it doesn't seem to work.

Error: Closure cannot implicitly capture a mutating self parameter

Code:

struct Workspace: Decodable {
    var guid: String
    var name: String

private enum CodingKeys : String, CodingKey {
    case guid = "guid"
    case name = "name"
}

init(){
    self.guid = "blbl"
    self.name = "oops"
}

mutating func getUserWorkspace(base: String, completed: @escaping () -> ()){
    let url = URL(string: "some url")!
    var request = URLRequest(url: url)

    request.addValue("Basic \(base)", forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    URLSession.shared.dataTask(with: request){ (data, response, error) in
        if error == nil {
            do {
                let res: Workspace = try JSONDecoder().decode(Workspace.self, from: data!)
                self.guid = res.guid  //Error here
                self.name = res.name  //Error here
                DispatchQueue.main.async {
                    completed()
                }
            }catch {
                print ("JSON error")
            }
        }
    }.resume()
}

I changed let to var, but I guess there's still something I don't understand..


Solution

  • I actually found the problem.. and it was pretty stupid.. I just had to change the "completed" part of the function, like this:

     mutating func getUserWorkspace(base: String, completed: @escaping (_ arr:[Workspace]?) -> ()){
        let url = URL(string: "SOME URL")!
        var request = URLRequest(url: url)
    
        request.addValue("Basic \(base)", forHTTPHeaderField: "Authorization")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
    
        URLSession.shared.dataTask(with: request){ (data, response, error) in
            if error == nil {
                do {
                    let rep = try JSONDecoder().decode([Workspace].self, from: data!)
                    DispatchQueue.main.async {
                        completed(rep)
                    }
                }catch {
                    print(error)
                }
            }
        }.resume()
    }