如何调试 APIService 的代码
How can I debug code for APIService
我是使用 swift 中的 API 创建登录的新手。我遵循了 treehouse 的视频教程,但我使用了不同版本的 Xcode 和 swift。我不知道我将在这些代码中放入什么。希望你能帮助我或者你能给我任何参考,我可以用它来创建一个登录页面,该页面将在文本字段中输入密码并提交以验证代码是否存在并且将 post 数据。太感谢了。
当我点击Fix时出现了这行代码
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)
}
}
为了开始解决这个问题,您需要查看 protocols 是什么。
关注与这种情况相关的信息,他们基本上定义了函数的签名(除其他外)。协议的名称和函数签名留下了关于给定函数的实现应该是什么的线索。这很容易通过一个简单的例子来说明:
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
将其与您的情况联系起来。协议 APIService
定义了如下函数:
protocol APIService {
func JSONTaskWithRequest(request: URLRequest, completion: (JSON?, HTTPURLResponse?, NSError?) -> Void) -> JSONTask
init(config: URLSessionConfiguration)
}
你的 EventAPIClient
class 告诉编译器它意味着要遵守 APIService
协议:
final class EventAPIClient: APIService {
为了符合协议,EventAPIClient
需要为APIService
中的所有定义提供实现。
至于解决这个问题,缺少一些关于 JSONTask 等定义的信息。但是,这里有一个示例实现,如果没有别的,应该给你一个起点:
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?
}
希望对您有所帮助:)
我是使用 swift 中的 API 创建登录的新手。我遵循了 treehouse 的视频教程,但我使用了不同版本的 Xcode 和 swift。我不知道我将在这些代码中放入什么。希望你能帮助我或者你能给我任何参考,我可以用它来创建一个登录页面,该页面将在文本字段中输入密码并提交以验证代码是否存在并且将 post 数据。太感谢了。
当我点击Fix时出现了这行代码
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)
}
}
为了开始解决这个问题,您需要查看 protocols 是什么。
关注与这种情况相关的信息,他们基本上定义了函数的签名(除其他外)。协议的名称和函数签名留下了关于给定函数的实现应该是什么的线索。这很容易通过一个简单的例子来说明:
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
将其与您的情况联系起来。协议 APIService
定义了如下函数:
protocol APIService {
func JSONTaskWithRequest(request: URLRequest, completion: (JSON?, HTTPURLResponse?, NSError?) -> Void) -> JSONTask
init(config: URLSessionConfiguration)
}
你的 EventAPIClient
class 告诉编译器它意味着要遵守 APIService
协议:
final class EventAPIClient: APIService {
为了符合协议,EventAPIClient
需要为APIService
中的所有定义提供实现。
至于解决这个问题,缺少一些关于 JSONTask 等定义的信息。但是,这里有一个示例实现,如果没有别的,应该给你一个起点:
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?
}
希望对您有所帮助:)