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

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.

主站蜘蛛池模板: 清丰县| 明光市| 任丘市| 永年县| 得荣县| 台湾省| 綦江县| 玉山县| 尉犁县| 阿城市| 花垣县| 曲阳县| 南陵县| 铜梁县| 比如县| 静乐县| 莒南县| 钦州市| 策勒县| 榆树市| 兰州市| 承德市| 峨边| 威宁| 东乡族自治县| 胶南市| 田林县| 宁明县| 黑水县| 贵溪市| 于都县| 九台市| 格尔木市| 嘉荫县| 布尔津县| 淮滨县| 兰考县| 双辽市| 镇康县| 鹿邑县| 泾川县|