- iOS 12 Programming for Beginners
- Craig Clayton
- 122字
- 2021-07-02 15:05:03
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.