Created
August 23, 2016 22:24
-
-
Save carlynorama/49fa80246b2c1f7060ace4bc8d1f80ca to your computer and use it in GitHub Desktop.
Getting down with classes, early days Swift 3
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
| // Created by carlynorama on 2016-08-22 | |
| // License CC0 - No rights reserved. | |
| // Modified from https://www.udemy.com/complete-ios-10-developer-course/learn/v4/t/lecture/5449162 | |
| import UIKit | |
| class Ghost { | |
| var isAlive = true | |
| var strength:Int = 9 | |
| var color = "grey" | |
| init() {} | |
| init(color: String, strength: Int) { | |
| self.color = color | |
| self.strength = strength | |
| } | |
| func kill() { | |
| isAlive = false | |
| } | |
| func isStrong() -> Bool { | |
| if strength > 10 { | |
| return true | |
| } else { | |
| return false | |
| } | |
| } | |
| } | |
| //This doesn't work... not even a little bit | |
| let ghostNames = ["blinky", "inky", "pinky", "clyde"] | |
| let ghostColors = ["red", "blue", "pink", "orange"] | |
| let ghostStrengths = [7, 8, 9, 10] | |
| var ghostCollection = [Ghost]() | |
| for (index, ghostname) in ghostNames.enumerated() { | |
| var newGhost = Ghost(color: ghostColors[index], strength: ghostStrengths[index]) // <-superbad trying to use a string as a variable name like this | |
| // newGhost.color = ghostColors[index] //if don't make custom init | |
| // newGhost.strength = ghostStrengths[index] //if don't make custom init | |
| ghostCollection.append(newGhost) | |
| } | |
| var blinky = ghostCollection[0] | |
| var inky = ghostCollection[1] | |
| var pinky = ghostCollection[2] | |
| var clyde = ghostCollection[3] | |
| var ghostInTheMachine = Ghost() | |
| print(blinky.isAlive) | |
| ghostCollection[0].kill() | |
| print(blinky.isAlive) | |
| print (inky.isStrong()) | |
| inky.strength = 20 | |
| print(inky.strength) | |
| print(inky.isStrong()) | |
| clyde.kill() | |
| print(clyde.isAlive) | |
| print(ghostInTheMachine.color) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment