Search code examples
iosjsonswiftalamofire

Alamofire 'invalid type in JSON write' error after upgrading


The following code was working with Alamofire 3.0:

let headers = [
    "content-type": "application/json",
    "cache-control": "no-cache",
    ]
let parameters = [
    "access_token": token.tokenString,
    "client_id": bundleID,
]

Alamofire.request(.POST, url, headers: headers, parameters: parameters, encoding: .JSON).responseJSON { response in
    // ...
}

I changed the request after updating to Alamofire 4.0/Swift3/XCode 8:

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
    // ...
}

And I get the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'

I've been looking for a solution to this problem but can't find anything relevant.


Solution

  • There is definitely something wrong with your parameters. Make sure that both token.tokenString and bundleID are Strings.