-1

EDIT: 19.02.2019 I tried the solutions offered. I went for a simpler option of passing a dictionary as the parameter and using Alamofire's JSONEncoder to turn it into a JSON object. This was the first thing I had tried but the problem was that Thanks everyone for the help.

I want to post a class object to an api but I'm struggling with types and encoding it to JSON.

let jsonData = try JSONEncoder().encode(classObject)
let params = String(data: jsonData, encoding: .utf8)

Alamofire.request(shopSubmitURL, method: .post, parameters: params).responseString { response in
            switch(response.result) {
            case .success(_) :
                if let data = response.result.value {
                    print("Successfully submitted class object: \(response.result.value)")
                }
                break

            case .failure(_):
                print(response.result.error)
                break
            }

And my classObject is set up like this:

class classObject: NSObject, Codable {
    var name : String = ""
    var items : Array = [Int]()
}

I've tried converting the object to a dictionary with an extension but when I do I have to use 'Any' or 'AnyObject' and that causes 'cannot infer generic parameter' errors.

At the moment I get the 'extra argument 'method' in call' error.

  • Possible duplicate of [Turn swift object into a JSON string](https://stackoverflow.com/questions/43126393/turn-swift-object-into-a-json-string) – jscs Feb 18 '19 at 17:27
  • 1
    That error must be related to your Alamofire call and not json encoding – Joakim Danielson Feb 18 '19 at 17:39
  • Josh - I thought that might have the answer but I'm using swift 4 and it's unclear whether I should be using JSONserialisation or some other method. – Richard Ross Feb 18 '19 at 17:39
  • Joakim - I get the Alamofire error whenever the parameters are not a suitable JSON object. I'm struggling to make the correct JSON object from my class object. – Richard Ross Feb 18 '19 at 17:41

2 Answers2

0

lets watch in detail:

AF.request(url: URLConvertible, method: HTTPMethod, parameters: Parameters?, encoding: ParameterEncoding, headers: HTTPHeaders?)

in Alamofire sources you can see that public typealias Parameters = [String: Any]

it means that you need to use it like

AF.request("https://somewhere.com", method: .post, parameters: ["name":"Name", "items":[1,2,3,4]], encoding: JSONEncoding.default, headers: [/*headers if need, also Map*/])
Mixon McLaren
  • 257
  • 2
  • 6
-1

As Josh mentioned, this question has been answered already. But to make it quick for you, I'll recommend few things.

First thing I would recommend you to use struct instead of class. learn more

Second you may need just a simple codable object like below.

struct classObject: Codable {
    let name : String
    let items : [Int]

    // CodingKeys are used to map with json keys
    enum CodingKeys: String, CodingKey {
        case name = "name"
        case items = "itemsArr"
    }

}

recommended link CodingKeys

Mani
  • 1
  • 1