URL session initilazition issue

Viewed 1573
class URLSessionDataTask: URLSessionDataTask {
    private let url: URL
    private let data: Data

init(configuration: URLSessionConfiguration = .default, url: URL, data: Data, statusCode: Int, completion: @escaping ) {
    self.data = data
    self.url = url

}

func execute() {
    let response = HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: nil, headerFields: nil)
    completion(data, response, nil)
}

I am working some test it is working but I am getting yellow error, How can i fix my code ?, please help me.

2 Answers

You're receiving this warning because swift automatically calls default super.init() which is deprecated. Specify it explicitly:

class MockURLSession: URLSession {
    private let url: URL
    private let data: Data
    private let statusCode: Int
    
    init(configuration: URLSessionConfiguration = .default, data: Data, statusCode: Int, url: URL = URL(string: "https:flicker.com")!) {
        self.data = data
        self.url = url
        self.statusCode = statusCode
        super.init(configuration: configuration)
    }
}

Subclass initialiser must call the superclass initialiser after initialising all subclass properties.

The warning is produced because of an implicit super.init() call added by the compiler. You need to explicitly call one of the non-deprecated initialisers, such as init(configuration:).

class MockURLSession: URLSession {
    private let url: URL
    private let data: Data
    private let statusCode: Int

    init(data: Data, statusCode: Int, url: URL = URL(string: "https:flicker.com")!) {
        self.data = data
        self.url = url
        self.statusCode = statusCode
        super.init(configuration: .default)
    }
}
Related