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 Displaying Data with Table Views in Swift 2 Enhancing the Networking Stack Finishing Up the Networking Stack

Protocol requires function JSONTaskWithRequest with type...Do you want to add a stub?

Using Swift 3, I'm getting an error that FoursquareClient does not conform to protocol APIClient and being asked if I want to create a stub. I downloaded the project and converted it to Swift 3, and it's having the same issue. Does anyone know how to fix this?

eberhapa
eberhapa
51,495 Points

Same problem. Anyone?

2 Answers

I just took those methods out of the protocol and left them in the extension. It works fine after that.

How you fix that ?

Yeah I've done the same tecastecs. Basically I removed the JSONTaskWithRequest & Fetch methods from the APIClient Protocol (that were outside of the protocol extension). However, these methods remain in the APIClient protocol extension. This will remove the error.

protocol APIClient {
    var configuration: URLSessionConfiguration { get }
    var session: URLSession { get }
}
Roger Antonell
Roger Antonell
18,252 Points

This problem happens because the APIClient protocol have different "JSONTaskWithRequest" and "fetch" functions signature, than the signature that you provided via extension , if you check the signature of the functions in the protocol declaration and the extension, you will see that they are different, here is how they have to look:

protocol APIClient {
    var configuration: URLSessionConfiguration { get }
    var session: URLSession { get }

    func JSONTaskWithRequest(_ request: URLRequest, completion: @escaping JSONCompletion) -> JSONTask
    func fetch<T: JSONDecodable>(_ request: URLRequest, parse: @escaping (JSON) -> T?, completion: @escaping (APIResult<T>) -> Void)
}
extension APIClient {

    func JSONTaskWithRequest(_ request: URLRequest, completion: @escaping JSONCompletion) -> JSONTask {...}

    func fetch<T>(_ request: URLRequest, parse: @escaping (JSON) -> T?, completion: @escaping (APIResult<T>) -> Void) {...}

    func fetch<T>(enpoint: Endpoint, parse: @escaping (JSON) -> [T]?, completion: @escaping (APIResult<[T]>) -> Void) {...}
Roger Antonell
Roger Antonell
18,252 Points

This problem happens because the APIClient protocol have different "JSONTaskWithRequest" and "fetch" functions signature, than the signature that you provided via extension , if you check the signature of the functions in the protocol declaration and the extension, you will see that they are different, here is how they have to look:

protocol APIClient {
    var configuration: URLSessionConfiguration { get }
    var session: URLSession { get }

    func JSONTaskWithRequest(_ request: URLRequest, completion: @escaping JSONCompletion) -> JSONTask
    func fetch<T: JSONDecodable>(_ request: URLRequest, parse: @escaping (JSON) -> T?, completion: @escaping (APIResult<T>) -> Void)
}
extension APIClient {

    func JSONTaskWithRequest(_ request: URLRequest, completion: @escaping JSONCompletion) -> JSONTask {...}

    func fetch<T>(_ request: URLRequest, parse: @escaping (JSON) -> T?, completion: @escaping (APIResult<T>) -> Void) {...}

    func fetch<T>(enpoint: Endpoint, parse: @escaping (JSON) -> [T]?, completion: @escaping (APIResult<[T]>) -> Void) {...}