- 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.
推薦閱讀
- EJB 3.1從入門到精通
- 計算機網(wǎng)絡(luò)與通信(第2版)
- Cisco OSPF命令與配置手冊
- 高校網(wǎng)絡(luò)道德教育研究
- SOA用戶指南
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實踐
- 社交電商運營策略、技巧與實操
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- Spring Cloud微服務(wù)架構(gòu)進階
- 智慧城市中的移動互聯(lián)網(wǎng)技術(shù)
- 企業(yè)私有云建設(shè)指南
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- 中小型局域網(wǎng)組建、管理與維護實戰(zhàn)
- 網(wǎng)管員必讀:網(wǎng)絡(luò)管理(第2版)
- 新手易學(xué):新手學(xué)淘寶開店