2

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..

AbyxDev
  • 1,363
  • 16
  • 30
Mika
  • 67
  • 10
  • If Swift let you capture mutating `self` parameters, then it effectively lets you use value types (e.g. instances of structs) as reference types. – Alexander May 11 '18 at 17:22

3 Answers3

0
//Try this one and let me know
struct Workspace: Decodable {
    static var shared = Workspace()
    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!)
                    Workspace.shared.guid = res.guid  //Error here
                    Workspace.shared.name = res.name  //Error here
                    DispatchQueue.main.async {
                        completed()
                    }
                }catch {
                    print ("JSON error")
                }
            }
            }.resume()
    }
}
Vivek Kumar
  • 405
  • 5
  • 15
0

I would suggest you to use class instead of struct. Anyway if you like to use your code, then capture the self inside your mutation method like below:

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

        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!)
                    myself.guid = res.guid  //Error here
                    myself.name = res.name  //Error here
                    DispatchQueue.main.async {
                        completed()
                    }
                }catch {
                    print ("JSON error")
                }
            }
            }.resume()
  }
Razib Mollick
  • 4,617
  • 2
  • 22
  • 20
0

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()
}
Mika
  • 67
  • 10