Last active
August 29, 2015 14:02
-
-
Save akisute/6c9234c15c56b4f4b934 to your computer and use it in GitHub Desktop.
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
| class ExampleShark { | |
| var name:NSString! | |
| var howSwim:NSString! | |
| init(name:NSString!, howSwim:NSString!) { | |
| self.name = name | |
| self.howSwim = howSwim; | |
| } | |
| func swim() -> String { | |
| return "\(self.name) swims like '\(self.howSwim)'" | |
| } | |
| func dangerousSwim() -> String { | |
| return "\(self.name.description) swims like '\(self.howSwim.description)'" | |
| } | |
| func safeSwim() -> String { | |
| return "\(self.name?.description) swims like '\(self.howSwim?.description)'" | |
| } | |
| } | |
| let shark1 = ExampleShark(name: "Shark", howSwim: nil) | |
| shark1.swim() // OK, "Shark swims like '{nil}'" | |
| shark1.dangerousSwim() // CRASH | |
| shark1.safeSwim() // OK, "Shark swims like '{nil}'" | |
| let shark2 = ExampleShark(name: nil, howSwim: nil) | |
| shark2.swim() // OK, ""{nil} swims like '{nil}'" | |
| shark2.dangerousSwim() // CRASH | |
| shark2.safeSwim() // OK, ""{nil} swims like '{nil}'" | |
| let shark3 = ExampleShark(name: "The Average Shark", howSwim: "Swim Swim Swim Lurk") | |
| shark3.swim() // OK, "The Average Shark swims like 'Swim Swim Swim Lurk'" | |
| shark3.dangerousSwim() // OK, "The Average Shark swims like 'Swim Swim Swim Lurk'" | |
| shark3.safeSwim() // OK, "The Average Shark swims like 'Swim Swim Swim Lurk'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment