官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 准格尔旗| 剑川县| 呈贡县| 五家渠市| 新巴尔虎右旗| 教育| 来凤县| 印江| 龙游县| 罗平县| 霍林郭勒市| 三门峡市| 甘肃省| 长海县| 紫金县| 通江县| 陕西省| 陇南市| 弥勒县| 延边| 会同县| 红安县| 罗甸县| 年辖:市辖区| 洛川县| 璧山县| 绥棱县| 二连浩特市| 清苑县| 广西| 栖霞市| 股票| 甘孜县| 土默特右旗| 特克斯县| 汉寿县| 阿勒泰市| 仪征市| 年辖:市辖区| 邢台市| 荣成市|