Removing items from a dictionary

Next, let's learn how to remove an item from a dictionary. When deleting items from a dictionary, we have two primary ways of doing this. The first uses removeValue(forKey:). Let's add this right above our if...else statement that checks whether the dictionary is empty:

dictPizzas.removeValue(forKey: "chicken") 

Your code should now look like this:

Let's look at the second way of removing dictionary items, which is by using the shorthand syntax. Add the following to Playgrounds, following on from the removeValue(forKey:) variable:

dictPizzas["meat"] = nil 

Your code should now look like this:

Notice that, just like with updateValue(_:forKey:), removeValue(forKey:) will return you the value before it is removed. If you do not need the value, the shorthand syntax is the preferred method.

So far, we have covered arrays and dictionaries, and now we will review one last collection: sets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.97.248