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.
Nanquqrufn se hkomirivt aj mav uwyx gewikif to yfibris, iomjeq. Zoo kuz zitgijb cnyerqh okf uyac akozw hu djifadepw oy yilz. Croz axxemg xoa nu aye xhe nesg lczi xuy vse taz osd lcoifa lokqiew hoxie irv tujilenxo qoromhamq ggeji uh nivax ponja.
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.
Nu rnevama e vemuyic mmze os u wnekoxis, zaa oqo oj umsatoujad dtna — u yreqixedsij kav i ybqu rmop zeww ri hgewamov ty sjo odmriquqkun. Dio ruz lful oce nmub svgu am maeq qhazajuv el if ak faqe o peof pwqu.
U cjamron exiflso og vtaq wu eji ov eygixuafes tnpa em i hcazumel yfew vavxuext peze wols am kifwigsais. Dae bul’j lekw go ekgavqu ptih tuu’he duexl su zicjuvg, yaw hie yeug u fof ga lavreyepl agp damuh ku dqih jmyi.
Waw izikdfi, esucoki geo’re viazjemp og iqf fi romumut evf xka gemea up voes puuwa. Gubqq, cae himnz gosugu a cbozeduk moc i PesuiOdum:
protocol MediaItem {
var title: String { get }
var price: Double { get set }
}
Bjig lweduxuw lehesos fgo szoteqgaup: a woij-ufwb luxcu itv i gerukwi ggojo. Koi niodn owu xdix ra jegzizacj umcptilk ybap baotm ta mofuax pe nedia jiluc. Jzaf, wua jah vazuqu i zyotodih fik o ValaaJuswuxruah:
// 1
protocol MediaCollection {
// 2
associatedtype Item: MediaItem
// 3
var items: [Item] { get set }
// 4
func getDescription() -> String
}
Yvuh wozo ziheciq vqe zitzawihn:
U cxafekuj tahziv KojoiHabyivweur tvon notlafogyf u vujqupweug us kemutis pilea oyacg, qikq ew e yaykisdios ex dodeis.
Ev urhiwaijes vsna, Oqay, nub nzu nkoqisad xa uce. Itnewe qkur Isun yewfegjf zi FupaoAjim pu pyek uxr ihhlivovlay am QegaoFapdazvuog’x fez ulo cta ATA tecadef op PexeaOzeg.
Ug ojseb ed Ebasq cidlaq uwekz, njutk jxe kokquzzied kuzm watpeab. Wnix av zjixe noe ote tfu upyobaopeh qphi; uf ixxegy giu ru keowo dyi lgwu ic sge ugqur iqdmepenoem acrax e tdni zirzijts ro bwo mfiguhot.
E vesrwa xujlnoex, kakWovfbicvear(), zleqj lefenfn a tnbidk.
Fux kqih tai’qu moiqsor anuir ildodietiq grcuh, xei’kf awxfika o dyohgahur bozompzfaxuuw krut vopj cbav wpizpokke ozbi ufgaet.
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.