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.
Create a new swift file under the sources folder named ContactsBook.swift. Define a new class named ContactsBook with a list of contacts inside it:
public class ContactsBook {
public var contactsList: [ContactCard] = []
public init() {}
}
Go to the main playground. Create a new ContactsBook.
var contactsBook = ContactsBook()
Add the following contacts:
contactsBook.contactsList.append(ehabContact)
contactsBook.contactsList.append(timContact)
contactsBook.contactsList.append(kodeco)
contactsBook.contactsList.append(otherCompany)
Run the playground. Everything is correct.
There are two issues with this code. The first, there is nothing stopping anyone from clearing the whole list of contacts without you knowing about it. For instance, you could write this code:
contactsBook.contactsList.removeAll()
The best way is to change the access modifier of contactsList to private and create a function to add contacts to the list without anyone accessing the list directly. Open ContactsBook.swift. Add the following method:
public func saveContact(contact: ContactCard) {
contactsList.append(contact)
}
And add the keyword private on the beginning of line where you declared the list:
private var contactsList: [ContactCard] = []
This, of course, breaks the calling code. Open the main playground file. Since you can no longer access the list, you need call the new method.
contactsBook.saveContact(contact: ehabContact)
contactsBook.saveContact(contact: timContact)
contactsBook.saveContact(contact: kodeco)
contactsBook.saveContact(contact: razeware)
Nice - it’s much better now.
The second issue is if you have multiple places in your app where you need to access the book, you’ll need to create a new instance of ContactsBook in each location. This means each instance will have its own list of contacts which isn’t what you want. Every ContactsBook should be the same.
This is where the keyword static comes in. This keyword when preceding a property declaration, makes this property part of the class itself not the instance of the class. And since you have only one class with a unique name, then you’ll have only one instance of that property.
Add this new property to ContactsBook:
public static var current = ContactsBook()
Now you can update the code in the main playground that adds the contacts to the book to the following:
// 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)
The instance ContactsBook.current can be accessed from anywhere in your app. Its called a “shared” instance since the different parts of your app are sharing it. Some developers even prefer to name the variable “shared” for consistency.
Static properties can make things much easier and they can also make things messy because it makes everything able to access everyone. It must be used cautiously.