Bob Lee Course Part 4 (lesson 7)
Back with Bob! Error Handling makes a lot more sense to me now. In my mind, you have if/let, guard/let and the ?? options to see if optionals have a value. Also, you can build in specific error handling options with a enum with messages, which follow the Error protocol. Then the do/try/catch block of code is used to test for errors. Error handling can also be built in to functions, to test for an error and also in classes within the initialisation. Like I said, I know a lot more this time round when I did EH with Treehouse! Right let's go!
Start time - 12:20
Type Casting
It's useful to start with the 'problem' Bob describes, to give this some sort of focus -
So the keyword 'as' I've used before, but I can't remember how!
Here's an example with 'as'
Start time - 12:20
Type Casting
It's useful to start with the 'problem' Bob describes, to give this some sort of focus -
So the keyword 'as' I've used before, but I can't remember how!
Here's an example with 'as'
let label = UILabel() as UIView
class Human {
func introduce() {
print("I am a human")
}
}
class Korean: Human {
func singGangnamStyle() {
print("Oppa Gangnam style!")
}
}
let bob = Korean()
bob.introduce()
Ok I think I'm figuring out what type casting is - converting type! Yes, that's it!
let newBob = bob as Human
So newBob does not contain the Korean only method of singGangnamStyle whereas the original Bob did.
UILabel is a subclass of UIView. In the first line of label, we are converting UILabel to UIView - which is 'upcasting'. UIView is the superclass - that's why. Opposite is down casting - going to a subclass. Cool.
Downcasting may fail - I guess the reason why is that there MIGHT NOT BE anything to downcast to. There are two ways to downcast - a safe way and a 'forced way'.
var name = "Josh" as Any
var number = 33 as Any
var anArray = [name, number]
Right in this example I have specified Josh and 33 as Any which is technically upcasting as Any is above String/Int. To downcast from Any to String/Int....
let newName = anArray[1] as! Int
That's good.
let newName = anArray[0] as! Int
Now there's a problem - "Josh'" cannot be an int but I've forced unwrapped! So there is an error....
let newName = anArray[0] as! String
This DOES work but it's still not safe!
The safest way is the optional way - implicit downcasting.
let newerNumber = anArray[1] as? Int
And if I put in the wrong type, then.... nil is returned, which is SAFE! I do still need to unwrap the optional though.
Why upcast?
Well it lets you group things together. To do this example, I'm going to create a sub class of Human....
class British: Human {
func sayHello() {
print("Hello old chap!")
}
}
let bob = Korean()
let josh = British()
let cath = British()
let jenny = Korean()
Now I have four different objects - two from each of two different types, which all share the fact they're from the superclass of Human.
let humans: [Human] = [bob as Human, josh as Human, cath as Human, jenny as Human]
That's cool - I have upcasted all of these so that they are in an array, all as Human class!
let humans = [bob as Human, josh as Human, cath as Human, jenny as Human]
Don't actually need to have the : [Human] bit due to type inference!
Even better still...
let humans: [Human] = [bob, josh, cath, jenny]
With this, we don't even need the as as we have specified the new type in the [Human] bit.
Even better still - now I know what will happen....
let humans = [bob, josh, cath, jenny]
Swift knows to GROUP these together as they all share the superclass of Human, and arrays HAVE to be of the same type! Neat!
for human in humans {
if let korean = human as? Korean {
korean.singGangnamStyle()
} else if let british = human as? British {
british.sayHello()
}
}
Good old for-in - always a great way to go through values! In this example, I've created lots of temporary constants to store values for that purpose. Basically, they are only in the scope of the for-in loop.
So this is a real life example of objects being created, which will then be put in an array. When they have, they are upcasted together as two of them are subclassed from UIView. So in the array, they are all upcasted to UIView! Nice.
The last little bit I've skimmed over as I've learned plenty about Type Casting for now1
Finish time - 13:24 (1 hour and 4 minutes, a little interrupted)
Great stuff again! I remember doing a bit about type casting, including down casting before, but I don't remember it being this logical and practical. Bob does do a good job making it accessible and clear. Next time it is Generics - something again I can't remember anything about. It's definitely worthwhile going through this course with Bob, feeling good about what I'm learning and expanding my knowledge about.
Comments
Post a Comment