Last active
August 22, 2023 14:27
-
-
Save clarkeben/ae0417583fea643b77436f9e8a0e34ab to your computer and use it in GitHub Desktop.
Self Vs. self
This file contains 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 LegoCar { | |
var color: UIColor | |
var wheels: Int | |
mutating func addNewWheels(wheels: Int) { | |
self.wheels = wheels // Using 'self' to refer to this specific Lego car | |
} | |
} | |
var newCar = LegoCar(color: .blue, wheels: 2) | |
newCar.addNewWheels(wheels: 4) |
This file contains 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 LegoCar { | |
static var defaultColour: UIColor = .red | |
static var defaultWheels = 4 | |
var colour: UIColor | |
var wheels: Int | |
static func create() -> Self { | |
return self.init(colour: defaultColour, wheels: defaultWheels) | |
} | |
} | |
let newBuild = LegoCar.create() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment