现在就用SwiftUI学习Swift。我正在尝试构建一个处理API的类,但遇到了很多困难。
因此,我有一个股票价格函数,每当有人加载视图时,我都想从这个API调用它,然后我希望这个函数返回股票价格对象的正确列表,以便将其呈现为图表。
出于某种原因,我对Result没有.success函数有问题。
很想知道这里的想法!
这是我现在的代码:
final class StockAPI{
func fetchData(symbol: String, completion: @escaping (Result) -> Void) {
let url = URL(string: "https://google.com")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let json: [String: Any] = ["symbol": symbol]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
//completion(.error(error.localizedDescription as! Error), error)
return
}
do{
let stockPrices = try? JSONDecoder().decode([StockPrice].self, from: data!)
DispatchQueue.main.async {
// update our UI
print("Dispatching request")
completion(.success(from: stockPrices as! Decoder))
}
} catch let jsonError {
return
}
})
task.resume()
}
在您的两种错误情况(网络错误和解析错误(中,您应该使用.failure
和错误调用完成闭包。现在你在盲目飞行。
Result
的使用很奇怪,因为它是一个泛型,并且通常是Result<[StockPrice], Error
>(即,指定成功类型是什么,即[StockPrice]
,以及失败类型是什么(即Error
(。Result是一个泛型,您希望指定.success
和.failure
的关联类型是什么类型。
所以,它可能是这样的:
func fetchData(symbol: String, completion: @escaping (Result<[StockPrice], Error>) -> Void) { // if you have compilation error here, try `Swift.Result<[StockPrice], Error>` instead
let url = URL(string: "https://google.com")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let object = ["symbol": symbol]
request.httpBody = try! JSONEncoder().encode(object) // or wrap this in `do`-`catch` block and use `try` without the `!`
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
completion(.failure(error))
return
}
do {
let stockPrices = try JSONDecoder().decode([StockPrice].self, from: data!)
DispatchQueue.main.async {
// update our UI
print("Dispatching request")
completion(.success(stockPrices))
}
} catch let jsonError {
completion(.failure(jsonError))
}
}
task.resume()
}