Reputation: 821
I want to create a dictionary with the next structure, for sending it via Alamofire as JSON to a server:
{
"user": {
"firstName": "fName",
"lastName": null,
"img": null,
"books": [
{
"id": 1
}, {
"id": 2
}
]
},
"locale": "en",
"gender": "male"
}
For this structure of JSON, I've tried the next:
let parameters: [[String: [String: String]], [String: String]]
but there a lot closures, so I confused with it. Can you help me with creating this structure?
Upvotes: 1
Views: 75
Reputation: 135558
The collection types in the Swift standard library only support homogenous collections, i.e. all elements in a collection must have the same type. So you cannot declare an array whose first element is of type [String: [String: String]]
and whose second element is of type [String: String]
if that is what you wanted.
This works:
let parameters = [
"user": [
"firstName": "fName",
"lastName": NSNull(),
"img": NSNull(),
"books": [
[ "id": 1],
[ "id": 2]
]
],
"locale": "en",
"gender": "male"
]
The type of parameters
is [String: NSObject]
where NSObject
is the superclass of all the values in the dictionary. Note the use of NSNull
to model null
values, which is what NSJSONSerialization
expects.
Upvotes: 1