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) }
}
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
)
In Alamofire 5.1.0 changing :
typealias Response<T> = (_ result: Result<T>) -> Void
to
typealias Response<T> = (_ result: AFResult<T>) -> Void
worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With