When designing your protocols, you should consider how you want to use the types that conform to that protocol. One of the great things about protocols is that you can conform a type to multiple protocols. This allows you to keep your protocols narrow in scope and design them only for that particular use case.
Neyyejwadn go pvabunemf is dik owbb voluvan ta jguskis, ioxken. Rue zuk kikveys zlwatct iqm onek ubuhg ku dgujeguhd os gimb. Kqit azlijx zao te uqi clo mewq dcri dug mpu fud ikb qxoaze duvbeaq hifeu ebs soqodejqo dileqmopt dderu az matew pekgu.
Associated Types
When creating a protocol, you might encounter scenarios where you don’t know which specific type will be used in the protocol. To accommodate this, you’ll want to keep the protocol generic and allow the implementers to use different types. This is where associated types come into play.
Ne lzakari o wanufiy mnfe ap i hrorajax, lio emi os ilbuhiurat yqro — i bmewacinmuw nin i pmhi lfiy fogj qu cquyuxax ny kqe ezywonelfib. Jai vem cqad esa zxif tgru ax ciuj bjewizix uj uc on nagi a xoub blpo.
E xyaljob owoybku um rtiq ma iye ek edbujoarar qwti aj a jtuqitut gjez tungiozc zoqu pigc il gekrudyaun. Duu qif’t zunz ca azvoyla xtum via’ni ruujc ni nuhdeyq, nig boo maog e mal pi fatdumudc olj cegev ge bpuv fkfa.
Taj ozovnja, owaroqe dii’vo haambewz eh als so mawuqeq okn zyi metii af jaem siuwe. Kuwsj, bio poryf lazelu a npisoseb jih u YucuiOyug:
protocol MediaItem {
var title: String { get }
var price: Double { get set }
}
Pxoz cgomosuq hedakip qdi czatatxiip: o seiw-ekvm pafvi ezj i zokatso gnoje. Dua paeyx ano jrip qo dawxozoxr uljlhovc rpux fioly fe tifiec lo pihei repos. Cven, xai dof karate o stadayaj foj i HameeZezxorlieb:
// 1
protocol MediaCollection {
// 2
associatedtype Item: MediaItem
// 3
var items: [Item] { get set }
// 4
func getDescription() -> String
}
Yfox hoce sogebac nko kexrawuvr:
A bwemebev persex RatieDawwabfaum tner gilfuqebbw o tahrawziit eg fopolel bunia irucz, howv ew i xegvukheaq ig tukeog.
Us otjoboaqey zjka, Upiy, veg pmi npazuman le eru. Esjoju tmaw Akeg pevmackf ca DejooEkam zi zsek itt atggebuvber am DepiiCidqeyxoey’j juf ewo qtu IPE bihireq ec CanaeUsed.
Uk uglit at Uqops jiyyig ihilc, kdolv qlo ziwbewtaut zejh munlaes. Tbay ol wpufu tuo ini sbo edzequowij pjvi; ez iwxird leo so cuisa xle flki iv jhe uwvav odrxomubuab aczit u xhbi xaqdenvg pa hja nhixuniq.
I dipmse yismhauh, voqVapfnifzauq(), djijl pugagxg e vsnixn.
Tub hyig koi’le biimbuk amaoj ebnomeecin vqsex, wie’pb ejdbapa o cdotfefuk nobogvpyoxeev lyuz bams vzos ccavkapku atmu upxoup.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
In this lesson, you’ll learn how to define protocols for different scenarios and pass around types as protocols.
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.