- iOS Programming Cookbook
- Hossam Ghareeb
- 186字
- 2021-07-09 18:29:39
How to do it...
Now, let's imagine that we are working on a game, and you have different types of monsters, and based on the type of monster, you will define power or the difficulty of the game. In that case, you have to use enum to create a monster type with different cases, as follows:
- Type the following code to create enum with name Monster:
enum Monster{ case Lion case Tiger case Bear case Crocs } enum Monster2{ case Lion, Tiger, Bear, Crocs }
- Use the '.' operator to create enums variables from the previously created enum:
var monster1 = Monster.Lion let monster2 = Monster.Tiger monster1 = .Bear
- Use the switch statement to check the value of enum to perform a specific action:
func monsterPowerFromType(monster:Monster) ->Int { var power = 0 switch monster1 { case .Lion: power = 100 case .Tiger: power = 80 case .Bear: power = 90 case .Crocs: power = 70 } return power } let power = monsterPowerFromType(monster1) // 90 func canMonsterSwim(monster:Monster) ->Bool{ switch monster { case .Crocs: return true default: return false } } let canSwim = canMonsterSwim(monster1) // false
推薦閱讀
- Designing Purpose:Built Drones for Ardupilot Pixhawk 2.1
- Linux運維實戰:CentOS7.6操作系統從入門到精通
- Hands-On DevOps with Vagrant
- 精解Windows 8
- SharePoint 2013 WCM Advanced Cookbook
- macOS效率手冊
- Android物聯網開發細致入門與最佳實踐
- Linux命令行大全(第2版)
- INSTANT Galleria Howto
- 新編電腦辦公(Windows 10+ Office 2013版)從入門到精通
- 鴻蒙操作系統設計原理與架構
- Learn Quantum Computing with Python and IBM Quantum Experience
- CSS揭秘
- VMware vSphere 5.1 Cookbook
- OpenHarmony開發與實踐:基于紅莓RK2206開發板