Object-Oriented Programming: Beyond the Basics

Oct 17 2023 · Swift 5.9, iOS 17, Xcode 15

Lesson 02: Polishing Object-Oriented Programming Concepts

Demo 2

Episode complete

Play next episode

Next

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

In the last demo, you added support for marking a contact as a company contact by introducing a new property that must be set manually when the contact is created. In this demo, you’ll use inheritance to hide this flag and set it automatically from the initializers of the new types.

public class CompanyContactCard: ContactCard {
}
public class PersonContactCard: ContactCard {
}
public override init(firstName: String, lastName: String, phoneNumber: String) {
  super.init(firstName: firstName, lastName: lastName, phoneNumber: phoneNumber)
  isCompany = false
}
public override func addRelatedContact(_ contact: ContactCard) {
  if !contact.isCompany {
    print("Calling super from Person")
    super.addRelatedContact(contact)
    print("Other contact is a Person too. Adding 2-way relationship")
    contact.relatedContacts.append(contactID)
  }
}
public override init(firstName: String, lastName: String, phoneNumber: String) {
  super.init(firstName: firstName, lastName: lastName, phoneNumber: phoneNumber)
  isCompany = true
}
public override func addRelatedContact(_ contact: ContactCard) {
  print("Calling super from Company")
  super.addRelatedContact(contact)
  if contact.isCompany {
    print("Other contact is a company too. Adding 2-way relationship")
    contact.relatedContacts.append(contactID)
  }
}
public func addRelatedContact(_ contact: ContactCard) {
  relatedContacts.append(contact.contactID)
}
let ehabContact = PersonContactCard(firstName: "Ehab", lastName: "Amer", phoneNumber: "1234567890")
let timContact = PersonContactCard(firstName: "Tim", lastName: "Contact", phoneNumber: "0987654321")

let kodeco = CompanyContactCard(firstName: "Kodeco", lastName: "", phoneNumber: "1111111111")
let razeware = CompanyContactCard(firstName: "Razeware", lastName: "", phoneNumber: "2222222222")
public init(firstName: String, lastName: String, phoneNumber: String) {
    ...
    isCompany = false
  }
public convenience init(companyName: String, phoneNumber: String) {
  self.init(firstName: companyName, lastName: "", phoneNumber: phoneNumber)
  isCompany = true
}
let kodeco = CompanyContactCard(companyName: "Kodeco", phoneNumber: "1111111111")
let razeware = CompanyContactCard(companyName: "Other Company", phoneNumber: "2222222222")
public func set(firstName: String, lastName: String) {
  self.firstName = firstName
  self.lastName = lastName
}
timContact.set(firstName: "Timothy", lastName: "Condon")
public func set(phone: String) {
  phoneNumber = phone
}
public func set(phone: Double) {
  phoneNumber = "\(phone)"
}
timContact.set(phone: "555-5555")
ehabContact.set(phone: 12345679.0)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Instruction 3