John
John

Reputation: 183

How to assign null value in dictionary?

I need to send to server parameters like this:

"searchConditions": {
     "firstNameLastName": "My Name",
     "email": null,
}

I'm trying to send like this:

let searchParameters: [String: AnyObject] = [
        "firstNameLastName": self.byNameField.text!,
        "email": ""
}

but server sees that "email": "" as email=

When I try to set "email": nil it gives me an error:

Nil is not compatible with expected dictionary value type 'AnyObject'

How can I send "null" value to my server?

Upvotes: 2

Views: 3587

Answers (2)

Jenel Ejercito Myers
Jenel Ejercito Myers

Reputation: 2703

For Objective-C:

"email" = [NSNull null]

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20369

John,

You can use NSNull() to insert null in dictionary,

like

let test : [String : AnyObject] = ["test" : NSNull()]

When you print the value of test key you will get

po test["test"]
▿ Optional<AnyObject>
  - Some : <null>

Upvotes: 3

Related Questions