1

I am converting an existing project that used Swift 2.3 to Swift 3.1. So far, I am successful in implementing all changes as per Xcode suggestions, and also added latest PODs.

Everything seems to be fine except the following - (Since this is an already existing code, I partially understood what's being done).

I have a below request -

var request = ModifyProfileAddressAPIRequestData()
request.clientContext = ClientContext()
request.customer = AddressContext(address: CustomerAddress(countryName:countryName, countryCode: countryCode, locationName:locationName,houseNr: houseNr, streetName: streetName, postalCode:postalCode, box: box ))           
request.fields = "customer.address"
let parameters = request.toJSON()

DDLogVerbose("modifyProfileURL parameters JSON:\(parameters)")

alamofireConfigure().request(request).validate(statusCode: 200..<503).responseObject{ (responseData: DataResponse<UserProfile>) in

...

And the ModifyProfileAddressAPIRequestData is a Mappable Struct as below -

fileprivate struct ModifyProfileAddressAPIRequestData : Mappable{

    var customer: AddressContext?
    var clientContext: ClientContext?
    var fields:String?

    init() {}

    init?(map: Map) {}

    mutating func mapping(map: Map) {        
        customer <- map["customer"]
        clientContext <- map["clientContext"]
        fields <- map["fields"]
    }    
}

I get an error in the alamofireConfigure request as Argument type 'ModifyProfileServiceAPIWithAlamofire.ModifyProfileAddressAPIRequestData' does not conform to expected type 'URLRequestConvertible'

My other requests, are successfully changed by following the solution from here and it works without any problem.

My only problem is with the request of type Mappable. It works well in Swift 2.3.

Could someone throw light on what is actually happening and what need to be done to resolve this issue occurring in Swift 3.1 please?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Lohith Korupolu
  • 1,066
  • 1
  • 18
  • 52

2 Answers2

1

You need to implement this method to conform URLRequestConvertible as is defined in Alamofire.swift

/// Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
public protocol URLRequestConvertible {
    /// Returns a URL request or throws if an `Error` was encountered.
    ///
    /// - throws: An `Error` if the underlying `URLRequest` is `nil`.
    ///
    /// - returns: A URL request.
    func asURLRequest() throws -> URLRequest
}

I think you need

fileprivate struct ModifyProfileAddressAPIRequestData : Mappable,URLRequestConvertible{

    var customer: AddressContext?
    var clientContext: ClientContext?
    var fields:String?

    init() {}

    init?(map: Map) {}

    mutating func mapping(map: Map) {        
        customer <- map["customer"]
        clientContext <- map["clientContext"]
        fields <- map["fields"]
    } 

    func asURLRequest() throws -> URLRequest{
        if let url = URL(string:"your http://......"){
            return URLRequest(url: url)
        }else{
            return URLRequest()
        }

    }    
}

Hope this helps

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
0

I would like to answer my own question. I made changes to the request as below (Please see my question for the original code)

var request = URLRequest(url: URL(string: modifyProfileURL)!)

                var data = ModifyProfileAddressAPIRequestData()
                data.clientContext = ClientContext()
                data.customer = AddressContext(address: CustomerAddress(countryName:countryName, countryCode: countryCode, locationName:locationName,houseNr: houseNr, streetName: streetName, postalCode:postalCode, box: box ))
                data.fields = "customer.address"
                let parameters = data.toJSONString()

                request.httpBody = parameters?.data(using: .utf8)
                request.httpMethod = "POST"
                request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
                request.setValue("Bearer \(accessTokenRequest.accessToken)", forHTTPHeaderField: "Authorization")


                alamofireConfigure()
                    .request(request)
                    .validate(statusCode: 200..<503)
                    .responseObject{ (responseData: DataResponse<UserProfile>) in
.
.
.

I basically distinguished between the URLRequest and my data. Hence the Mappable Struct remains same (as in my question).

Lohith Korupolu
  • 1,066
  • 1
  • 18
  • 52