tristanhimmelman/AlamofireObjectMapper

Using object mapper with Alamofire 4.0

Closed this issue · 8 comments

Is possible to do it? I didn't see responseObject method

Works fine. Using AlamofireObjectMapper with Alamofire 4.0 without any problems.

anybody have a example of mapping json to a swift object using alamofire 4. i see tons of examples using alamo3 but none using 4. any help appreciated

I´m in the same spot. I´d like to see an example with Alamofire 4. D:

God where is example for 4?

This is a quick example for Alamofire 4

request(URL, method: .get).responseObject { ( response : DataResponse <YOURCLASS>) in 
}

Correct approach is as @frankgumeta says. However you have to put import AlamofireObjectMapper statement to top of your file.
I have forgotten to do it and compiler gave me an following error: "Value of type 'DataRequest' has no member 'responseObject'" :]

It seems that you have to import both of ObjectMapper and AlamofireObjectMapper:

import ObjectMapper
import AlamofireObjectMapper

A full example could be nice as of course @frankgumeta help is very valuable (it helped me to find what I will past bellow) but things happen also after when you try to retrieve the objects. You previously had to check the response value, now if .success is called, you are sure that you have a valid object. So here is a full example of how I used it. Notice the now bloated failure management. I may have misunderstood something about this errors part, but it seems to me that you have to check a lot of cases now that you didn't have to check before due to the new AFError error object.

let wsUrl = "https://whatever"

let parameters: Parameters = ["subscription_id": "123456789"]

Alamofire.request(wsUrl, method: .get, parameters: parameters, headers: HTTPManager.getDefaultHeaders(.get))
    .validate(statusCode: [200])
    .responseObject { (response: DataResponse<PaymentMediumResponse>) in
        switch response.result {
        case .success(let paymentMediumResponse):
            delegate.onPaymentMediumServiceGetPaymentMediumSuccess(paymentMediumResponse)
            break
        case .failure(let error):
            if let error = error as? AFError {
                switch error {
                case .responseValidationFailed(let reason):
                    print("Response validation failed: \(error.localizedDescription)")
                    print("Failure Reason: \(reason)")
                    
                    switch reason {
                    case .unacceptableStatusCode(let code):
                        switch code {
                        case HTTPManager.HTTPCodes.Failure.BAD_REQUEST :
                            delegate.onPaymentMediumServiceGetPaymentMediumFailure(.bad_request)
                            break
                        default:
                            delegate.onPaymentMediumServiceGetPaymentMediumFailure(.server_internal_error)
                            break
                        }
                    default:
                        delegate.onPaymentMediumServiceGetPaymentMediumFailure(.server_internal_error)
                        break
                    }
                default:
                    delegate.onPaymentMediumServiceGetPaymentMediumFailure(.server_internal_error)
                    break
                }
            } else {
                delegate.onPaymentMediumServiceGetPaymentMediumFailure(.server_internal_error)
            }
            break
        }
}