4

Using PromiseKit for API call in Swift 4:

 let apiCall = ApiManager.shared.fetchActors()
    apiCall.then { actors -> Void in
        self.dataSourceArray = actors
        self.tableView.reloadData()
        }.catch { error -> Void in

        }

I get this error:

Cannot convert value of type '() -> Void' to expected argument type '() -> _'

How do I solve this issue?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nhuluseda
  • 637
  • 3
  • 8
  • 19

4 Answers4

7

Use .done Instead of using .then it will work.

4

A solution:

func fetchActorsFromApi () -> Promise<[Actor]> {
    return Promise<[Actor]> { seal in
        return Alamofire.request(API_URL).validate().responseString(completionHandler: {
            response in

            switch (response.result) {
            case .success(let responseString1):
                print (responseString1) // should be converted into the modelclass
                let actorResponse = ActorApiResponse(JSONString: "\(responseString1)")!
                seal.fulfill(actorResponse.actors!)
            case .failure(let error):
                print (error)
                seal.reject(error)
            }
        })
    }
}
Kejsi Struga
  • 550
  • 6
  • 21
  • What will be the syntax if i need to handle generic "Model class"/ and not just "Actor" model class. – j.krissa Apr 23 '18 at 12:27
4
let apiCall = ApiManager.shared.fetchActorFromAPI()
        let _ = apiCall.done {
            actors -> Void in
            self.dataSource = actors
            self.myTableView.reloadData()
            }.catch {
                error -> Void in
        }

use this.In the new version of PromiseKit .done() function is able to do that. Documentation : https://github.com/mxcl/PromiseKit

1

replace .catch { error -> Void in with .catch { error in

Hunaid Hassan
  • 732
  • 5
  • 13