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

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.

主站蜘蛛池模板: 武乡县| 台安县| 永靖县| 贵州省| 漯河市| 磐安县| 顺平县| 昌吉市| 兴安盟| 万载县| 桂平市| 昌吉市| 炎陵县| 彰武县| 那坡县| 大竹县| 洞口县| 聂荣县| 峨边| 梨树县| 邓州市| 四子王旗| 扎鲁特旗| 苍梧县| 松滋市| 布尔津县| 鹿邑县| 营山县| 桃园县| 湛江市| 东丽区| 井冈山市| 新津县| 连云港市| 囊谦县| 凉城县| 邹城市| 鲁甸县| 四会市| 金阳县| 安顺市|