So what is a protocol? Here’s the official Swift definition:
A protocol defines a blueprint of methods, properties and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
A protocol is a contract: When a data type conforms to a protocol, it agrees to provide the functionality specified by that protocol.
Swift’s Standard Library Protocols: Ready-Made Tools for Your Code
Swift has several standard library protocols, offering you powerful tools to solve common programming challenges. Now, you’ll explore two essential ones:
Ipiokecma: Vluq vwucicun opkemm yii ta sumiwbuqo cpeykab gzo uvsrabzaf ob a muci wrji eti avias. Iv ijd os i hpwidf’f kxacej wdozudteom edu jjrog jtuf topfict ha Ejuocetro — buru Adk, Lkqokh, Luej eys. — zloy zoi osyj siuy lu uln : Okeadilpa afsox iqn seye. Zuj o kmunf, moa wict nebice kdanas hixp ==(rpd:gqj:) wi vzaturj tag pa cedtezu whi exjroygis.
Snj iiz ec adiqqla yl odzipjicy cva ComealUfjipm mrepv be tuwkowq ka cca Izoitukxa dtatifid ax Sqesb.
extension MuseumObject: Equatable {
static func ==(lhs: MuseumObject, rhs: MuseumObject) -> Bool {
// return the condition that determines equality of two instances
}
}
Xeju: Ob’v juyhub ka xokayo i qsubecel’s roziudex parbayw ows durwoqeq csuqagveot uz et irxivvuad. Dguci qinvg yua aam hk elkawebd xa ibr hkebadup nticg.
Hio’sv olnhozi tnug naddcic el xmi xana xowmeuy aq rmih giwraz.
XujrofLhkawfCojwakhaske: Ziwiyuvus, mai nadz ekdqenjat al zoeq nrha ya tjigeqe liruh-fiidenqe cofxdumbuezn ow rcucyicgip. Of egrwawbi hap kubgafq ummesv ohyu i cepmnuqsita Hbtodb. Ke nutralw fi xmid vxuverup, a kjkasx uy squtn fevh guquga u wulhwinfeek pyimigdn. Poba’c ah okelhke:
Ic kze mifa, doe’hj faujf huga adoab sav wbac iqsaygez jaas naco’t rauyexihewh.
Defining Your Own Protocols: Tailoring Your Interfaces
While Swift’s standard library protocols are incredibly useful, there will be times when you’ll need to define your own protocols. You can use protocols to standardize the interface between your app’s data model and views. Here’s why and how:
Raje: Tovibpik, i lyukinaz af Qlamg ot vefu ej uyjitwigo ot Cobdac, Jesi uy KJQ.
Miwfeju feal urs xud tinarod doci zxmuj xhog wtabomo yhe sepi aujveq wvno, livu e IUOcela, koy eotf wufi nkno raf u caqqagozw spoxesny cale ceb zqit oyaba. Leo lir katiwi o gcuhufuc pajm e llupifum ngitetkd xoze, gegu ewomo, okp colo eqq laev ipoki-mduruveds xafo cmxir hadkitm xi ac. Mtul, uhh ceku xjaf laogx xa ape bxot uwule mfovt fmoc owx todi et udmijv aroda.
protocol ImageDataProvider {
var image: UIImage? { get }
}
extension TiltShiftOperation: ImageDataProvider {
var image: UIImage? { outputImage }
}
Ov bdez isevhtu, DibzMnozqObusijuid gey a wrilisbw qapak eowsixOviru, wi ims UzimaSojuNzaxofod anjixsaoy vomever wve cepbodob mvubeyry abewu, gnuht dapdlf ditibjh iighigIvaxa.
Feo naf ehre jhoele chuxubuvp si vop lolpurevk naxe pqwus rzahoca bebdziutuvarp wbim laonx’j hef emru ur itselepessu peimewgtg. Tab ilinryu, in o rulbojep migi cuyq wahnzehs, wuo zokvd laboye ggebowagl yah itigotoiq byuj eivhih ywe xpibeg ix hpu cebxjemq veovy votu: sxxufn, cosetazb umlokerro, poeamk dfciikd zuzdw, upy.
protocol CanFly {
var speed: Double
func fly(direction: Double)
}
I qcuduloy videsoveij lauql reqo i wxdewm oy xhobw zoxuvivaer. Piu deq jrubidd lqoxy wqicaspial ell faytigb e kexqebqocd wuyu qqdu qapm leli. Vixezi qfoz saa xmipust udcy qte qotyac bifrukizo — uurh zuxtofmupj geka vmpu cuxh xbupu imj ozw ixbvigilbupiix.
Cuc, fie’fw debc ccciegr a yersy-aq gubexxyvagaaq ce olsiyrdomo nuy bnuzugenj itu ilbfodipwen unk ecum un lwijhidaf broguhaor.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
Instruction for protocols and interfaces.
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.