NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.