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

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.

主站蜘蛛池模板: 扎鲁特旗| 资源县| 扶余县| 广昌县| 青浦区| 任丘市| 鄢陵县| 津市市| 遵化市| 巴林左旗| 江城| 拉孜县| 南充市| 绥阳县| 普定县| 三江| 辉南县| 阳江市| 永胜县| 施秉县| 马公市| 婺源县| 讷河市| 大埔县| 万州区| 韶关市| 万山特区| 镇原县| 林芝县| 弥勒县| 龙江县| 玉门市| 苏尼特左旗| 五家渠市| 迁西县| 湄潭县| 邢台市| 凉山| 西藏| 浙江省| 罗平县|