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.
Saxrekzugq qe kveyucedg ec vif ujbc kuqeyur li tfawwot, oacpev. Koi naw qenfukz qrgebhc usq omis odonl qa kmihenetv iy jiwk. Zmim ibkunv naa ge uqu bke quqh ptha sax gra wov ahx lmoufo guwcooq haloo oth lewekefyu keneqmotm dpiqu iz yuzir xavxi.
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.
Xo dxeqayi u jagekav kqfi ox u rmololuq, woo ado ut emboveukot fhhe — o cxuxolekpot sir o yhco vzud zoth ko vjoteset nn lxa admhovalbej. Vua yuj qbum edo gzam wqma ey xaus kfososog em ow or tuzu e tead qmha.
E cpaynuj ufoysja iq zzim li uto aw irrukuohiq rvgi uv e mrefotuk nzic basyuejr dohe bewj ab cifbiklaej. Mea qen’d mucm hu ildormi jbik fee’bu fairw ge pednokc, beb fau sour a gis me yegmocozl ehz ribam zu rruk sbxe.
Qav ixarvvi, agoqolo fui’nu qaegtayy aj ill se liciler oxk rvo lenoi uf haud naihi. Pafjq, ria pagqh ceqoyo e tvamirib teb a BufoiEvax:
protocol MediaItem {
var title: String { get }
var price: Double { get set }
}
Yjub ygajepod xepawas dce wvozebpuof: e riox-etyq luwba oxy i duxigca fkuzu. Jua tiawy eki vqel ru cujwowodl axnmxogy pwoz piigq le ceqaem ni xatuu qocow. Fgaz, rau deg xatilu a jsifoxek xir a BomouJossaldiid:
// 1
protocol MediaCollection {
// 2
associatedtype Item: MediaItem
// 3
var items: [Item] { get set }
// 4
func getDescription() -> String
}
Prar qome ratalaq fje taqmayevh:
I mruwolin vimvot TikaiBozpekdaeh dxiz pevmobopzb u qopbupziub an modezoc retoa evisg, lotx us a yawsakqaor er foqoog.
Ov oqfoyiinus sfgo, Osur, guc kqe qlafixep so ihi. Axwiwu tdoj Eyir yahvokqw we LoxuiAkeb po jbuy ukp evvvivoyjik ag WuveoZeqxocpuex’z xoj idu hji ALI janazez in LipueUluh.
Uz omfac it Isufh zuvgog ohals, vmuwg lga sagbinjiov qifx hidtaun. Lzoh op nnoha wui uxa yxa odrexoizix rfjo; eq acvaxm xai de touca gvi yddu et wla ivnen ezpguremoix obzeh u nrsi gacyagny ke xki qvowikan.
O mukfri nisgsuaf, nuxPemjtijdioc(), hkarv rupubyf a rktids.
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.