Dilapidus
Dilapidus

Reputation: 425

What is the specfic type of the Alamofire response to responseJSON?

(Swift 2.2, Alamofire 3.0)

I have a fairly complex error/state checking protocol that I apply after many of my REST calls and I would naturally like to re-use this code.

Alamofire.request(.POST, "some/rest/url", params, encoding:.JSON)
        .validate()
        .responseJSON { response in
             // This is what I'd like to do
             self.myAwesomeMethod(response)

My problem is declaring myAwesomeMethod and, depending on what I try to use, the constants in my resulting switch statement.

Of course, just working within the responseJSON closure, everything is hunky dory :

switch response.result {
            case .Failure(let error):
                handleGracefully(error)
                return
            case .Success:
                // 4. Profit
            }

But when I try to move that switch block into myAwesomeMethod, I get tripped up a couple of ways. I can't seem to find the right declaration for result and I can't quite figure out what the explicit enums are for .Failure and .Success in my cases.

I get that we've been handed a 4 tuple and I've tried declaring it a number of ways after poking through the source. Here is my latest :

private func myAwesomeMethod(
    afResponse: (_, 
                 response: NSHTTPURLResponse?, 
                 result: Result<Value, Error>?,
                 error: NSError?)) {

         switch afResponse.result {
    case .Failure(let error):
        beGraceful(error)
        return
    case .Success:
        // 4. Profit
    }
    ...

However, the Value in Result<Value, Error> gets me this:

Use of undeclared type 'Value'

But .Failure and .Success are not in the context of the closure any more and they are defined in the Result so any other declaration results in not being able to access them.

I've tried unpacking the constituent parts, but that didn't really change the problem for me. I always am using myAwesomeMethod with .responseJSON calls so I'm hoping there is some type I can use for Value that will save me here.

Does anyone have any ideas here?


Fixed thanks to the push in the right direction from @Jon Schier

The declaration of the helper function was pretty off. All I actually needed was the following :

private func myAwesomeMethod(afResponse: Response<AnyValue, NSError>)

After which everything seems quite happy.

Upvotes: 0

Views: 636

Answers (1)

Jon Shier
Jon Shier

Reputation: 12770

The implementation of responseJSON will answer that question for you. It's Result<AnyObject, NSError>.

Upvotes: 1

Related Questions