Bob Lee Course Part 10 (lectures 16 to 18)
Such a busy week - first chance I've had to code for a while! Last time, it was REALLY useful to consolidate and go through what I had done previously. Now I am ready for the next chapter: Object Oriented Programming! Let's go!
Start Time - 16:57
Convenience Init
I know that these are used with classes but can't remember the purpose of the 'convenience' init. Is this just an alternative way of initializing? Not sure!
So this is to do with initializing quickly. Lazy keyword?
class Human {
var name: String
init(name: String) {
self.name = name
}
}
let larry = Human(name: "Larry")
let jeff = Human()
In the above, an error comes up as I have not initialized the jeff object.
Here we go!
class Human {
var name: String
init(name: String) {
self.name = name
}
convenience init() {
self.init(name: "Jeff Green")
}
}
let larry = Human(name: "Larry")
let jeff = Human()
Yes, so the convenience init is so that you can create an object with just the class and the (). Neat.
More specifically, it initialises the designated init method. The difference is that I can set up the value of the property in the convenience init.
let randomColour = UIColor(red: 255/80, green: 80/255, blue: 85/255, alpha: 1)
extension UIColor {
convenience init(r: Float, g: Float, b: Float) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
}
}
OK, there are various errors coming up here - Swift has updated since Bob's course.
But I get the concept - why we use convenience methods. It does not phase me now when things don't work - that's one of the things when using outdated versions of Swift/Xcode! The convenience init method makes life easier for people using the code. The colour example was to show that the Swift engineers have specifically created such convenience init methods to make life easier for the users. Makes sense so far!
Computed Property
The question here is do we always need functions for calculations? No! Of course I know about these already. They are used within classes or structs and carry out some sort of calculation.
There are TWO types - GETTABLE and SETTABLE!
Gettable - can be only read, not modified!
class NumberClass {
var readableNumber: Double {
get {
return 999
}
}
}
NumberClass().readableNumber
After this I CANNOT change the value of this readableNumber. I can get/retrieve the value but not set or change it in any way.
Settable - you can change the value of something
OK...this is not working...
var radius: Double = 10
var diameter: Double {
get {
return radius * 2
}
}
diameter
Again, I'm assuming that this is an issue with this being a later version of Swift than Bob's!
var side: Double = 100
var area: Double {
get {
return side * side
}
set {
side = sqrt(newValue)
}
}
This not working either. Frustrating as I am getting hang of the syntax!
*For newValue, I can change this default name, by putting what I want it to be called in ( ) after set, then using this same name in the set code. OK.
The whole point of computed properties and the use of get/set is that they are less wordy and more concise then creating separate functions.
I'm just going to see what else I can find out about get/set....
Finish Time - 17:50 (53 minutes total)
Well I am completely flummoxed by get/set not working. What I've googled has not revealed anything so I have no idea why get and set do not work.
struct Square {
var side: Double
var area: Double {
return side * side
}
}
let square1 = Square(side: 5)
square1.area
Well that DOES work!
struct Square {
var side: Double
var area: Double {
get {
return side * side
}
set {
side = sqrt(newValue)
}
}
}
That achieves the same thing with no errors. OK, done for now. Just frustrating that get and set did not work within the var created!
Comments
Post a Comment