0

I tried to make a put request to server with JSON type and it gives me error. Here is error detail.

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

Here is a code.

var attdList : JSON = [:]
...
var clientsAry = attdList["clients"]
...
let params = ["clients" : clientsAry, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...

I printed params variable and it displays like this. Could anyone please help this?

▿ 1 element
  ▿ 0 : 2 elements
    - key : "clients"
    ▿ value : [
  {
    "id" : null,
    "hours" : "0",
    "client" : {
      "id" : 2,
      "name" : "Anders Andersson"
    }
  },
  {
    "id" : null,
    "hours" : "1",
    "client" : {
      "id" : 4,
      "name" : "Gun Gunsson"
    }
  },
  {
    "id" : null,
    "hours" : "2",
    "client" : {
      "id" : 3,
      "name" : "Johan Johansson"
    }
  },
  {
    "id" : null,
    "hours" : "3",
    "client" : {
      "id" : 1,
      "name" : "Maria Martinsson"
    }
  }
]
      ▿ rawArray : 4 elements
        ▿ 0 : 3 elements
          ▿ 0 : 2 elements
            - key : "id"
            - value : <null>
          ▿ 1 : 2 elements
            - key : "hours"
            - value : "0"
          ▿ 2 : 2 elements
            - key : "client"
            ▿ value : 2 elements
              ▿ 0 : 2 elements
                - key : "id"
                - value : 2
              ▿ 1 : 2 elements
                - key : "name"
                - value : Anders Andersson
        ▿ 1 : 3 elements
          ▿ 0 : 2 elements
            - key : "id"
            - value : <null>
          ▿ 1 : 2 elements
            - key : "hours"
            - value : "1"
          ▿ 2 : 2 elements
            - key : "client"
            ▿ value : 2 elements
              ▿ 0 : 2 elements
                - key : "id"
                - value : 4
              ▿ 1 : 2 elements
                - key : "name"
                - value : Gun Gunsson
        ▿ 2 : 3 elements
          ▿ 0 : 2 elements
            - key : "id"
            - value : <null>
          ▿ 1 : 2 elements
            - key : "hours"
            - value : "2"
          ▿ 2 : 2 elements
            - key : "client"
            ▿ value : 2 elements
              ▿ 0 : 2 elements
                - key : "id"
                - value : 3
              ▿ 1 : 2 elements
                - key : "name"
                - value : Johan Johansson
        ▿ 3 : 3 elements
          ▿ 0 : 2 elements
            - key : "id"
            - value : <null>
          ▿ 1 : 2 elements
            - key : "hours"
            - value : "3"
          ▿ 2 : 2 elements
            - key : "client"
            ▿ value : 2 elements
              ▿ 0 : 2 elements
                - key : "id"
                - value : 1
              ▿ 1 : 2 elements
                - key : "name"
                - value : Maria Martinsson
      - rawDictionary : 0 elements
      - rawString : ""
      - rawNumber : 0
      - rawNull : <null>
      - rawBool : false
      - type : SwiftyJSON.Type.array
      - error : nil
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
Ioan Moldovan
  • 2,272
  • 2
  • 29
  • 54

1 Answers1

4

The error message indicates that an illegal type is used (Parameters), JSON supports only string, number, <null> and array / dictionary.

You should cast your JSON data to String:

let jsonData =  try JSONSerialization.dataWithJSONObject(attdList, options: .prettyPrinted) // first of all convert json to the data
    let convertedString = String(data: jsonData, encoding: .utf8) // the data will be converted to the string

then send your data to server:

let params = ["clients" : convertedString, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...
Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • 3
    Please, suggest native Swift types like `JSONSerialization` instead of `NSJSONSerialization`. Also you can suggest that he can start using `Codable` instead of SwiftyJSON and then he can just easily encode the data using `JSONEncoder` – Robert Dresler Jan 19 '19 at 14:42