0

I used Alamofire and PromiseKit as separate Cocoapod installs. I can retrieve the JSON data using Alamofire, but I am receiving the error below when configuring PromiseKit. The error below appears in the line where 'fulfill, reject' are in.

Error message: Contextual closure type '(Resolver<_>) -> Void' expects 1 argument, but 2 were used in closure body

I am using Xcode 9.2 and IOS 11.2 inside of the Simulator. Thank you for your advice in advance!

      func wantToReturnAnArrayOfActor() -> Promise<[Actor]> {
    return Promise { fulfill, reject in
        Alamofire.request(ApiUrl.url.rawValue).responseJSON { (response) in
            switch(response.result)
            {
               case .success(let responseString): print("my response string = \(responseString)")
               let actorResponse = ActorApiResponse(JSONString: "\(responseString)")//converts all of the data into the ActorApiResponse model class
              return when(fulfilled: actorResponse)
              DispatchQueue.main.async {
                print("actorResponse = \(String(describing: actorResponse))")
                }

               case .failure(let error): print("alamofire error = \(error)")

            }
        }

    }
}

1 Answers1

1

Should it rather be like this,

func wantToReturnAnArrayOfActor() -> Promise<[Actor]> {

    return Promise() { resolver in

        Alamofire.request(ApiUrl.url.rawValue).responseJSON { (response) in
            switch(response.result)
            {
            case .success(let responseObject):
                let actorResponse = ActorApiResponse(jsonObject: responseObject)
                let actors = actorResponse.getActors()
                resolver.fulfill(actors)

            case .failure(let error):
                resolver.reject(error)

            }
        }

    }
}

The initializer closure for Promise takes in single argument, which is of type Resolver, which is what your error says. Then, you would want to resolve your promise with result which is of type [Actor] when the promise execution is finished or then reject with error if error occurred during the execution.

Few points to note here:

  • Alamofire.request(_).responseJSON returns json object not json string.
  • If your ActorApiResponse is the object which transforms the json to [Actor], you should have proper method to convert json object to actual data type ie. [Actor].

You could have your ActorApiResponse something like this,

struct ActorApiResponse {
    init(jsonObject: Any) {

    }

    func getActors() -> [Actor] {
        // calculate and return actors
        return []
    }
}

Then, you can call it from else where,

    wantToReturnAnArrayOfActor().done {
       // do something with [Actor here]
       // You can also chain the multiple promise using .then instead of using done
        }.catch { error in
            print("Error occurred \(error)")
    }
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • Sandeep, thank you very much for your response! I am using ObjectMapper to associate the JSON data with my model. I am can see the data via the print( ), but I am recieving this error: Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.} – MikeIOSMike Apr 02 '18 at 23:06