Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

40. Initializers

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 39. Challenge: Inheritance Next episode: 41. Challenge: Initializers

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 40. Initializers

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve already learned how to write basic initializers. You use them to set any values needed by your types at initialization time! But when inheritance is in the mix, you’ll need to incorporate a few more rules when writing your initializers.

47 var sports: [String]
49 init(firstName: String, lastName: String, sports: [String]) {

}
50 self.sports = sports
51 super.init(firstName: firstName, lastName: lastName)
  StudentAthlete(...... sports: ["Foosball"])
StudentAthlete(firstName: "Bernie", lastName: "Kosar")
49 … sports: [String] = []) {
49 … sports: [String]) {
54 override init(firstName: String, lastName: String) {

}
55 self.sports = sports
   super.init(firstName: firstName, lastName: lastName)
55 … = []
36 required init…
54 required init…
49 //  init(firstName: String, lastName: String, sports: [String]) {
//    self.sports = sports
//    super.init(firstName: firstName, lastName: lastName)
//  }
  
  required init(firstName: String, lastName: String, sports: [String] = []) {

49 init(firstName: String, lastName: String, sports: [String]) {
    self.sports = sports
    super.init(firstName: firstName, lastName: lastName)
  }
  
  required init(firstName: String, lastName: String) {
57 self.init(firstName: <#T##String#>, lastName: <#T##String#>, sports: <#T##[String]#>)
57 self.init(firstName: firstName, lastName: lastName, sports: [])
54 required init(firstName: String, lastName: String) {
    self.init(firstName: firstName, lastName: lastName, sports: [])
  }
54 required convenience init(firstName: String, lastName: String) {
45 convenience init(transfer: Student) {

}
46 self.init(firstName: transfer.firstName, lastName: transfer.lastName)
47 grades = transfer.grades
36 let rudy = StudentAthlete(firstName: "Daniel"…
…
39 StudentAthlete(transfer: rudy)
39 …rudy).sports
65  convenience init(transfer: StudentAthlete) {
    
  }
self.init(firstName: transfer.firstName, lastName: transfer.lastName, sports: transfer.sports)
grades = transfer.grades