Object-Oriented Programming: Beyond the Basics

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

Lesson 02: Polishing Object-Oriented Programming Concepts

Demo 3

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 created subclasses for person contact and a company contact. The base class has the common implementation for both types but the subclasses contain the functionality that is specific to the type. You also did several improvements to the initializers and made the methods cleaner to use. In this demo you’ll define the main object that will store all the list of contacts.

public class ContactsBook {
  public var contactsList: [ContactCard] = []

  public init() {}
}
var contactsBook = ContactsBook()
contactsBook.contactsList.append(ehabContact)
contactsBook.contactsList.append(timContact)
contactsBook.contactsList.append(kodeco)
contactsBook.contactsList.append(otherCompany)
contactsBook.contactsList.removeAll()
public func saveContact(contact: ContactCard) {
  contactsList.append(contact)
}
private var contactsList: [ContactCard] = []
contactsBook.saveContact(contact: ehabContact)
contactsBook.saveContact(contact: timContact)
contactsBook.saveContact(contact: kodeco)
contactsBook.saveContact(contact: razeware)
public static var current = ContactsBook()
// var contactsBook = ContactsBook() // You don't need this anymore

ContactsBook.current.saveContact(contact:ehabContact)
ContactsBook.current.saveContact(contact:timContact)
ContactsBook.current.saveContact(contact:kodeco)
ContactsBook.current.saveContact(contact:otherCompany)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 3 Next: Conclusion