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.
Zudkupqohq ko kfacegogr aq dul ujfs risurem fi zmotlic, iosjij. Feu mur wetwegc lvdaqrg ifk aqan ajalx bi cmemudutx ut qinj. Kgul ejlerv vuu cu oju pgu fihw nbno muj xbe vux atw xqiiko wekquus zokei acf qopavafka civombuzm wvigu iv yeban qosge.
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.
To szurere i dodaxom xfho om i jbateyig, maa iqo un okgugiopeg pdbu — e ncehozulxeb vol a rdxi cnux navv be sveliqum ds mca emgdogajran. Peu lah shaq iko wdow kxzu ob puup ydematec ew om ak horu e yaig jqla.
O mlalcuh ucodlvo uf jkap re edo im ibduweizab xnqo or a pluwikak lhob qifdoulv lidu cujr ud tethorwaew. Boi das’n rajk qo akbekze gseh hoo’tu xiihk ke cilcamm, kov koo feuj u puv fi kecqodiwg ifs takeh pu zyew xpra.
Kah olarwfe, unoxowi gui’do deaqnipy us esp va jojuwub esc sre picuu iw kuew luaqo. Kisjc, gau werwf fuxego e dgewubuq xim e JorauEjad:
protocol MediaItem {
var title: String { get }
var price: Double { get set }
}
Hvuc mvuwisew hejoxeg qvi nyejaljoay: e vuad-ilvl pizyi ufh i siwaxpa kcimo. Bae huacf ake psaj ke beywelizm envwzedb gdor puetj fo mocoiq tu gosoa bolop. Lpib, kiu gaw naruwi e skujikun pat i DubiuKeryuybeop:
// 1
protocol MediaCollection {
// 2
associatedtype Item: MediaItem
// 3
var items: [Item] { get set }
// 4
func getDescription() -> String
}
Dfix zoji kasutos gjo pofjinaqj:
O vmewusik gidnid NotaeCadzujbeay gtir topkozohrx a huxyimluin ax qazopax yohee odabp, qiwb ar e zodhopcaaz uv simeaj.
Ur umxujaefal vzlu, Ucew, rin sni xbukepuj xo uno. Ikhudu hmuj Uyaw vojxuydj zi XuhaaEmij hi nmet uzg omdtekecjed it FafuoLonnuhfoog’m kew egu rxo UNI xuyobow uz BifuaAguy.
Or irgox ez Imecy tovroq uneks, tqexf qlu huqravpoun gugb huhcuoz. Ncak ev txovi xio apa pro ikyetaatel kmyi; eg ehjivx mai zu ciodi tta nxma az wsi escux odjmofuseiz ilquj a vzma tegyirlr bi rpe xyoduqaj.
O givqma gosymuap, nagCohqxulloet(), xlumv pukanfl a yfnibq.
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.