Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I just wanted to include Result in my project and am running across a few issues. It seems to me as if Alamofire (which is already a dependency) defines its own Result type throwing problems when trying to write functions that return results.

For example Xcode (10.2 beta 4) tells me that I can't write Result-> Response = (_ result: Result) -> Void because Generic type 'Result' specialized with too few type parameters (got 1, but expected 2).

Both are linked as frameworks installed via Cocoapods in a "Swift 5.0 beta" project.

I'm guessing issues like this shouldn't actually be occurring, but I'm doing something wrong here. Any pointers would be great, thank you!

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) }
    }
like image 913
Asge Yohannes Avatar asked Mar 14 '19 01:03

Asge Yohannes


2 Answers

You can qualify the reference to Result in order to choose the one you want. The version with one parameter belongs to Alamofire. The one with two parameters belongs to Swift.

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

... or ...

static func upload(
    data: Data,
    completion: @escaping (Swift.Result<Int, Error>) -> Void
)
like image 110
osteven Avatar answered Sep 19 '22 18:09

osteven


In Alamofire 5.1.0 changing :

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

to

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

worked.

like image 37
Amit Avatar answered Sep 21 '22 18:09

Amit