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

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.

主站蜘蛛池模板: 横峰县| 仁寿县| 湖南省| 惠水县| 绵阳市| 宿松县| 永年县| 万宁市| 平罗县| 巴南区| 珲春市| 尼勒克县| 内江市| 陈巴尔虎旗| 尼勒克县| 呼伦贝尔市| 乐业县| 和硕县| 介休市| 建湖县| 石台县| 确山县| 惠来县| 金沙县| 惠来县| 汶上县| 烟台市| 漳平市| 禹城市| 城固县| 古浪县| 元阳县| 随州市| 万年县| 漾濞| 营山县| 七台河市| 额济纳旗| 航空| 通许县| 葵青区|