In order to begin to resolve this issue, you need to look at what protocols are.
Focusing on information relevant to this situation, they essentially define the signature of a function (among other things). The name of the protocol and the function signature leave clues as to what a given function's implementation should be. This is easily illustrated by a simple example:
protocol MathematicalOperations {
func add(_ int: Int, to int: Int) -> Int
}
class Calculator: MathematicalOperations {
func add(_ intA: Int, and intB: Int) -> Int {
return intA + intB
}
}
// Usage
let calculator = Calculator()
let sum = calculator.add(15, and: 10)
print(sum) // 25
tying it back into your situation. The protocol APIService
has defined the functions like so:
protocol APIService {
func JSONTaskWithRequest(request: URLRequest, completion: (JSON?, HTTPURLResponse?, NSError?) -> Void) -> JSONTask
init(config: URLSessionConfiguration)
}
Your EventAPIClient
class tells the compiler it means to conform to the APIService
protocol:
final class EventAPIClient: APIService {
In order to conform to the protocol, EventAPIClient
needs to provide an implementation for all the definitions in APIService
.
As for solving the issue, there are some missing pieces of information regarding the definition of JSONTask etc. However, here is an example implementation that should, if nothing else, give you a starting point:
func JSONTaskWithRequest(request: URLRequest, completion: @escaping (JSON?, HTTPURLResponse?, NSError?) -> Void) -> JSONTask {
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
completion(nil, response, error as NSError?)
} else if HTTPResponse.statusCode == 200 { // OK response code
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? JSON
completion(json, response, nil)
} catch let error as NSError {
completion(nil, response, error)
}
} else {
completion(nil, response, nil) // could create an error saying you were unable to parse JSON here
}
}
return task as? JSONTask
}
init(config: URLSessionConfiguration) {
self.configuration = config
self.token = "APIKey" // put your default api key here, maybe from a constants file?
}
I hope you find this helpful :)