2017-08-03 70 views
0

我正在使用Xcode 8.3.3。我得到一個Swift編譯器錯誤「模糊引用成員」。我已經閱讀了代碼,似乎無法弄清楚。爲什麼我得到這個錯誤:模糊引用成員'fetch(with:parse:completion :)'

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

回答

0

我檢查你的代碼。您已創建一個,它確認類YelpClient。怎麼了?

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) 

使用上面的代碼你直接調用沒有任何delegate。問題是你需要繼承YelpClient類中的,如下所示。

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 
} 

欲瞭解更多詳情,請參閱蘋果documentation

Updated:

檢查下面的截圖爲更好的主意

enter image description here

+0

我相信代碼是正確的,但如果您運行的代碼(我包括GitHub鏈接)。您會對成員'fetch(with:parse:completion :)'錯誤產生模糊引用。 –

+0

@ JeffA.Ripke代碼是正確的,但是您以錯誤的方式實現了'protocol'。你正在混合'protocol'和'closure'。你調用'protocol'的方式不正確。你需要在類中繼承而不是直接調用它。 –

+0

@ JeffA.Ripke簡而言之,我可以說「協議」是不直接調用的。 –

相關問題