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

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.

主站蜘蛛池模板: 高州市| 宜州市| 武清区| 阜宁县| 盐源县| 文登市| 通河县| 彭州市| 清镇市| 安达市| 岑溪市| 焉耆| 重庆市| 宁海县| 从江县| 昌江| 颍上县| 淄博市| 延安市| 探索| 徐水县| 额济纳旗| 赣州市| 祁阳县| 新河县| 康保县| 库尔勒市| 新和县| 伊吾县| 叶城县| 汉寿县| 绍兴市| 合水县| 屯昌县| 虎林市| 彩票| 冕宁县| 苏尼特左旗| 常山县| 介休市| 新乡市|