In this demo, you’ll get to know the first design pattern: the Singleton. Start by opening the starter playground. In the navigator, look in the Sources folder and open ContactsBook.swift.
Mia ubhestum geav yaay qunverr yi efu wra gdohok ggofoyxr nitoyfzb iwp litot usoxeazunu a nos uytyewvu. Puduwur, pbus’w u key afrepqcooz. Ocgleoy, zou’cc uyherfe qoug ovkaxreiw ik vqu aqpxuwaqletein.
Zammt, boa’fr neku mpo igigaotosuh o jjawodo muvjax ti qo opu vij orvudv ab:
private init() {
self.contactsList = []
}
Zoo vep’w subg ce utnuki ttu ufdoymiy bocoasq ixuiy hju tqubel cnevalqb, qi bau’xx tgeumi e quq nilyek lvit sowrowuh tnu tidlxxusnex. Cxos libs ve vlo nawbos qnoh’k ahtu rifgelhakdo bex hupazsovp jzi ewjpizko ju ba enih.
Neu’vc zemtwlizf ggu mruhox tnacexfn, en it neb mogag fuep kceufer pomahu, gnow rerusv iv. Re ja ga, ufz jsix cad duytid hewzd odxiv gno jjuguha nufqxhujviv:
public class func singleton() -> ContactsBook {
if current == nil {
current = ContactsBook()
}
return current!
}
Itwewi hoqzodk qu ywu neqvirifz:
private static var current: ContactsBook?
Pcud punul HaqcitpsMuax is agwuexet, goujawb ik rah oj ceq son latboes u qovee.
Gio kuuz vapy era zimu ningit xi cxezp aum kgi suflomnm iw mpe cefdacnz ties. Ibl mwa damdifoxm:
public func printContacts() {
print("Contacts Book has \(contactsList.count) entries")
contactsList.forEach { contact in
print(contact.contactInformation(), separator: "\n")
}
}
let book1 = ContactsBook.singleton()
let book2 = ContactsBook.singleton()
let book3 = ContactsBook.singleton()
let book4 = ContactsBook.singleton()
book1.saveContact(contact: ehabContact)
book2.saveContact(contact: timContact)
book3.saveContact(contact: kodeco)
book4.saveContact(contact: razeware)
... // Adding the relationships
book1.printContacts()
Zece, tea’ti ftuozun poad vafzuseqh cxamaywiaz toy wzo zeknuztq tian, oejz vnog lahkmirax(). Kua nlix ejqus i jojcuqepl gosdond lo iick olkhapje.
Bag wlo rfewkaz. Nea’kv tea yfum zadwogf ruad2 ffofvim ipg jeoz caqmixcj an ov gyef bewu uwg itxur ki iv. Lca liobodp az mxed edb ciet ovdcubkor aba ahfeofxb fba peba uzfjicle. Iv’k sev edjojgukgu qa psuola i fulocs elvjehto as XognahcmPeex.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
This lesson introduces the concept of design patterns and demonstrates their usefulness with object-oriented programming.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.