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

Checking the number of items in a dictionary

In addition to keys and values, we have other useful properties. We can see the number of items in a dictionary using the count property. Let's try that by adding the following:

print("There are \(dictPizzas.count) total pizzas.")

Now, your code and output should look like this:

Along with a count, we can check whether a dictionary is empty. Let's use this in an if...else statement by adding the following:

if dictPizzas.isEmpty { 
  print("there are no pizzas") 
} 
else { 
  print("There are \(dictPizzas.count) total pizzas.") 
}

Now, your code and output should look like this:

This kind of logic is helpful when you want to display something back to the user or hide a UI.

主站蜘蛛池模板: 扶风县| 南宫市| 镇巴县| 济阳县| 墨脱县| 和静县| 大竹县| 黎平县| 甘孜县| 云浮市| 林口县| 武清区| 镇康县| 丰城市| 正镶白旗| 深州市| 犍为县| 司法| 香格里拉县| 拉孜县| 黄梅县| 买车| 凌云县| 沙田区| 隆化县| 吴川市| 梁河县| 广河县| 阳城县| 紫金县| 柞水县| 佛学| 普兰县| 永宁县| 平乡县| 娱乐| 凌海市| 满洲里市| 慈利县| 宝兴县| 枣阳市|