![iOS 12 Programming for Beginners](https://wfqqreader-1252317822.image.myqcloud.com/cover/76/36699076/b_36699076.jpg)
上QQ阅读APP看书,第一时间看更新
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:
![](https://epubservercos.yuewen.com/2E52D1/19470383901517806/epubprivate/OEBPS/Images/deeff95c-9ebe-4c1c-a789-d0b874a460c5.png?sign=1738890505-rIbzifpKLHJTjnZjyvVGUsvlBKbxYSvR-0-5a9ae2bbf067df214200642afc494b35)
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:
![](https://epubservercos.yuewen.com/2E52D1/19470383901517806/epubprivate/OEBPS/Images/805fe7c9-8d10-44ce-b7e0-e2a9a509e031.png?sign=1738890505-MJh3h1xNHJMioxb56c7i0ggVxuVaHSZw-0-78a7b297bf080087258719be15685cdb)
This kind of logic is helpful when you want to display something back to the user or hide a UI.