Bob Lee Course Part 14 (lectures 24 and 25)
Last part of Bob's chapter 2! After that I'm going to do a consolidation of the entire chapter, making sure that all of those concepts are clear in my head before continuing. Right no time to waste!
Start Time - 11:38
Singleton Pattern
Presumably not an explanation as why people don't have partners! No seriously, I have never heard of this so something else new to learn!
So a singleton is an object that is instantiated once exactly.
Start Time - 11:38
Singleton Pattern
Presumably not an explanation as why people don't have partners! No seriously, I have never heard of this so something else new to learn!
So a singleton is an object that is instantiated once exactly.
class AccountManager {
var userInfo = (ID: "Josh", Password: 43432424)
func network() {
}
}
AccountManager().userInfo
Not too sure about this but hopefully it will become clearer with the next bit. \
Use of 'private' - this means it cannot be accessed outside the block of code that it is created.
So in the above, we only want to create ONE object of AccountManager.
static let sharedInfo = AccountManager()
Right so this static keyword means that it can only be used within the class. But by doing it of the same type as the class, it means that I can then access any properties...don't really see the point of this yet!
class AccountManager {
static let sharedInfo = AccountManager()
var userInfo = (Name: "Josh", Password: 133212)
func network() {
}
private init() {
print("Created")
}
}
As soon as I access the properties, then the private init code runs.
So Bob has lost me on a lot of this. The key thing was that this is used just for one object being created). Right, so the key bit also here is that ONLY the user can initialize the object (Bob's Mum example...) and only once. If someone else tried to do it again, then an error message would come up. Once initialized, the private init print code is read. OK, a little more sense but confusing stuff here.
Conclusion
Bob's summary basically.
Finish Time 11:59 (21 minutes)
So a frustrating final entry but I'm sure I'll get more practise with it. I'm now going to do a separate entry for the consolidation - making sense of this entire chapter before embarking on chapter 3!
Comments
Post a Comment