我只是想在我的项目中包含 Result 并且遇到了一些问题。在我看来,Alamofire(已经是一个依赖项)在尝试编写返回结果的函数时定义了自己的 Result 类型抛出问题。
例如 Xcode (10.2 beta 4) 告诉我我不能写 Result-> Response = (_ result: Result) -> Void 因为通用类型“结果”专门使用了太少的类型参数(得到 1,但预期为 2) .
两者都链接为通过 Cocoapods 在“Swift 5.0 beta”项目中安装的框架。
我猜这样的问题实际上不应该发生,但我在这里做错了。任何指针都会很棒,谢谢!
import Foundation
import Alamofire
typealias Response<T> = (_ result: Result<T>) -> Void //error here
class APIClient {
private static let baseUrl: URL = URL(string: "https://api.flickr.com/services/rest/")!
private static let key: String = "8e15e775f3c4e465131008d1a8bcd616"
private static let parameters: Parameters = [
"api_key": key,
"format": "json",
"nojsoncallback": 1
]
static let shared: APIClient = APIClient()
let imageCache = NSCache<NSString, UIImage>()
@discardableResult
private static func request<T: Decodable>(path: String? = nil,
method: HTTPMethod,
parameters: Parameters?,
decoder: JSONDecoder = JSONDecoder(),
completion: @escaping (Result<T>) -> Void) -> DataRequest {
let parameters = parameters?.merging(APIClient.parameters, uniquingKeysWith: { (a, _) in a })
return AF.request(try! encode(path: path, method: method, parameters: parameters))
.responseDecodable (decoder: decoder) { (response: DataResponse<T>) in completion(response.result) }
}