简体   繁体   中英

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?

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>

对于Objective-C:

"email" = [NSNull null]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM