Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeff Ripke
41,989 PointsI getting this error: Ambiguous reference to member '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
Thanks for you help in advance. Jeff
9 Answers
Pasan Premaratne
Treehouse TeacherJeff Ripke Jack Yi Jeff McDivitt
Apologies to everyone here. This post got lost in the back burner and I marked it as resolved by mistake. There are a variety of reasons that this error could pop up, but for the most part the error you see is largely misleading. The compiler isn't doing a great job here.
In Jeff (Ripke's) project, the issue is because YelpBusiness does not declare its conformance to JSONDecodable. The initializer is implemented but without the object explicitly saying it conforms to the protocol the compiler can't deduce that.
The reason you get the ambiguous reference error is because we're relying on the compiler's type inference to do a lot of work for us. Both fetch methods are defined with a generic parameter T, where T conforms to JSONDecodable. Once you provide a concrete type for T, based on the signature of your parse function (whether it returns a single object or an array), the compiler can figure out which particular fetch method you're calling.
In this case the signature of your parse function is JSON -> [YelpBusiness]
but since YelpBusiness does not conform to JSONDecodable it isn't a valid representation of T. Since it doesn't conform to the generic contract that T defines, the compiler thinks you are referring to some other fetch method other than the two defined since the signatures don't match up.
Hence the ambiguous reference error. After adding explicit conformance to the type in Jeff's project it did not build at first pass, but after upgrading his project to Swift 4, cleaning out the build and rebuilding, it compiled fine.
This might not be the universal case so if it isn't we can debug further.
Jeff McDivitt
23,970 PointsI replied on GitHub
Jeff Ripke
41,989 PointsI don't see anything different from my code. Do you get that error?
Jeff McDivitt
23,970 PointsHi Jeff - I do not get the error, I am thinking the error is being caused by another file in your project. You could zip me your entire project if you would like mcd132002@yahoo.com
Jeff McDivitt
23,970 PointsDisregard I forgot it is on Github, let me take a look
Jeff McDivitt
23,970 PointsI am at a total loss on this one I looked it over and re-wrote the code. Also compared it to my project and it is exactly the same. Not sure why this is happening
Jeff Ripke
41,989 PointsThanks for looking, I appreciate it. I will take it to stack overflow.
Jeff Ripke
41,989 PointsPosted this to stack overflow but I am still confused.
Jeff McDivitt
23,970 PointsYour are doing exactly what they explained that is why I was confused. Also the code that they posted is exactly the same as you have. I would reach out to Pasan and ask him to look at your code. Please let me know if you figure it out because I am very curious of why this is happening.
Jack Yi
8,450 PointsI'm actually running into the exact same error. Any luck getting this resolved?
Jeff Ripke
41,989 PointsNope, even reached out to the instructor and he never responded.
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsDid anyone actually find a solution to this error? I have the same error, and the solution provided here didn't work for me