Bob Lee Course Part 11 (lectures 19 to 20)
Schoo inspection is over! This thankfully means that I will have more time to code in the coming weeks. Last time I focused on computed properties including get/set. Now not all of that worked before. Never mind. Anyway, we're starting now with 'property observers' - getSet and willSet. Let's go!
Property Observers
Start Time - 15:04
So that is the difference between willSet and didSet. Interesting.
Property Observers
Start Time - 15:04
So that is the difference between willSet and didSet. Interesting.
var myGrade: Int = 80 {
willSet {
print("Hey something happened!")
}
didSet {
print("Hey something else happened!")
}
}
myGrade = 100
Simple enough but once again my playground seems to be using the same error message of the code not compiling....
Paused at 15:09...
Right I am doing the update on Xcode - we will see if that helps! It could be the mac needs an update too...Anyway, I'm going to crack on just by looking at Bob's video without actually coding!
Restart at 15:11!
You can use 'newValue' again - that's an automatic constant that is provided by the Swift language.
oldValue - this refers to the previous value basically. So 80 in the case below.
So I guess the point of the property observers are to give warnings when a value is about to (willSet) and has (didSet) changed!
Again, like computed properties, you can specify the names of the values e.g. oldGrade/newGrade or whatever you want.
So with this you can see the specific values put in with willSet(newGrade) and didSet(oldGrade). OK fine so far.
You can use if statements to make the didSet more specific:
Paused at 15:25
Restarted at 21:17
So plenty of hours in between but I'm picking this back up with an updated Xcode. Let's see if that works....!
Paused again while another update goes on!
Restart at 21:28
So the next question is what application is there for property observers? Well Bob explains a login example.
Restarted at 21:17
So plenty of hours in between but I'm picking this back up with an updated Xcode. Let's see if that works....!
Paused again while another update goes on!
Restart at 21:28
So the next question is what application is there for property observers? Well Bob explains a login example.
var loggedIn: Bool = false {
willSet {
print("The user has tried something")
}
didSet {
if loggedIn {
print("The user has switched from \(oldValue) to \(loggedIn)")
}
}
}
So again, the willSet print line prints out to the console first, then the didSet print line will come up.
There are lots of other real life applications with this - background colour, pop ups, password changes etc. All sorts of things basically!
So how are property observers different to computed properties?
So it is about being OBSERVED that's the key thing here. The will/did set do not get called until the value is actually changed. TRACKING DATA - used with PO.
Failable Init
Right so a recap first with using enum to create custom errors. Then using the throw keyword instead of the return symbol.
So in the above, the init uses 'throws' and then what error type, depending if the name is empty or not.
Then error testing can be used....
class Blog {
let name: String
init?(name: String) {
if name.isEmpty {
return nil
}
self.name = name
}
}
let newBlog = Blog(name: "")
So newBlog is of optional type.
if let blogName == newBlog.name {
print(blogName)
} else {
print("There is no name")
}
Now that did not work. It's to do with it being failable - i.e. no value...
if let blog = newBlog {
print(blog)
} else {
print("No value")
}
OK even better -
if let blog = newBlog {
print(newBlog?.name)
} else {
print("No value")
}
The issue with this is that the value is not unwrapped yet.
Stopped at 22:04 (total today - 54 minutes)
Well, some good stuff here. Stuff which I will need to go over. Property Observers were not new but there role has been clarified for me - tracking data basically. And as for failable init, well I'm not sure as to the advantage of designing purpose built error handling code.
As this has been four courses now since a consolidation, I think I need one of these before moving on. Those consolidation sessions are where I really understand it on a deeper level and have the chance to 'OWN IT'. Then it will be four more courses before I finish this chapter.
Another good thing with this course is that it is at a higher, more complex level than anything I have gone through AND stuck to. Usually by now I would have given up! But I'm keeping up with this, even though I do need to pause, think, stop and check more often. That's fine though - I could spend several more months on this course!
Looking at the longer game, after this course I have another Intermediate one lined up - from 'Sandra'. Anyway, the ULTIMATE aim is not to do as many Udemy courses as possible, but to actually start branching in to app development. I will review that after I've completed all of Bob's course!
Comments
Post a Comment