通用类型 'Result' 特化的类型参数太少(得到 1 个,但预期为 2 个)

Generic type 'Result' specialized with too few type parameters (got 1, but expected 2)

我只是想将 Result 包含在我的项目中,并且 运行 遇到了一些问题。在我看来,Alamofire(已经是一个依赖项)似乎在尝试编写 return 结果的函数时定义了自己的结果类型抛出问题。

例如 Xcode (10.2 beta 4) 告诉我我不能写 Result-> Response = (_ result: Result) -> Void 因为通用类型 'Result' specialized with too几个类型参数(得到 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) }
    }

您可以限定对 Result 的引用,以便选择您想要的结果。一个参数的版本属于Alamofire。有两个参数的属于Swift.

typealias Response<T> = (_ result: Alamofire.Result<T>) -> Void

... or ...

static func upload(
    data: Data,
    completion: @escaping (Swift.Result<Int, Error>) -> Void
)

在 Alamofire 5.1.0 中更改:

typealias Response<T> = (_ result: Result<T>) -> Void

typealias Response<T> = (_ result: AFResult<T>) -> Void

成功了。