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

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.

主站蜘蛛池模板: 南宫市| 南华县| 芜湖市| 沁阳市| 磐安县| 高安市| 陇西县| 民丰县| 揭阳市| 吉安县| 台东县| 凯里市| 西乌珠穆沁旗| 韶山市| 抚远县| 桓仁| 招远市| 马边| 鱼台县| 平果县| 冀州市| 舒兰市| 太和县| 全州县| 水城县| 射洪县| 连山| 多伦县| 静安区| 石台县| 翁源县| 四会市| 湖南省| 咸丰县| 阜宁县| 社会| 和平区| 舟曲县| 临洮县| 新津县| 孝昌县|