Nick Walter Challenges Part 8 (Extensions)
Cracking on with Nick's course. I'll be starting with extensions - I can't actually remember what these are! So it will be interesting to see.
Start Time - 18:00
Extensions
So this next has flummoxed me. Adding an init to a Boolean, but what can I initialise? With ints, strings and doubles there seem to be more options. So I'm coming back to that one and moving on to 3!
//3
Start Time - 18:00
Extensions
class Dog {
var name = ""
var age = 0
}
extension Dog {
var birthYear : Int {
return 2018 - age
}
}
let theDog = Dog()
theDog.age = 8
theDog.birthYear
That was it! It's come back to me. An extension is a way of adding to a previous class etc. But why bother with this - couldn't I just put the extension in the original class?
extension String {
var EXTREME: String {
return self.uppercased()
}
}
var name = "Josh"
name.EXTREME
Ahhhh OK I can extend other types too - like strings! Nice!
extension String {
var EXTREME: String {
return self.uppercased()
}
init(string: String, repeated: Int) {
self = ""
for _ in 1...repeated {
self += string
}
}
}
This is really cool! Nick's added an init, then a for loop to repeat the string a certain number of times! Nice!
class Quote {
var author = ""
var text = ""
}
extension Quote {
convenience init(text: String) {
self.init()
self.text = text
self.author = "unknown"
}
}
OK a rule here - with extension you can't use the designated init so have to use the convenience keyword.
func printIt() {
print(self)
}
This func has been added within the extension. Neat!
extension Int {
mutating func doubleIt() {
self *= 2
}
}
var number = 6
number.doubleIt()
This function in the extension for Int has to be MUTATING because the self value changes. I can't make number a constant either - has to be a variable as the value changes.
Extension Challenges
OK so what we got! These look fine....I think I will be OK with all. Let's do it...
//1
extension Double {
var differenceTo1Point5: Double {
return Double(abs(self - 1.5))
}
}
var newNum = 45.6
newNum.differenceTo1Point5
Phew well that was a challenge!! I had to check from my notes from an early Ray lesson about using the abs method. But creative!
*Paused at 18:48
*Restart at 21:42
//3
extension String {
func combineString(name: String) -> String {
return "You, \(self), have just made friends with \(name)"
}
}
var myName = "Terry"
myName.combineString(name: "Steve")
I am proud of this one! A good idea.
//4
extension Int {
mutating func multipleOf5() -> Bool {
if self % 5 == 0 {
return true
} else {
return false
}
}
Good idea - I am proud of this too!
Let's look at //2 again....
extension Bool {
init(is number1: Int, greaterThan number2: Int) {
self = number1 > number2
}
}
YES! Finally I have something! Lots of head scratching around that one. Let's just see the solutions....
Cool. I spent a while on these but all of my examples/solutions are good and make sense.
Finish Time - 22:22 (1 hour 28 minutes total today)
So that was extensions! These make total sense in my mind now - thanks Nick! His last part is protocols, which I will again go through to get some experience. At this point, I've really got to a solid intermediate status, so I will be mixing it up with Angela's course again. Good stuff!
Comments
Post a Comment