gpt4 book ai didi

ios - 为什么我收到此错误 : Ambiguous reference to member 'fetch(with:parse:completion:)'

转载 作者:行者123 更新时间:2023-11-30 12:15:32 25 4
gpt4 key购买 nike

我正在使用Xcode 8.3.3。我收到 Swift 编译器错误“对成员的引用不明确”。我已经检查了代码,但似乎无法弄清楚。

protocol APIClient {
var session: URLSession { get }
func fetch<T: JSONDecodable>(with request: URLRequest, parse: @escaping (JSON) -> T?, completion: @escaping (Result<T, APIError>) -> Void)

func fetch<T: JSONDecodable>(with request: URLRequest, parse: @escaping (JSON) -> [T], completion: @escaping (Result<[T], APIError>) -> Void)
}
fetch(with: request, parse: { json -> [YelpBusiness] in
guard let businesses = json["businesses"] as? [[String: Any]] else { return [] }
return businesses.flatMap { YelpBusiness(json: $0) }
}, completion: completion)

https://github.com/jripke74/RestaurantReviews.git

最佳答案

我检查了你的代码。您已经创建了一个确认 YelpClient 类的协议(protocol)。怎么了?

fetch(with: request, parse: { json -> [YelpBusiness] in
guard let businesses = json["businesses"] as? [[String: Any]] else { return [] }
return businesses.flatMap { YelpBusiness(json: $0) }
}, completion: completion)

使用上面的代码,您可以直接调用协议(protocol),而无需任何委托(delegate)。问题是您需要继承 YelpClient 类中的 protocol ,如下所示。

func fetch<T>(with request: URLRequest, parse: @escaping (APIClient.JSON) -> T?, completion: @escaping (Result<T, APIError>) -> Void) where T : JSONDecodable {
//Code
}

func fetch<T>(with request: URLRequest, parse: @escaping (APIClient.JSON) -> [T], completion: @escaping (Result<[T], APIError>) -> Void) where T : JSONDecodable {
//Code
}

更多详情请引用apple documentation

Updated:

查看下面的屏幕截图以获得更好的想法

enter image description here

关于ios - 为什么我收到此错误 : Ambiguous reference to member 'fetch(with:parse:completion:)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45475914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com