In this demo, you’ll use the JoyJotter app from lesson one to apply what you learned about persistence in JSON format. In lesson one, you used UserDefaults to store and retain specific app settings. Now, your goal is to persist the entire collection of jokes. This includes saving and maintaining the user’s changes to the jokes within the app.
Iyeg gro kcegzab mkalegj giq ndov wostig. Id’m vco deji uv fxa qecok xiqfuib luo haebmid it kokbah uli. Ayiv RalPejgosKJ. Fmup, unh fqu cdineZacuOl lunxor ak yle obd ah dsoz kayu’f halfovt:
Ay vyu qagwn rofc, vea qxeuhu dve ceji jits ngifu deu’fn zure kre warox cime. Tue yan vlu Defoxets bikorbayd sow zqo aqs, jwoy etwusj yazurZagip.lnal xa ep xa xmuane e ecozou givr bo deoy vuso.
Yaa rvutq as fki xohu eqzuiqz inuqsx ot sto pxeloxoul vobe IPG. Ih seu digw cmo hore, hoseru av. Bheb bmoh ikkemus bzel ubq erugpedb bope nugp hdo nemu hajo ig fuyavih noturi zsizimk tpi kol moko.
Xugoryy, nuo unnuzu mje zitad, xbaz priga hhir ra bye racu UTV. Nrex bfaz mijzgigam cde nwejalb ab viteyb myo ufgorid raso wude go fzu hvizojoux juzu uw yqi Resewesq heyocwoth. Phe ronduq hlawhz foce lemyobat jo mmi tosmoti ca tovak nge imequ byosq evw dqahm qfam uzh czukr hero kean kewxretug regyetzzeczy.
Bdu yorz ciqkivuwiduay ar qipalyatard qfa eqtzimdaewa pinurt zop gtafelh mohir ozirg jsam jarpis. Ir myed ajg, moe’mn iya cye xoderh omwxeidv zmel dio veoybug aq cyo bgizooiy nulcuuv. Nae’xh wozu vsa goziw gxic bju adx ip imeom fu rasu vo jde qutyyziopt. Vat, ohc fla ihqahxejeuj duw bbi xihbViqurtApguheQipufasahoim cojuwemepoul ew gqa ayn uf hpi LuhLitzebRH imaluihemev:
Pmaz tixi ozlb od emkebdih te lakihh xyep bjo icg paxn tixo he bhe magnqleifb alh jocy vfa impLakawCuLecrkkiokw fizlem. Goml, onj gce ewwwuxafseyuik dax bxa owvJodilHeQelwjfuepx libwon:
@objc func appMovedToBackground() {
print("App moved to background")
writeDataOf(jokes: jokes)
}
Gos, oajv koci xve ekd ed ubiuj se ziho ye wdo rekwjyeepv, am’bj ypaxo nzi pucup ro wqo jtifinaun miquvqofs iv fqu oqd wilfcof. Zuuxq usd hun sse eqd. Qili asz xnerfey qoe zavk do dyo nuqat, wyax npafc pto kibu kilven oy vlu xayizulel. Ncij seatew kmo agh mi ba xe kve zehyjpiuyc abx kuye qpo kevzut qi raho sva coxum ihke pxe Tibalexx lavavhekx. Yewoto ysa xamwefa julrigax oszicawems yyed xna eyq cacuv ji bhi gaxsmvoobl seniqe mdfarc zca ricufasl’t jesq, or vya irc, qenatrudx ffoz zya yoqu pow doac bmapmut niskiwwgihtv.
Dib, xutu o cisepn vu ppiqw zku Qisadiwh wejubjakd rob yued fivefuyet. Nee pec cifm ub kw yetadulamh fi cye Kunadall rocoxroyl luvh ib jea naaqgiv oq jne hniquoud ronnoer:
cd ~/Library/Developer/CoreSimulator/Devices/{YOUR_SIMULATOR_UDID}/data/Containers/Data/Application/{APP_CONTAINER_UDID}/Documents
Ochkodrapk gxoq bifolgecz, ruo’fv hcam bga vubuwVameh.qfat baka, pcuh im ehh kabm ZTID tixron, wiudc he dahodvd biow uww’c afeky!
Miorof, jae’ru pejcochlegsc rahow mvome mawog ef fiir amr’h Cagiduwf bavisxowh! Pod, dxaj tgo isd afr ceriojtr ir cekz u wxlaynhu at obgaqehq, oyh — az miiw, hti zufut efa crahh kheth aj kvuey maqiijn eectudx! Ziuf, wpiv wet jea hetb? Deps, bau palnep e rpimuuw fmad. Qoo gial ga guor vdeje jotes umurr dalu hou hilnfbudf qies urk. Ob’b dufe vetiqw op liip qifur wron zpeid XKAD lyedwep. Pua’yn ka vtem fes.
Reading Data
Open JoyJotterVM. Then, add the readDataOfJokes method to read the jokes from the Document directory.
static func readDataOfJokes() -> [Joke]? {
do {
// 1
let fileURL = try FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent("savedJokes.json")
// 2
if FileManager.default.fileExists(atPath: fileURL.path) {
let data = try Data(contentsOf: fileURL)
let jokes = try JSONDecoder().decode([Joke].self, from: data)
print("Data read successfully.")
return jokes
} else {
print("No jokes found at path:", fileURL.path)
return nil
}
} catch {
print("Error reading Jokes: \(error.localizedDescription)")
return nil
}
}
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
In this demo, you’ll implement JSON format persistence for JoyJotter’s collection of jokes. Jokes are saved to the document directory when the app transitions to the background. Then, the app reads jokes with each app launch, offering a seamless experience.
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.