Reputation: 55
I'm a newbie in creating an sign in using API in swift. I followed a video tutorial from treehouse but I used different version of Xcode and swift. I don't know what will I put in these code. Hope you could help me Or can you give me any references that I could use in creating a sign in page that will input passcode in textfield and submit to validate if the code is existing and will post data. Thank you so much.
When I click Fix these line of code appeared
final class EventAPIClient: APIService {
func JSONTaskWithRequest(request: URLRequest, completion: (JSON?, HTTPURLResponse?, NSError?) -> Void) -> JSONTask {
<#code#>
}
init(config: URLSessionConfiguration) {
<#code#>
}
let configuration: URLSessionConfiguration
lazy var session: URLSession = {
return URLSession(configuration: self.configuration)
}()
private let token: String
init(config: URLSessionConfiguration, APIKey: String) {
self.configuration = config
self.token = APIKey
}
convenience init(APIKey: String) {
self.init(config: URLSessionConfiguration.default, APIKey: APIKey)
}
}
Upvotes: 0
Views: 145
Reputation: 306
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 :)
Upvotes: 0