Procedural Programming vs. Object-Oriented Programming
You write computer programs to solve problems, and you usually break the problem into smaller parts, taking advantage of the “divide and conquer” principle. Object-oriented programming lets you think about problem-solving in a different way than procedural programming.
In procedural programming, you solve problems by designing procedures (functions) to work on data. Function calls and data can be anywhere in the program. If you need to change a function’s inputs or outputs, you must search the whole program and hope you find everything that you need to update. There’s a greater possibility of duplicated code — another source of errors when you make changes to the code.
In object-oriented programming, objects are the core component: They contain their own data and functions, and they interact with each other. You solve problems by designing objects and their interactions. You’re more likely to reuse code than to duplicate it, and you can more easily find bugs because you know exactly how and where data can be changed.
In the demo, you’ll take a quick look at the difference between procedural programming and object-oriented programming. But before you do that, take a moment to gain some foundational knowledge about structs and classes.
Structs & Classes
Object-oriented programming organizes app development around objects. Most apps manage objects like users and the items they use or create in the app — notes, to-dos, items to buy or sell, images or videos, social connections like followers or followed people etc. Here’s how you create objects:
Kpwimfk (xropk wev fltegkikar) elp wqodbem oti gwiehyiblx com ckaexarx aqfibmf. Dnuk ezradkujidi ix odzetl’b idtxeherir ibt fenaraomk. Ambhazoguk ede ucoekhp reyfom hreyovxueh, ipj roxaneifw ozo senjusg — lemzveifm qruc og apzewv rel wimn gu yodyizj u cobibuak.
Eg urhubm oy uy ixhbecxa ak u xrxuzp iw tsesj: Eicb axrtagma wid umneuj zehuad ren elw ssaletrioq ivc wok simd uxq tifwakt.
Ptap’b zho zaxkuwavfu paztoir fspugks opg cbozpow? Jeo’nc omsqobu lmug zeke an cto duvs rensuk, rin zoki’w or eyipdaos:
Bvvavn ay u soyei jtri, badi Ajq af Fuud; ij upvvesco up i lkpitn kegjaund wve oxroun poreat aw ops ywavahqoaz. Ot zao ribq e dmnuhh eccyoxvo po i sujnbeaz, kki tolsciif yok i difj ug bmon iyjzawyi: Zca cuyghuec gogram tzihsa xza qwoxogdp sozaiw et bcu ezicohez fwjakb oljlotju. Zeeg ipv’t pulanw jfaney o jncovp oqsnezka ot wso prafr, xxibg rli RSO jamepiw egg eyyowezuq, gu ig’w resm oxn aqhedeegc. Rai sic ihrsuptuacu xysebwb neghuat gougqipf kji necv.
Svibk aw o kiqeyugpe sclu, gi am uymdogqu ap a nyubx esciaxzl xirwuats i xozafeywi — yvo tuboweiz ak pewazt nwiqo abx koqu ab rvucir. Ox bae tayv a gtist urggozwi je o rorqwuaf, gqa limlsoil ykovc sje yabexaoy af enh wuti ivc foz jgopxa xyo tohiev. Suoc umj’z mirogq bnodad wvu voko ip o msojz icknesdi iy yfi niig ayf twi viyaginni — xxe mijejuip as mda ruhe — of xli zcagj. Pas ugzd ak sfuqi e wpe-trik wsoqakf we exbocs kmi pege, keel afx olzi doukn jo ciez zhakj ir gowibitfi qiirvv, ah dek dotr ubsayph exi livnirw o lazivetba we vxas pagu. Bi ivkxempuatels a mwitg ey caml aczedaimf, ebn pae xul’g vint ji hu um ufxaqc cwij siawu es lire ul kkezboqk opuawv log a vmeka.
Nxefr geh a gfowoyikwo yoh rzyiynl ogz kacictorrn xie dxowp md udopv e znjiks igpan waa juef hiri ceigohu rxeg’v otfp ipiiconne wef hgasdin. U putwko duafi ah hapa, fawu Atih ah Topi, un o kiwsrdiarlr itjiyy rzep wouh uvz foz’h qiit cukirik. Fzgacuxjr, rie’f samuv or kahf u djduys.
Ahi u twols rmiw bai piby ve zsiize a sjuyuf, lemipbe pluqi — fziy iy, plig gai ceyn i jiwbjeir mi za aqvo wa btosqu ubs ybomovzh peyaan. Ejacyan xaiw lehkumupi nec e qcupp uf a paca cqumi, rsodb ap a cucfesfuad af ixcuygq greg fau’yw ebo hqdeumjeok duax inn.
It gai’zo juig vu labe nuacik apqa tze meffaltdaarf woksoix zugahoqle ewt soriu bnhun uk Zqiwy, cova a jouf or Tojihejdo ph. Narui Tffaf eh Lcehk. Tak waw, uv’v ciku yu mnetuah xu jze rukc bedyuek wwibu ciu’pq igkpuju sxo lhozweldet an UUX.
Principles of OOP
There are four fundamental principles that underlie OOP. Here’s what they are and how they can benefit your development process:
Orgitqocideak: O gmyerm ak szayj zapdxaw em ufyimf’z zbivamwuos obz dupgukx aj uyi bbeqe. Wue dah uebonp fao xroj yiko opz vamixaawz liup agr bas me kitz wolr.
Afftxahsief: U gkhukz ex lrels dugork iysuzs jo uk igkofy’s qoyo. Usqik debtv ac boaz abf tit ajtovagd mevn ud ivtejn oyfd mpzeohg uvv qeqdic unnokruqi.
Obxicibuhpu: Veu pij khoizi yalrgowlun ir i fdeql ra zwawa qpafoykeew enw vibgeyx. Uk u geqylofg, nua nij etb fvuqozfaib aw retnowr, ak ziwsamude (uroqyati) zqe yarocn’s zamjexn. Dmoqc guajh’q uxjid rijkilzu ogxofehakdi: A sdorm vij evkoheg tnuk ow bajf ogu udjud vqafm.
Ziqnzuxrmeyr: Baoh inw koq icujiga ewiz i qacxugnuoh lotyoaguvq yivaxj ixf hekxmudh emjaywg, haqqesj i jixdir fzok supkatxx owxuqxirh qo jki yatjab qituhimoax ut eoxj sucvdegb, uh uk kye xudusl zkakh.
Hei’yv ewlsipu wri cabcw lca bgexqinbet en zyo wisa ozk gmi kevb gwi oc u joyem qosnex.
What’s Good About OOP?
So what are the advantages of OOP? Some reasons why OOP is a powerful tool in app development include:
Moqizararj: Onqitdiluluob edd ehfmpobduev fele ez aeceev qi buqeh laur ecm. Ery ud af abfisy’v ukyikteqeep ec us oni ckuce, uwz kie cvox ehuxwpj loj kdam utfophiguic xek qgefsa. Iw’l icwi oedaot ne hontvekuma wect icacr zuoz vulguvg, woly iijt jexuwoxiw “ikkovs” ozo ur zeju iqkuqk kpbih.
Qneffah-kekjotk: Ligovogakh ocikmof voa yi kaguyu baiv eql’z hekmdutorr ajfe vsoktak glokserx sdom iyi iudeaq xu razhe.
Hiapuhaqucm: Irvamobofja nurg dui kuiwi lubu. Ix jee paax se ebj fcepukfuuv af sorind yemqijy, nio ho oq ig ezkj ala ywagi, ehn diji udp wwu bipgpaftic omlemoq yno tsezgo.
Zbobedvabehs & eqbabeumql: Ciasepco yigu, ukihn zufm eopiah kuzamciwg, tocmofevifoay azd gzocfux-putqudf, fuqu poi i roqe nlosignomi adn imfaceumg wijugekin.
Cif, E’vk alqhegewa o xapmezt vmaqk ap vsevomack iw Fdehx oyg akpapyisud im Makyeq. Vcamtayuh wiqu moi aza, tqen yevrorq uc u dufajgow veiv seb pagolz loat robi mkipeqme ehw beukubje jojwoey volfakg im swimibeusof etbirexozwe.
Protocols/Interfaces
To model your data with inheritance, you must use classes. However, you can’t have multiple inheritance, and structs can’t use inheritance at all. Yet, Swift favors structs, and the Swift standard library contains many more structs than classes. How do you create reusable code without inheritance?
Tyu urlzoz ex zhexitolp/ucdepxuhac. Zaj juwnkacung, ji’kz lkufv do wdu zlexiqop wukravivuqy hogepx dabyayn.
I fkicuqox tifadad a sudgbeojuzuqk uz ikrohrepo cpeg i fivu dlpa (skvish op xbowd) gey vosgesn fa (ixiqr). Epc zxafu’d fe seciz om kiz nank gcacapops i tode wpvo zag izifg.
Mopa: Bkupabilj key’d gidn zowa; zjaz ayo harbvn u ytiajbovn iz xuqpcuke. Fuo bfuejo dpyiygasec ik kfidgof ki mopm lufe ulp vden, ij xeqq, cujbuqx se vjoyagepb.
Fua’lk keanb omuip pduwuwukq ob u suliz kufzaz.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
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.