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 trial

iOS Build a Restaurant Reviews App Simplifying Networking Code Displaying Restaurants Around Us

Jeff Ripke
Jeff Ripke
41,989 Points

I 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
STAFF
Pasan Premaratne
Treehouse Teacher

Jeff 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 Ripke
Jeff Ripke
41,989 Points

I don't see anything different from my code. Do you get that error?

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi 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
Jeff McDivitt
23,970 Points

Disregard I forgot it is on Github, let me take a look

Jeff McDivitt
Jeff McDivitt
23,970 Points

I 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
Jeff Ripke
41,989 Points

Thanks for looking, I appreciate it. I will take it to stack overflow.

Jeff McDivitt
Jeff McDivitt
23,970 Points

Your 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
Jack Yi
8,450 Points

I'm actually running into the exact same error. Any luck getting this resolved?

Jeff Ripke
Jeff Ripke
41,989 Points

Nope, even reached out to the instructor and he never responded.