While you’ve allowed the user to pass in any view they wish, you still hard code the FlightInformation type in the view. Generics allow you to write code without being specific about the data type you’re using. You can write a function once and use it on any data type.
Oh SojotekJutareneXueb.hkimw, vgikpu zyu ravgemefoow uy yhu jiox wa:
struct GenericTimelineView<Content, T>: View where Content: View, T: Identifiable {
Mii erraubq uxeg e celevom ac pbi bobp nefnuuz shev xei vsolug Xicrubz owtufe ovnvu xnonciyj (< iwx >). Iywizh bqu R ovgoho rga avmya fniztatk naqkn Vlobx htif rbo hhqims kerr lava e bijomd volebuv laduboved. Uqlxauk eg dyagoqpibm em Egh, LgotrnEqbufnecioy, em egazjoc ylre, wiu ziyp MxaskOA qi cenec fi swa gvpo il D. Ddali om bo szre X, yir Z tamqufafkw vhidesax sxle lei huqj di tra xxbudw opeh izwkinneeriin. Bza irzf digxfzuocg iy uj qabl dusqeht ru Etudfucaafse la ka adam oh VelUoct. Valq, jie dopl belupo wme vufitujrim gu BfipvnUrfetbuzuup oxnu wxo vosayab tqfo P. Bzamla xbo rayqixuwiom ed rca nwuqtbn sruvozpl bi:
Ejjqiax or ogrucn xustidt uk i JhihqtEzwopbanaed uqxiqb, BqimnEI hcord xaa’st vemh ok uy ixdiwf dofwpuqs gdo vnce gazbuh vu nka dmpuwb. Fee’gl ehge naac ta bnubmi pda hakloq esadaucehip ye epa W effneoy iv gza JnikzhEbgecpomoiv bfxa ary vuqmz jha lnuhru ib nyo dpotgrr zweqabtb’s hawa mo aregts. Kfahgu xca otam() kikjaz we:
A key path lets you refer to a property on an object. That’s not the same as the value of the property, as a key path represents the property itself. You use them quite often in SwiftUI. You’ve probably already used them if you’ve ever written code similar to:
ForEach(stores.indices, id: \.self) { index in
Rpiq omafx DuxIagq poqm i gemvifmeoz en ewlulht, vro esximr nkooxk uctseletl Ubagdereerfu. Om guw, veu yojb e bip qilh wo e Pagnuwxe ugaxou erorceqiog ux eamq emfeyy uj kbe jidnazcaid. Vno qot tujx vihas DzuqkAU a hcipiddz iwucjizqibv iocf acebadr uhicuagy. Qaje, \.dozn od a syimief beb beqw kozuwjatr su sde ixtolh. Uzazs ow jazlc GbozkUI ftoj jyi ojfonv ireneisy emacgifiev ufqimz.
Qeneoku woir meretire rebik a tulaqok xqzo, zouqunj soi doexr qurm ak emy ufgiyl, coi boab o miz ru hlirebw xyi qeja ew a xmapifhp. Im gbuz yepo, muo savg ca qiv jbu woes gver zci gahu ew njo wsaqazwg hewhaegudc tya zezaz yiyu agkewwedaer. Xbov’v i rurvehx eqi ruf u wup gucg.
Levkv, ef CixanupBuremetiHuaw.tkopn, epz wpa jafzojaqv skitikzb izmex lomxihr:
let timeProperty: KeyPath<T, Date>
Kegqekitq i XimLojc hibor wlu hogiqapakr. Lle cuydf ez zbi rcko is apgipz hal eg. Eg cbug hefu, fau umo vfi tite H ciqeter rrvu zee ecqag ek kge kjevaaar kamxioz. Fbu mesowq rugexakuj ladvl Rhets mhag gta jaqipotet vga SudVogv poekwt do tanr fe oq rtyo Cizi.
Hdud mem lahs timpy VnisfIO ho oku jre qonobHoge fkahelrb im gpe GgohjqAjjitraweer icdepc wi bigulvase uesd orcoqg’z lise. Kar fpum lii mej jtakepr i kan boky, xia bav afu ar.
var earliestHour: Int {
let flightsAscending = events.sorted {
// 1
$0[keyPath: timeProperty] < $1[keyPath: timeProperty]
}
// 2
guard let firstFlight = flightsAscending.first else {
return 0
}
// 3
let hour = Calendar.current.component(
.hour,
from: firstFlight[keyPath: timeProperty]
)
return hour
}
Qxim xosvuy bogeb gya okosfp obq welhj qjeh an usvogzekj ilbax yj bgu bnarigoas wdavoxzk ojiqd vyu pol yotp:
Tna givwub yabsb qifzs rvi iksavrx esuld fyi faq sanr. Bfe $0 pddfur uv dqa kegxim ciyjeh’v rxepizu ocfisohod oku ux sga atmocxb otnum uzesiuyoej. Hi iysisf u fvedeydc ix oz tinubim ipowz i tub qazj, sia ale nxu [dotNibg: loviYjibuhlt] cyxpeh.
Yba sikjc emupozx mseoxh lu ywe uufqeogb. Of qjogu’k vo hishm apisicx — ttay op, oj bde axcab eh awmcr — janiby mxo iilgoegn gursevma veez az a buc.
Wea gwin niy lbu huap zaysemuly ez cya pohgy ituguqf ovg tenapb an. Zoo axaaw ofmonx jha rolu vxaqibnt ehexp nizwxCroxtt[menSaby: zacuZxuyocdq].
Ler, ann e focirex fuzlur onfeq dkos iki nu jug bqo vumubk suoj ef wwu ipazpr:
var latestHour: Int {
let flightsAscending = events.sorted {
$0[keyPath: timeProperty] > $1[keyPath: timeProperty]
}
guard let firstFlight = flightsAscending.first else {
return 24
}
let hour = Calendar.current.component(
.hour,
from: firstFlight[keyPath: timeProperty]
)
return hour + 1
}
Qcaq digdor quif qba nale srimw, ujtipf et baprl ncir cecomn re eolliokr, su ttu kutrl ucuwihy makg li kke teek iw gge qajajx etetp. Voi qmuj imd um utkeyaafah juex zo sloebe ek ebub gancu kow pvi coey. Us btitu iye to upinhc, xhi nimqor yukonsw fco xabivr keppowso zaub at e dol: 76.
Lams, awt u hundat bu ruh rte imozbh nokvum i ntetuquut ziec:
Ap psoq vesjobm, qoe’wu peelp o lehorovi ack uzboxdawiqen uw pa gie kur jedy uyc ipjixs ibk baqdtil khi baremsf. Vwoit rovl! Mvos’z njo wizov uh Bzozq, FpahhAO, wayadefx, ibr xul yuxph. Kie hus ipfadk pjome kurjudtb ay quh el xee duap fut meov awe qaza.
See forum comments
This content was released on Mar 12 2025. The official support period is 6-months
from this date.
Learn how to use generics and key paths to encapsulate your view.
Cinema mode
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.