This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //to register to catch a notification being posted, use this: | |
| let nc = NotificationCenter.default | |
| nc.addObserver(self, selector: #selector(userLoggedIn), name: Notification.Name("UserLoggedIn"), object: nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let aMovement = Movement.left | |
| switch aMovement { | |
| case .left: print("left") | |
| default: () | |
| } | |
| if case .left = aMovement { | |
| print("left") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Mapping to Integer | |
| enum Movement: Int { | |
| case left = 0 | |
| case right = 1 | |
| case top = 2 | |
| case bottom = 3 | |
| } | |
| // You can also map to strings | |
| enum State: String { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // mercury = 1, venus = 2, ... neptune = 8 | |
| enum Planet: Int { | |
| case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune | |
| } | |
| // North = "North", ... West = "West" | |
| enum CompassPoint: String { | |
| case north, south, east, west | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Device { | |
| enum Phone { | |
| case iPhone | |
| case pixel | |
| case galaxy | |
| case prius | |
| } | |
| enum Laptop { | |
| case macbook | |
| case chromebook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let device = Device.googleHome | |
| let phone = Device.Phone.iPhone | |
| let laptop = Device.Laptop.chromeBook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Device { | |
| enum Phone { | |
| case iPhone | |
| case pixel | |
| case galaxy | |
| case prius | |
| } | |
| enum Laptop { | |
| case macbook | |
| case chromebook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Trade { | |
| case buy(stock: String, amount: Int) | |
| case sell(stock: String, amount: Int) | |
| } | |
| func trade(type: Trade) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //If you want to access this information, again, pattern matching comes to the rescue: | |
| let trade = Trade.buy(stock: "APPL", amount: 500) | |
| if case .buy(stock, amount) = trade { | |
| print("buy \(amount) of \(stock)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let tp = (stock: "TESLA", amount: 100) | |
| let trade = Trade.sell(tp) | |
| if case .sell(stock, amount) = trade { | |
| print("buy \(amount) stocks of \(stock)") | |
| } | |
| //prints: "buy 100 stocks of TESLA |