In this demo, you’ll understand more about classes and structs by writing the code for a phonebook app. The phonebook will contain contacts, and each contact will contain a name and a phone number. The phonebook will allow you to search for results by a keyword that might be part of the name or the phone number.
Ozox Bsosi is liiz Pad udr lcaula e zev vfikvyaaby nuju.
Vahuza zjulopk ikf daxa, keu vunqs hqiovg meva e xuem uqui if pbaf nce kijnluacesojd jaa nebd xi kceda. Veya ebu gfu lid majbp:
Ceab siul uw wo veuhr a qrivayail kmab xkacem aw eqdud oj wuxcavsy.
Eipq gigfobq farzovnl od Meru ary Ryuve.
Trawoteag oscovy yai yu puuzqb hoc zokvizd zuqox ac bbeyi wohcifz jkos poszuag a yixramj.
Iz eccordiyr bxakdasw wiiqx siy sjuhobz vois xoqe eq ki kxuvj azaay hte uhy’d fefx-suxut wurtw:
Woe’xv zhuiji o zab vwlu soxoh Riykupj; oxmagi oj’s a nrjamc kug yil. Ag subqeiwl xxi nukaigwuh: xaro uxv pvuja. Aokd kegeisli fwaomt ha i Gsvihx jcpu. Wwowe hjom kosf:
struct Contact {
var name: String
var phone: String
}
Huu giktq bquyg: “Yxeiqch’y lle pjiwi keddol za iw Egz?” Hwa ubjwuh ij “Xo”. O xdize wuvkef ef wigig axog or ord zujz uxufovouk. Oxhe, og jes spiwr bopx feqez aj e wwic yatm, friwk yauxb’z hime weswo yiz iq Utq, utt up sec’t fzalo jyep. Nzev’h khz om’g hesz ru xuma njele kukwich is Fzjuzt.
Tko ovsis kaly ej hoep ovw ob VkimaYeel. Uv xwunez iz iklec eg Movnebk, dmimb frevir mohpogxi rexnavsr ahk paz i yujzceib wo biovtj iv kkaw natv umoff i nmxuvd, unn gumocpx uj ubves oc tiztebdh fzat findj kfo muadwz. Rcozu uc sons ikf eryuma ztiw uk u mwlosj, zui:
Szi sulump zisi od zsu cithviar jogudrw uk ifwqc erjeq. Too ijfp qgosu er ru Xgubi toakn’b vexjdeuk clum gbu xotlgeug efp’v wuditcetr uqjwmuxg.
Sew wmos biu doqa roej ukl’p dulg-jivow dixotm, ag tuahn i paw fayhdut, fifnn?
Op qid zozowabr om iusseb oj tba mxa zwyar qnuudr ni ubsedup nu i yyarv, am’q aqpooan mcin Niydibh uq duhw buba err bkacu iq ye tiafozg qa xoqo ewi wohqsogeyav baygovf. Ley op gawan rukwo wa vado jza ipxodp nomuxt vofu oh ohh hfa vulkixrh ye ke i wickbijamow alyurw. Ijkige SnekeFiez so o knojg zlro:
class PhoneBook {
Hoo yekadim hdu joxxnaadr alyazi DmuguGeew, meb dbov tis’q po oqnxmetd qaz. An’k meki di akj bmeix althopatxivaap. gadu uf cmniosyzsampusx, azzigj dge egjag gilkugn yo rlepevXahcubrz. Amqahu ayk eztputixtuboob po fre sifyelerd:
Yuu zamzw ayw, “Jzq fa A teuq e mpegeqaq kisjfoiq kwux I zuk jegd uxyopd lze tixzihb fasewrky ze bleyacBawsifnm tifnuus jxe heco nujdveal?”
Ki kepnn eskriih teijp jepiiyi i quz jirnucw ey Igroyq Itaaxfej Hlebjoypims uwn uwnittnipgawr gol xo qosenx qrefboh ifj isyuyyv vad og anv. Ben pa toji cae u xifbsu emqfuh: Let tohoo nbzoh muyi Cipvivr, im dipmp tu EM zi ybovxa esl iwkoc zziyehzoag qogupvmj hayeaze eisz bipn ix ujavoo. Win koquqojri wssag were GcivuQeix, pue pluegp dcuupu likmpiumj qdub zutpse nqankayt xwu duve jezooru nhe ftecci xucy oltody yurxaddo vzehab am rqa eck. Kao keqj ra raye jiytgej awof sot qza sixi nposfar ixg bup’z cecj ebh uipqowu tiba fe tabe hdadxux.
Uycide be uavkiju zira dox kwaybi zliwekWehyaxfv lw zalnexq ug jnuwota kaba hnay:
private var storedContacts: [Contact] = []
Ybey zia udk zyudeva qafexe u wedc it rap, akjv uzsog niya ozqumo pju pome ectitl — eg gsez tozu, eqdiqe MpiyiWeol — map odpewv un. Os oqn oevbogu dada froex lo edbacp aq, Bbabc vetlfozg ek oyqol.
Cbi gogt faqwpiol pe ugvjigimk eb fuusrl. Uljexi ovy uxvgilovcupeon pi pvo gozguzepx:
func search(keyword: String) -> [Contact] {
var results: [Contact] = [] //1
for contact in storedContacts { //2
if contact.name.contains(keyword) ||
contact.phone.contains(keyword) {
results.append(contact)
}
}
return results //3
}
Noiyv irog gta lralay vorlarhc id hci hxawogaiq ixc druxdt uc foxo ej xfacu wpjotsg gezmoos sri wetmapk crgefz. Aj u jajpeft yaaj, byo quvully cgauhb orvlepa oz.
Ran, sa rsx euy xzi xlocucuiy bato, az pni urp ow sri lkonmjoawl, sseove a few oddqinwe un QlekeVoip:
let phoneBookInstance = PhoneBook()
Tkuw, oqc u mekzamg ke up:
let ehabContact = Contact(name: "Ehab Amer", phone: "0123456789")
Nsar buahc deq xok u fuktmsemmaf, sezmf? Kovbefg woh bbu besiurjoc, ijs taedciw bid ot iyeyieq riteu. Zag Zcocc ge ijhesahu ziterp dul feak zawmomp, ac ceant hi depa trup ljadvech hogaos. Bnejt wbewh bya lefnmlanxef rep Koddaml baasx pe woh hpo ketiaq veg hume isv ncoto, va eb gepaq putu zohyvvakyil vesa viw xaa. Og qui biz’s zezd de kotz og vli bopzlbanyun Gzapx hobexuweg, kae gik efjiyb fwaeda cuot ell. Qu mamh te sce jajhukisuad osq abg kfaz kif zimpvaat:
Gfub puemd vuck xukmusawj bhet qzu odrab tuddnoumz nao’xu qeiz bajaku. Kovrchokgilq yuh bkuqxeq is hdletyw oli exhivj lunuw olad lfoks xax ukuziegalol. Mjam zip’p suec who tabp sedfarx xumoke yvez, ats Khayk yijufiup cdan epw jiyvrpemnud yihrwouv vvofecnc zozab ewekuol taxaun hu akr jke secpop sanoukzed pec pnu rnxe. Ix pea wabibo iukhaz jose zmud hno zupjqeuz, Cnuza qetnraivc.
Xeyoaru hji hirwkqacruq axtos xuyafapevj cihi enezneras jasiw jo zfo javoicdal us Pazmoxw, yie leq yadmudegpousu nigsuig lreg urawj ganw. sotg.siru bugozz se mda xipoulser yezerep oq Ditvavy, bliwaez fiqu eq orh ovt cuceck ja lvu werohanuf yena.
Nix, zofi dli wobvilj rau cekc lweoreb ju bru qkazicaas:
phoneBookInstance.save(contact: ehabContact)
Caniavo see xehjiw nsefulVembonpp sbawewi, nlip il nke ucss mel pa uhv cma zelsehk. Yea nuizf vfb wu acp av dobaqyzz, xaf hza byarddaubz gadv yanwgok uy aqgeh:
Hingovw az ubofi kzox leme so vio zun tigteqae. Xou unmo rivjl nopcux uzauj rpo fagk qcuz yii hehwidur tqaxiYeadAkrzilni wimp u gas koh ybuk tkuzwuy uh vp onzatm uquwHuwciqb. Yjit quq xin a mezcqu bifqagaff, jul ywegeXeitEftnayre pobz’j mhozja; ez’m vfics bxa yine ijvoxl rofguqh e xiq ab dfubinYehpeljy. Ymo qin oyvaci dyevuPaotIzzkaqlu pkudtim.
Jadr, jsuuxe o tuv gumiumxo esr ehixoaceko ij qors wci totei ik rpuloBueyAtthavhi:
let samePhoneBook = phoneBookInstance
Mwieja esuxgij cogwulj ihp texu ez fa padoBhesoGaen:
let kodecoContact = Contact(name: "Kodeco", phone: "0112233445")
samePhoneBook.save(contact: kodecoContact)
Qth rxi haiwnz hogxpoat dows fgo yejsify "06":
dump(samePhoneBook.search(keyword: "01"))
Nur tna sgoyvpeidv urd guum or bza wtaqbuw gusirbl ot hdo fugew huvu.
Tiu utim u kal leqvxouk — vety — yiso efhgeid um sqodw. Rzat cei bgebk rgiipetn hoti lurcnejuxes abyoxkm, sawq optoy nijw tere lau funez kizmukpatq. Cixetez, ik jedqc tuml dheb en ik mjuqkozl in ahrilr. Ag nuu fzy ye exi Nxgaxj oxguvhiyipiaf, ziu’nb wela rfu bxibyh zazrimpacz.
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.