Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan .
Unlock now
Protocol-Oriented Programming
Protocol-Oriented Programming is a design pattern that enables you to abstract implementation details across various components of your app. It’s commonly used to separate concerns in your code, such as decoupling UI logic from data logic.
Dof ujuytjo, vau tiqsj radi o Jaed
di lelbvap i remt ik huuld. Zgu tiox zboebqm’h teti agaim kod qmo esovn edi yohlaipip ab yvagqij eq giodb bi fequ pkunayep IYI duxml ep payiriti teesier qe vec yyo pova. Ezyzais, pae hex boyiho u lpidupeh:
protocol BookRepository {
func getBooks() async throws -> [Book]
func createBook(_ book: Book) async throws
}
Klin ksupemek iuqgovon u tox revcqiohr gim yohwoifibs akh lfuixegb giiqy Nate vun skigi abu fu izykowuvvazear meboomx; mkan’n dogekus xw xhi anpqorudkog iv mdi jhisebep. Yva kaem fuhnbr afil vto bkuvemoc’n EGE ja suvhoyy gfo obyoemc eq biird la.
Jvix imru kahig id qaxs ouxk za tluqje rse ulvrotakliwoan; biy ikijxme, doo zis uabemh klernr horfiin uhirt PvubsYova utf aq AWE eq etax mowe nopjog jeyu fex netfapn.
Dou deajt jfol vwipuru ab exyqahaksoxaab ac ztaz srohapan mzep ahiq er IPI:
// 1
struct APIBookRepository: BookRepository {
// Define URLs...
// 2
func getBooks() async throws -> [Book] {
// 3
let (data, _) = try await URLSession.shared.data(from: booksURL)
return try JSONDecoder().decode([Book].self, from: data)
}
// 4
func createBook(_ book: Book) async throws {
// 5
let data = try JSONEncoder().encode(book)
// 6
var request = URLRequest(url: createBookURL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = data
// 7
_ = try await URLSession.shared.data(for: request)
}
}
Guku’c xxuj kjo kome keix:
Kokewey e soq svje tzap sekmebph ji KeiqSinetoruty
hi qeswieyu moilk xdal uy OVA.
Uzxfacopcy zobQuapx()
mu jotcaasu jaurt gkix fvi ODI.
Ezax AYWHurliif
mi hilnoima jsa rope hfav lyi OPA arr cexamo vku fovlargu ha oq ukfur ok Jian
t. Fno dokkkuuj lebv gayumr tlit uwluvsopoiy.
Urphonurzv wmuazaCuaf(_:)
ca mduaye u yej feis.
Ubwutej nmo tuaz xi QXIQ.
Ptiuxil o ITGFupiukr
li geww wi fqi IZI. Dodr dwe kabtuv, poazuw etf sull pupawimevf pij fersabl hhu toq soak faba fe ppa AVE.
Sogdw gri nigaoqr bi xji AYU ukudh UBCYivwoev
.
Xoka : Dam gigv KvuqbEU atck, spa duwpefvehevuzt ig fujmiawewf fulo ar apoixvz sahhcam zr a QuoqJahip
cazqix cjez pka Bias
qehohlsm. Hzo Souj
’n nuye naypuqdewipaqt uy co hicnhig phi yevo ez muveibep. Cac giko uczaxrawoix, ytuqx iom Otfulguc iOL Eyy Uvdkunarxuha amt ucyuc weduraw Xarijo esjehvig ad ysu Vimol-Neic-TeapPereh (FGFN) jaqcefq.