WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How transwer generic to closure?

Forums > Swift

Good day. How transwer generic to closure? Error "Cannot convert value of type '([T]) -> Void' to expected argument type '(Optional<Array<_>>) -> Void'"


protocol FetcherService {

    var networkDataFetcher: NetworkDataFetcher {
        get
    }

    init(networkDataFetcher: NetworkDataFetcher)

    func dataFetcherDevice<T: Decodable>(url: String, completion: @escaping ([T]) -> Void)
}

class DataFetcherService:  FetcherService {
    var networkDataFetcher: NetworkDataFetcher

    required init(networkDataFetcher: NetworkDataFetcher) {
        self.networkDataFetcher = networkDataFetcher
    }

    func dataFetcherDevice<T: Decodable>(url: String, completion: @escaping ([T]) -> Void) {
        networkDataFetcher.dataFetcher(url: url, completion: completion)
    }
}
class NetworkDataFetcher {

    var networkService: NetworkService

    init(networkService: NetworkService = NetworkService()) {
        self.networkService = networkService
    }

    func dataFetcher<T: Decodable>(url: String, completion: @escaping ([T]?) -> Void) {
        networkService.get(url: url) { (data, error) in
            if let error = error {
                print("Error received requesting data: \(error.localizedDescription)")
                completion(nil)
            }
            guard let newData = data  else {
                return
            }
            let decoded = self.decodeJSON(type: [T].self, from: newData)
//            if data != nil {
//                let decoded = self.decodeJSON(type: [T].self, from: data)
//                completion(decoded)
//            }
            //let decoded = self.decodeJSON(type: [T].self, from: data)
            completion(decoded)
        }
    }
    }

1      

There are a few different ways you could address this. Here's one:

func dataFetcherDevice<T: Decodable>(url: String, completion: @escaping ([T]) -> Void) {
    //this takes in result, which is of type [T]?
    networkDataFetcher.dataFetcher(url: url) { result in
        //and gives back to the completion handler either a filled in [T] or an empty array
        completion(result ?? [])
    }
}

So instead of just passing along the completion handler to the inner function call, you nest completion handlers.

Of course, it would probably just be easier to have NetworkDataService.dataFetcher take a completion handler of type ([T]) -> Void in the first place and return [] in the event of failure rather than nil.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.