Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add nil value to Swift Dictionary?

I have made a request to my server in my app. And posted data something like this.Server side is waiting for all parameters even they are nil. But i couldn't add nil values to dictionary.

 var postDict = Dictionary<String,AnyObject>  postDict[pass]=123  postDict[name]="ali"  postDict[surname]=nil // dictionary still has only pass and name variables. 

Is there a way to add nil value to dictionary ?

like image 203
yatanadam Avatar asked Oct 24 '14 08:10

yatanadam


People also ask

Why is dictionary unordered in Swift?

Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can't insert a value of the wrong type into a collection by mistake.

Which syntax is correct for declare dictionary in Swift?

Which syntax is correct for declare dictionary in Swift? To declare a dictionary you can use the square brackets syntax( [KeyType:ValueType] ). You can initialize a dictionary with a dictionary literal. A dictionary literal is a list of key-value pairs, separated by commas, surrounded by a pair of square brackets.

How does dictionary work in Swift?

Swift 4 dictionaries use unique identifier known as a key to store a value which later can be referenced and looked up through the same key. Unlike items in an array, items in a dictionary do not have a specified order. You can use a dictionary when you need to look up values based on their identifiers.


2 Answers

How to add nil value to Swift Dictionary?

Basically the same way you add any other value to a dictionary. You first need a dictionary which has a value type that can hold your value. The type AnyObject cannot have a value nil. So a dictionary of type [String : AnyObject] cannot have a value nil.

If you had a dictionary with a value type that was an optional type, like [String : AnyObject?], then it can hold nil values. For example,

let x : [String : AnyObject?] = ["foo" : nil] 

If you want to use the subscript syntax to assign an element, it is a little tricky. Note that a subscript of type [K:V] has type V?. The optional is for, when you get it out, indicating whether there is an entry for that key or not, and if so, the value; and when you put it in, it allows you to either set a value or remove the entry (by assigning nil).

That means for our dictionary of type [String : AnyObject?], the subscript has type AnyObject??. Again, when you put a value into the subscript, the "outer" optional allows you to set a value or remove the entry. If we simply wrote

x["foo"] = nil 

the compiler infers that to be nil of type AnyObject??, the outer optional, which would mean remove the entry for key "foo".

In order to set the value for key "foo" to the AnyObject? value nil, we need to pass in a non-nil outer optional, containing an inner optional (of type AnyObject?) of value nil. In order to do this, we can do

let v : AnyObject? = nil x["foo"] = v 

or

x["foo"] = nil as AnyObject? 

Anything that indicates that we have a nil of AnyObject?, and not AnyObject??.

like image 55
newacct Avatar answered Oct 01 '22 15:10

newacct


You can use the updateValue method:

postDict.updateValue(nil, forKey: surname) 
like image 36
Guiller Avatar answered Oct 01 '22 16:10

Guiller