Created
February 27, 2019 11:07
-
-
Save cyyeh/766a861a3f1b95058b3e271b302107b5 to your computer and use it in GitHub Desktop.
Swift Initialization
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
| /* | |
| Referenced from Stanford CS193p | |
| #: important | |
| ##: very important | |
| Initialization | |
| - Setting Initial Values for Stored Properties | |
| - ##Customizing Initialization | |
| - Default Initializers | |
| Reference: https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html | |
| */ | |
| /* | |
| Setting Initial Values for Stored Properties | |
| */ | |
| // Initializers | |
| struct Fahrenheit { | |
| var temperature: Double | |
| init() { | |
| temperature = 32.0 | |
| } | |
| } | |
| var f = Fahrenheit() | |
| print("The default temperature is \(f.temperature)° Fahrenheit") | |
| // Prints "The default temperature is 32.0° Fahrenheit" | |
| // Default Property Values | |
| struct anotherFahrenheit { | |
| var temperature = 32.0 | |
| } | |
| /* | |
| ##Customizing Initialization | |
| */ | |
| // Initialization Parameters | |
| struct Celsius { | |
| var temperatureInCelsius: Double | |
| init(fromFahrenheit fahrenheit: Double) { | |
| temperatureInCelsius = (fahrenheit - 32.0) / 1.8 | |
| } | |
| init(fromKelvin kelvin: Double) { | |
| temperatureInCelsius = kelvin - 273.15 | |
| } | |
| } | |
| let boilingPointOfWater = Celsius(fromFahrenheit: 212.0) | |
| // boilingPointOfWater.temperatureInCelsius is 100.0 | |
| let freezingPointOfWater = Celsius(fromKelvin: 273.15) | |
| // freezingPointOfWater.temperatureInCelsius is 0.0 | |
| // Parameter Names and Argument Labels | |
| struct Color { | |
| let red, green, blue: Double | |
| init(red: Double, green: Double, blue: Double) { | |
| self.red = red | |
| self.green = green | |
| self.blue = blue | |
| } | |
| init(white: Double) { | |
| red = white | |
| green = white | |
| blue = white | |
| } | |
| } | |
| let magenta = Color(red: 1.0, green: 0.0, blue: 1.0) | |
| let halfGray = Color(white: 0.5) | |
| // Initializer Parameters Without Argument Labels | |
| struct AnotherCelsius { | |
| var temperatureInCelsius: Double | |
| init(fromFahrenheit fahrenheit: Double) { | |
| temperatureInCelsius = (fahrenheit - 32.0) / 1.8 | |
| } | |
| init(fromKelvin kelvin: Double) { | |
| temperatureInCelsius = kelvin - 273.15 | |
| } | |
| init(_ celsius: Double) { | |
| temperatureInCelsius = celsius | |
| } | |
| } | |
| let bodyTemperature = AnotherCelsius(37.0) | |
| // bodyTemperature.temperatureInCelsius is 37.0 | |
| // Optional Property Types | |
| class SurveyQuestion { | |
| var text: String | |
| var response: String? | |
| init(text: String) { | |
| self.text = text | |
| } | |
| func ask() { | |
| print(text) | |
| } | |
| } | |
| let cheeseQuestion = SurveyQuestion(text: "Do you like cheese?") | |
| cheeseQuestion.ask() | |
| // Prints "Do you like cheese?" | |
| cheeseQuestion.response = "Yes, I do like cheese." | |
| // Assigning Constant Properties During Initialization | |
| class AnotherSurveyQuestion { | |
| let text: String | |
| var response: String? | |
| init(text: String) { | |
| self.text = text | |
| } | |
| func ask() { | |
| print(text) | |
| } | |
| } | |
| let beetsQuestion = AnotherSurveyQuestion(text: "How about beets?") | |
| beetsQuestion.ask() | |
| // Prints "How about beets?" | |
| beetsQuestion.response = "I also like beets. (But not with cheese.)" | |
| /* | |
| Default Initializaters | |
| */ | |
| class ShoppingListItem { | |
| var name: String? | |
| var quantity = 1 | |
| var purchased = false | |
| } | |
| var item = ShoppingListItem() | |
| // Memberwise Initializers for Structure Types | |
| struct Size { | |
| var width = 0.0, height = 0.0 | |
| } | |
| let zeroByzero = Size() | |
| let twoByTwo = Size(width: 2.0, height: 2.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment