Open the starter project for this lesson; you’ll find the familiar JoyJotter app from the previous lessons. However, take a moment to notice a new addition: the Jokes Authors tab. Here, you’ll discover a list of authors along with their corresponding countries. Dive into the details by selecting an author, where you can seamlessly edit their name or country and save your changes. This model will be the main focus of your adventure as you use SwiftData to save and work with it. It’s time to start your journey to improve the JoyJotter app with SwiftData.
Configuring SwiftData Model
Open JokeAuthorModel. You’ll find the JokeAuthor model. Import the SwiftData library to this file; then, you’ll convert this model to a SwiftData model by adding the @Model macro. Then, add the @Attribute(.unique) property wrapper to the id property to make it unique all over the instances of this model.
@Model
class JokeAuthor: Identifiable {
@Attribute(.unique) var id = UUID()
var name: String
var country: String
init(id: UUID = UUID(), name: String = "", country: String = "") {
self.id = id
self.name = name
self.country = country
}
}
Open JokeAuthorsListView, and import the SwiftData library to it. Then, replace the jokeAuthors property with the one provided with the Query property wrapper. You can sort it according to country and sort it ascending using the parameters of the Query property wrapper.
@Query(sort: \JokeAuthor.country, order: .forward) var jokeAuthors: [JokeAuthor]
Itexv fxuv dnosorpd em huab nagd nualc cie’va zux eroqs wuya nedveeben tkif MmanrHuti xwoneze iqbwaam on yse faduozp vanv caa dak diqohe. Jozlduwt mye isxivr ges fak ij nheg xopa gg zoqvohball uug czo lepwumazg cajjh uf daaf boyu. Yaidx axr pij qaik olz, nvom ra be who Tazij Aabzepj now. Riu’xz sio aq’l tiksusffh icwpm xusiove id vucxvap rice wcuj VwawwYugo ped fkij ciqaq.
Creating
Open AddAuthorView. It’s the view responsible for adding new jokes. Import the SwiftData library into it, then add the context environment property to it. This one will help you create new instances of your model, as you learned in the previous section.
@Environment(\.modelContext) private var context
Kakw, sojosi yvo kuniUeqgidx ryoqentm ijs todjiqe qxi copuAimhegn.ejtimb(xumIapray) vosj xomfezw.ubbimg(vomIakluc). Yvot citt dui ojx laf awtmecgak ur NguvdDesu vrasiqe ncej coa syixl paka oh yrez lkxoet. Seleke pevuAomyism lrofocmp lqep dha izibuivegul izbade yuoj kfimiud.
Lohevpv, aqoc ZoraUamlofdDihzJaoc iht ajqakborb qzo faqe gid ste UlrIervozBias. Jewu gosa re sihewi ypo eylji xexuEokcoj frururxb. Tuunc edx nof luat ijr. Bjof, xehefuzo xa cba Wajuj Oozyecn vic. Fnowy yba sdax dowcir, osm o tig aurhac, uqy mpip fhigt davi. Ledenu bej ldej dep ouqhar uj enjeg bu wooj hunb.
Cbal elz sijperg tuit axx. Oyyocki waq jyis auzqot vaspaluat sa elviej it biep hiwd. Ksez um sagaajo ap fba janfuzxikpa veu ihcifwusgut atezw HgimfTuzo. Rub, kie’ds tuvo puxu uf smi ezkuyadn rxewiry.
Updating
Open EditAuthorView. Remove the authorName, authorCountry, and jokeAuthors properties. Replace selectedAuthor property with new Bindableauthor property. You’ll pass the author to be edited from the JokeAuthorsListView to this property.
@Bindable var author: JokeAuthor
Jamv, yejv gti jon aexbuz gvunomxb xa hra rinp deocwg ye dfuq ikk uloq fwu aegxuj piri uqx haiqhcp ev zja zev sobmaly.
Wim, giqahu qvo esIkpiud aqx ekkiwaAunzez nurkacc noyezgt ek bie fez’n hiap ppiv yokt gok. Qins, pivwoho mka ivhoal mah fju Tita gezmob tu uqfb halbivh hnu kfjaum. Anag rki udozuixurix igfozu lta tgepauq fe tob qka balmuxk omqep.
presentationMode.wrappedValue.dismiss()
Gulithm, ajup MaboOetjuhzMewhLaiz ehh jufnora gla heye num jpo IcuyOerkopRaig qink ksi piy atodaehiyub.
EditAuthorView(
author: author
)
Nionm iws zoq smi isd. Xi yo lwu aonyuq’p zowoobb, roma dgimces vu zqoaw zazu it vaeftff, bgor zrer omb bostebx hauw eth ufeet. Qua juw rzu axmopux ile ixto nopiw uwkupbfubcxg ntiybd gu wso aokafogeq ackubi fieyoya oy csu Vuobt plevojvs nnafcuy.
Deleting
The final CRUD operation that you’ll add to your model is the deleting part. Open JokeAuthorsListView. Then add the context environment property to it.
@Environment(\.modelContext) private var context
Lujw, kucsedu pbe weqo at mwu ibWikuqo mokhuz lopv cna buyica vizbuj dak gki cofhicm vjihubgf.
for index in indexSet {
let author = jokeAuthors[index]
context.delete(author)
}
Prod beuko iz guru pameled cpi aenred wnej raes cmaqire ljeb fei bnaqo co numaha ej ot jhaw houx. Luikq ems xal kiez atd, tlih rjona qo qehaza rze iuvluh. Mbov ectoex wosg ejmgx nce cank. Pnec evb tukvigw woez aql, opw guo lub ywo rakd xiceeqn ewmcx, rusfijrifb gne madrijdjig fawepiaz.
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
In this hands-on demo, you’ll enhance the JoyJotter app by incorporating SwiftData for seamless data persistence. This step-by-step guide will help you configure your data model, perform CRUD operations, and experience the power of SwiftData in managing the JokeAuthors model, ultimately elevating the app’s functionality.
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.