Angela Yu Course Part 13 (up to lesson 133)
I'm back! And so is Angela! Well, the last bit of her epic course is - this time 'Intermediate Swift'. I was going to leave it after Auto Layout but actually felt this would be well worth exploring and completing. THEN it will be time for another Udemy course! Here we go!
Classes and Objects
The first bit of this is recap - a class containing blueprints e.g. properties and behaviours.
The object is an instance of the class.
So first up, I've created a new project under MacOS that is a 'command line' file.
Something interesting - you cannot call instances/values a keyword e.g. var class would be too confusing! Putting back ticks around it is allowed though it should be discouraged!
All of the rest is recap to be honest.
Enums
This is basically creating a new data type. In our example we're working on, it is a 'car' data type.
Classes and Objects
The first bit of this is recap - a class containing blueprints e.g. properties and behaviours.
The object is an instance of the class.
So first up, I've created a new project under MacOS that is a 'command line' file.
Something interesting - you cannot call instances/values a keyword e.g. var class would be too confusing! Putting back ticks around it is allowed though it should be discouraged!
All of the rest is recap to be honest.
Enums
This is basically creating a new data type. In our example we're working on, it is a 'car' data type.
enum CarMake {
case Ferrari
case Mercedes
case BMW
case Honda
}
class Car {
var colour = "blue"
var numberOfSeats = 5
var typeOfCar: CarMake = .Ferrari
}
This is pretty clever - we are using the enumeration within the class. This was like using .normal as part of the Destini app title style. It was UIState - this was an enumeration.
How to Create an Object from a Class
*Useful info - if the icon is 'greyed' in the navigation pane, then it means the file needs to be saved. Useful!
Class Initialization
So the purpose of having class init is to CUSTOMIZE something from the blueprint! In our car example, we want to change something e.g. the colour, the number of seats, the type etc. etc.
One way of changing a property is to change it manually after the instance has been created.
A better way is using init as part of the blueprint (code).
Inits are the part of code used for when a new object is created from the class blueprint.
So inits allow you to override the default settings on all of your properties. It's also the time point in which the object is created - initialised.
Designated and Convenience Inits
So the point here is most of the time - for the cars - we can just produce the bog standard car with no inits.
Designated - compulsory - you cannot ignore it.
Convenience - this is optional!
class Car {
var colour = "blue"
var numberOfSeats = 5
var typeOfCar: CarMake = .Ferrari
init() {
}
convenience init(customerChosenColour: String) {
self.init()
colour = customerChosenColour
}
}
This is really clever! I love it!
Creating a Method
So this is a function within a class. This will show some sort of behaviour - actions.
This is the same diagram from before. Nice and clear!
func drive() {
print("Vroom!")
}
Then this can be called:
standardCar.drive()
Class Inheritance
So I know about this already - the colon and using the previous class (the super/base class).
The class inheriting in is the sub class.
Really good example!
Subclasses inherit from superclasses and they inherit all of the functionality from the superclass.
How to Override Inherited Method
So in this example, the breathe method from the fish class is inherited from the Animals breathe method, but it is modified in some way.
The super.breathe() bit refers to the superclass. So it means do what happened from the superclass, then anything else you want!
class SelfDrivingCar: Car {
var destination = "One Infinite Loop"
override func drive() {
super.drive()
print("Driving towards " + destination)
}
}
*This has really helped to clarify the use of override and why we have to use super. Good stuff!
Optionals
So force unwrapping is where you are 'swearing on your life to the compiler that you - the programmer - are 100% sure that it contains a value'. Nice analogy! So with this case, you cannot have a value containing nil. Using the ! is like manual override. You have to be certain to use it.
So you can make this work by in the main code changing the value of the destination...
class SelfDrivingCar: Car {
var destination: String?
override func drive() {
super.drive()
print("Driving towards " + destination!)
}
}
mySelfDrivingCar.destination = "35 Privet Avenue"
Here is where the safety checks...if statements in this case...
class SelfDrivingCar: Car {
var destination: String?
override func drive() {
super.drive()
if destination != nil {
print("Driving towards " + destination!)
}
}
}
Now it's Optional Binding!
if let userSetDestination = destination {
print("Driving towards " + userSetDestination)
This is the syntax for optional binding. No force unwrapping is needed! This is definitely safer.
So the ? changes the data type from string to optional string! So every data type has its optional counterpart. The ! converts the type from optional string to string. OK, makes sense.
So the optional binding is much safer than force unwrapping!
Well, that is all from this 'Intermediate' module as well as Angela's course up to this point. This has easily been the best course so far. I've learned so much and feel much more confident and secure with my Swift knowledge now.
Here is the next plan:
Tackle Ray Wenderlich's course - again, up to a certain point. This will be great to put more into context. Once I've - like with Angela's - got to a stage where I feel like I've spent plenty of time on that course, then I will go back to Treehouse and start the 'Intermediate' course on there.
Comments
Post a Comment