VAAA VAAA - 6 months ago 25
Swift Question

How to pass nil as a param of dictionary<String,AnyObject>

I have a Dictionary of type

Dictionary<String, AnyObject>
and I need to pass a parameter as nil:

let params = [
"number": nil,
"description": nil,
"lang" : "spa",
"byFee": true,
"plan" : data.Plan
] as Dictionary<String, AnyObject>

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])

If I do this I get an error:

enter image description here

So I tried to change the Dictionary to
Dictionary<String, AnyObject?>

But If I do this then the error comes in the request.HTTBody:

enter image description here

Appreciate any help.


A null value in JSON is equivalent to NSNull in ObjC/Swift:

let params : [String : AnyObject] = [
    "number": NSNull(),
    "description": NSNull(),
    "lang" : "spa",
    "byFee": true,
    "plan" : data.Plan,