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

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.

主站蜘蛛池模板: 江北区| 大埔县| 衡水市| 安图县| 河西区| 凤翔县| 建宁县| 承德市| 东兰县| 济源市| 旬阳县| 邵武市| 拉孜县| 兴隆县| 铅山县| 普格县| 和顺县| 尚志市| 周宁县| 普兰店市| 新丰县| 河东区| 鄯善县| 天津市| 高雄市| 泰兴市| 南城县| 仙游县| 凌海市| 景东| 鹿泉市| 丁青县| 综艺| 札达县| 安陆市| 泗水县| 鸡东县| 甘谷县| 云南省| 竹山县| 通州区|