VAAA VAAA - 1 year ago 83
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.

Answer Source

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,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download