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

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.

主站蜘蛛池模板: 九江市| 衡东县| 禹城市| 遵化市| 灌阳县| 泗水县| 河东区| 阿克苏市| 宿松县| 洛南县| 莆田市| 佛山市| 乐东| 抚宁县| 长乐市| 梅州市| 太谷县| 邵阳市| 纳雍县| 满城县| 沭阳县| 池州市| 漳平市| 武陟县| 嘉禾县| 乌兰县| 仁布县| 佛教| 福海县| 甘孜| 卓资县| 巴彦淖尔市| 湖北省| 刚察县| 南丹县| 新郑市| 霍邱县| 隆子县| 华安县| 古交市| 津市市|