Chris Ching Course Part 2 (Classes, Inheritance, UIKit, Initialization, Optionals)

This Chris Ching course is different for many reasons - it's not practical in terms of projects; it really is just a refresher/consolidation for me at this stage. It's reassuring that what I know is at a deeper than level than what he is explaining. This makes sense as the course is for beginners! It's also interesting how much shorter it is; I'm 43% through already! Anyway, I'm going to crack on today with classes, then it will be a whole mixture of other bits. Let's go!

Time started: 13:14

Classes

So here are some values:

var blogTitle = "Josh's Swift Journey"
var blogBody = "Swift information"
var author = "Josh Gonet"

var posts = 5

The point with this is that it's difficult to do much with, especially if you want to make new versions etc. 

Using the class keyword...I know all this so am not going to type much more!

class BlogInfo {

var blogTitle = "Josh's Swift Journey"
var blogBody = "Swift information"
var author = "Josh Gonet"
var posts = 5

}

No init needed as everything has a value!

class BlogInfo {

 var blogTitle: String
    var blogBody: String
    var author: String
    var posts: Int
  
    
    init(blogTitle: String, blogBody: String, author: String, posts: Int) {
        self.blogTitle = blogTitle
        self.blogBody = blogBody
        self.author = author
        self.posts = posts
    }

}

let myBlog = BlogInfo(blogTitle: "Swif Journey", blogBody: "Swift information", author: "Josh Gonet", posts: 5)

myBlog.author

All this is now separate to what Chris is doing. No inits were used in his - he must have been hiding the error somehow! Ah I know, he has put in empty strings, but has still initialised each value. 

Functions can be added - simple stuff. 

All good. 

Inheritance

All fine so far. 

OK I've been doing some research to help me with the init for subclass:

class RacingBlog: BlogInfo {
    
    var contributor: String
    
    init(blogTitle: String, blogBody: String, author: String, posts: Int, contributor: String) {
        
        self.contributor = contributor
        super.init(blogTitle: blogTitle, blogBody: blogBody, author: author, posts: posts)
        
    }
}

It's very wordy so I wanted to make sure I got it right!

I've added a new variable (contributor). I've then put in all the inits with the data types (strings/int etc.). Then I've done the self for contributor as it's the new value for this subclass, then done the super, as all of those values are from the super class. Phew!

I've also added in this:

  override func addPost() {
        posts += 2
    }


So that was all pretty basic really - I went into more detail than Chris did at this stage - no mention of initializers, which often catch me out. 

UIKit

Chris shows an interesting diagram that UIButton inherits from UIControl, which is from UIView, which is from UIResponder, which is from NSObject! Fascinating actually to see that it is a long way down in the chain of classes. 

NSObject is the superclass/parent class of quite a few!

This was a load of theory - nothing much more of note. 


Initialization

Yes here it is! This is the thing that gets me in knots. So here we go....

The first bit is all fine - the basics of using init. 

There's nothing really new here but it reaffirms to me that you HAVE to put in inits when a value is not set but you can still initialize with values you have put in. Setting new values (If they're variables). 

Optionals

Something else I've never been too confident with. Let's have it!

So I get when we use these...

class BlogInfo {

    var blogTitle: String
    var blogBody: String
    var author: String?
    var posts: Int

}

I know that's incomplete - it's just to show the ?. It means that author MIGHT be empty. Now there is an error handling code that can be added but I will leave that for now...

init(blogTitle: String, blogBody: String, author: String?, posts: Int) {
        self.blogTitle = blogTitle
        self.blogBody = blogBody
        self.author = author
        self.posts = posts
    }


let newBlog = BlogInfo(blogTitle: "d", blogBody: "r", author: nil, posts: 6)


Chris uses the box analogy - it is wrapped and labelled but there may be something else inside! Or it maybe empty!

Right so now we're moving on to Optional Binding...

This is what I need to look over!

if let actualAuthor = myBlog.author {
    print(actualAuthor)
} else {
    print("No value!")
}

This is better. The error but I'm not so sure about - I could have created my own by enum!

guard let actualAuthor = myBlog.author else {
    throw NSError.init()
}

Looking at the info online, guard isn't always better - it's good for an early exit! So if/let is absolutely fine. 

Another way of unwrapping...


if myBlog.author != nil {
    print("myBlog.author has a value!")
} else {
    print("myBlog does not have a value!")
}

Or this...

if myBlog.author != nil {
    print(myBlog.author!)
} else {
    print("myBlog does not have a value!")
}

Forced unwrapping is to be avoided UNLESS YOU KNOW THERE IS A VALUE!!

Time: 14:26 (Total time - 1 hour and 13 minutes)

So some good stuff again. I'm still not too hot on optionals. The initialization bit is coming and I will get used to the syntax with subclasses etc. That just needs more practice. So all in all, positive again and good to go over these bits. Tomorrow I am going to finish this consolidation course with properties, designated and convenience inits (yes!), arrays and dictionaries. 



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)