无法对不可变值使用变异成员:“自身”不可变

时间:2018-08-01 00:15:49

标签: swift swift4 protocol-oriented

我不明白为什么会出现此错误。 SomeController是一个类,而不是一个结构,并且它是不可变的。有没有解决的办法?

class SomeController: APIFetchable {

    let baseUrl = "https://jsonplaceholder.typicode.com"
    let defaultSession =  URLSession(configuration: .default)
    var dataTask: URLSessionTask?

    func fetchDataFromAPI(atPath path: String, _ callback: ()-> Void){
        fetchDataAtPath(path) { data, error in  /*Error on this line -> "Cannot use mutating member on immutable value: 'self' is immutable"*/

        }
    }

}


protocol APIFetchable {
    var baseUrl: String {get}
    var defaultSession: URLSession {get}
    var dataTask: URLSessionTask? {get set}

    mutating func fetchDataAtPath(_ path: String,  _ callback: @escaping (Data?, Error?)-> Void)
}

extension APIFetchable {

    mutating func fetchDataAtPath(_ path: String,  _ callback: @escaping (Data?, Error?)-> Void){
        dataTask?.cancel()

        if var urlComponents = URLComponents(string: baseUrl) {
            urlComponents.path = path

            guard let url = urlComponents.url else {return}

            dataTask = defaultSession.dataTask(with: url) { data, response, error in
                defer {self.dataTask =  nil}

                if let error = error {
                    print("data task error \(error.localizedDescription)")
                    callback(nil, error)
                } else if let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 {
                    callback(data, nil)
                }
            }
            dataTask?.resume()
        }
    }


}

1 个答案:

答案 0 :(得分:3)

您似乎有2个选项可以解决此问题(根据this page)。

  1. fetchDataAtPath()上实施SomeController的不变版本。
  2. APIFetchable协议仅标记为类:protocol APIFetchable: class { ... },并删除对mutation的使用(如OP注释中提到的@rmaddy)