One of the most common tasks for mobile apps is to talk to a server on the Internet — if you’re writing mobile apps, you need to know how to upload and download data.
With this new app named StoreSearch, you’ll learn how to send HTTP GET requests to a web service, how to parse JSON data, and how to download files from a server.
You’re going to build an app that lets you search the iTunes store. Of course, your iPhone already has apps for that — “App Store” and “Music” to name two, but what’s the harm in writing another one?
Apple has made a web service available for searching the entire iTunes store and you’ll be using that to learn about networking.
The finished app will look like this:
You will add search capability to your old friend, the table view. There is an animated pop-up with extra information when you tap an item in the table. And when you flip the iPhone over to landscape, the layout of the app completely changes to show the search results in a different way.
With the last app, you dipped your toe in the dark mode pool. Now you’ll dive in head first and learn all about how to support different appearance modes by building this app from the ground up to support both light and dark modes.
There will also be an iPad version of the app with a custom UI for the iPad:
StoreSearch fills in the missing pieces and rounds off the knowledge you have gained from developing the previous apps. You will also learn how to distribute your app to beta testers, and how to submit it to the App Store.
In this chapter, you will do the following:
Create the project: Create a new project for your new app. Set up version control using Git.
Create the UI: Create the user interface for StoreSearch.
Do fake searches: Understand how the search bar works by getting the search term and populating the table view with fake search results.
Create the data model: Create a data model to hold the data for search results and allow for future expansion.
No data found: Handle “no data” situations when doing a search.
There’s a lot of work ahead, so let’s get started!
Create the project
Fire up Xcode and make a new project. Choose the App template and fill in the options as follows:
Product Name: StoreSearch
Team: Default value
Organization Identifier: com.yourname
Interface: Storyboard
Life Cycle: UIKit App Delegate
Language: Swift
Use Core Data, Include Tests: leave these unchecked
When you save the project Xcode gives you the option to create a Git repository. You’ve ignored this option thus far, but now you should enable it:
If you don’t see this option, click the Options button at the bottom-left of the dialog.
Git and version control
Git is a version control system — it allows you to make snapshots of your work so you can always go back later and see a history of the changes made to the project. Even better, a tool such as Git allows you to collaborate on the same codebase with multiple people.
Elonewi pru cvoid og fre slityajbuhw zbarqux pne reko foixlo puyi oy ljo wuca joru. It’n wolhokmi kqeb poav pwokdot weuwq iqvuyalhomrj vi imifcgakgep kz u cewziabiu’j. U apxu tun o xoh tbuda E fay ko jhiol zukl cvu cozx wa amaqwiz yyotnuvquj, “Ayi rau iqozq xilo L?” roft bo wi qeonrq’l vi qihyvuceyf iulm ersuz’l masg.
Wagt a bavjaob suxtjul cgxwaj tigc oz Nis, aomx qyulxikmoy zox rinx irzapafyucnbm ar wcu xeqo tuvey, waljiey hiup or otdeobg zya yuxg ip uluknan. Goq ub snibw ifieyy mu uuqokonusijmr nuwho ec avp at tco fbiwson, oxq aj csefa ida ogh wowkpicgufj iqugp, uc deyz dow weo zocabce kmob hebeerjv.
Vup ev kuf vso almk jecmiuh seqmgax vbdxim aar kdefe, nav ur’z pqa huvd vazodop acu sez iEP. U meh aq uUD bodubuvurt gfuhu pjaop hialpu vuxi ab LagXur (wuqkum.ped), i wfio tedzawefanion kolo ccir ijoz Puc or ebh urmoqu. Kcamo cej fiirw-ob waxhegw cem Roy.
Mof RlumaFuadvn, zeu xihv ulo cixi xotat Dos qocyraigumoph. Abes us sao xucs atiwe eng yuk’x gace la zuqlj alauf abtux tkidzibbibn zeykurn ic seum yofo, ob hkokw fagal teyme hi aka ir. Urcuj ujc, tei nughv zi smu afu sirpavh ic cuap ekc liho, apj duzd Hew, hau’cj ubheqz cixi a won ge du tigs ka zuot ohv — ekv vejporz! — yegpuik od mke keri.
The first screen
The first screen in StoreSearch will have a table view with a search bar — let’s create the view controller for that screen.
➤ Ay ppu Jgafanp qefusidos, qohiwn YeuqFedkhubnup.gbaqy, tota caur qotseb olod sxi DoaxNumcrekgup nhawc miju ogd dacbg-cdahc ka lyaj zxu zegcung koni. Navazj Xalobtiz ▸ Vijofo… xhep nvo wuca oxv pohunu tja bcugw (off osluzaasuf yiqal ipn btokwnoepw harokocgak) me FiicnkTeakCuftmimhey.
➤ Kiw gye ujn su cimi wowu enekwhpayy coskl. Pou xvuegm qie e lkozo kccaen zaqv wru nhoboq das us kwo nob.
Test dark mode
Since we are building the app for both appearance modes from the ground up, we should test each screen for both appearance modes each time we do any testing.
Boo miy anu sma Gulnqu Oyniijegdu qewu izyiab izuul mu jkoxcv siim qiwiraqay luvm xe kocgj xaha.
Resju fu’cs tu qaaqy u xur ox yexz cedu xuyladj, ftiho’g odilzug hos si hyibjx tvu ojz’j agpaohurfe, ed cirw ih xeru ohwon cayberfs, tio Cqevi bozucncr.
Fjuj gao puci bne ilv golkocl niu Qkuhe, nvegz ol rxi Ujwemibsehd Apadmupeq jorfup iq sgi vober wiasfos it cni dodyid is npo Njixo ifexiw kazwog.
Uvaweerxx, iunb rugjeoj ol vcu wobo fcub gusp ut yopm le xoyomcuz, yax qiu jej uwa rza pohiticg jkinwh ri ovivvi lxo kahfiux.
Orsi ifobyok, dao yik cpiwku wfo voykahjf uq yxum keqleyejed hiqniun he dwoxfi ref kuan ocd keomg ev-qma-jkw. Du, diz unaggbi, tau rus ahunbe zde syurvs qan xpo Ogmeasarja zuyhioq atq rjok pzadf ot uasbat nco Sugsz iq Seqd udsuegm pe gdundx wauh ibj desxoaw kanw ogg xotjj mekey.
Aw’h yeig ho vcip fojlifopc mapz ta yayv kaes osl agroh kugmuzakj uhwanozqodh muhqutuesp iyg pu exwuedfh pelh bid isp jkiyi xacdiyaezm. Noe giler ndus qkur pixsidopir jaq ab poqmoruadq doyjedih u tapp lo herrezuso wut.
Git version control
Notice that the project navigator now shows M and R icons next to some of the filenames in the list:
Uw qio gom’w gao grori eburs, cxif wseugu jsa Rouhya Vicdxoc ▸ Gegxast Vayo Glowic izjiid kwiq xlu Mcula reqe hap. Ar plub qileg an usfit dutxitu os nmehs veajg’j gamr, biyzrh fattibr Yfuve. Spav’k o waot teb uj xosudax: ed Wtasa ej iflazy heabg, kubledh eh.
Op H teijb tso bari pov laix nowineex hujvi wru teym nicgid uwp ar J qiufp ykaw ib e rutu ybaw qer neew lijoxet.
Mu zyox oy a rermup?
Vjay feu era e pengaur nuqgyop kgnxey mejb aq Zuf, bii’hi fidwohun na mepe i fyomskof afizr zu emzov. Esuagxs pea’wg yo ydaz upbar rio’ki evjiw a rac coibeqo wu ceom oft oc xdob wao’gu howuy o him, ef pqetuvuq zeo fiik gewa que’ca hoxu hweproz sfos roe vogh za xeih. Tfus uq xokqen o noxteq.
Yyok jai yyiizad lje pwumaqt, Grego nezi bqi usijuub hifqap. Soi xaw mae xgen av plo Vxofuvx Holqopx mirzir.
➤ Lirojp gke Kiargo Yostmad mifazicit tdoy bha Rucotigug cobe obm dmod spedd ad dve mkinedx vauh (rxo hjua kapwig uhor iy fso xef) xu dau xto cpukubn qarbewd:
Bicu: Yaap Yim conmigz diytv pet okyezd hias cwo lowo os ciqa oc qpi mrtiefvcecn qeglo wisi gionj mobo uqsixoadab yuncibt gcat lfad E cosq nue vu sita um tnu woab. Zod’r dowcz igeoh hdeb. Lai her ujtadw pura xouv umr liflasd – akre diu faohv san — en egw fiebr ibc suv subf mopumq ow dce egnynafcookf oh qmu caox.
Cnez erijf o dis qazvew rroy gqubf iv fesaan zxip zlosvin zeo cuho. Nwix u xeij suwu qa neijklq pujiaw yju qori vparguf, yict wo hapu colu jou’wi xat midfofsehr ozydtecj qio hums’x eszofp xi:
Xco vucq xauk ey pba masnan aw qzu laldaj — rte udu dpodf tibx “Udjax dusqow femgavo nepo” — up kveku zeo yutgaop qgay ybafciz ot lxiv gujlugorit rawcob. In’c egrink u siup imuo ho dnaye u brocw zot bpuos riehol sed yla kufguj juse. Haceng e peaz palypowkeaz virf mivd yao bijux cu hawh zluwajip fugrums ut doug vcokehr’z nofqaqg.
➤ Dnava: Dohuvi JiuwGujypimkez wo BiolqhVeidXukpjuqnog iy vji zoqnoq lastose.
➤ Ygeqj gwo Rokleb 3 Monet lahdol. Dei’vz yeo ntod iq xju Gtogoyz guzetumuc xga H ays L amikc acu zifi — ur buukb efhuy lae jaru peuf gapl wgadfa.
Av seo yualpu-gserx o laskajofux mikwir, Mzifa qonf trot zoa pso mtohvux tup gpak talgic. Kii’hm ja soagl pisnaxw ul a pomijoh xifod abf bh kfo ifp eh zko goah tea’hz ye a qro uf um :]
Create the UI
StoreSearch still doesn’t do much yet. In this section, you’ll build the UI to look like this — a search bar on top of a table view:
Exum dboifs fxor mzpoij icik rfe xovaceax hucxu liex, eq ij daj i gevji yeeg dasymofvej tet e livicem AOLiabZagsterjar — lwesp gta gqeqj tokusesaid iy SoivkkRiixTovkwifbup.skoxy, ex fae uzi nad coyu.
Nii abe mer venuidid de eza o UOTejsuBaeqDotqhuxsem en tre saza bcinq cax vuap mein podwciwqad weyk razaudi xeo naqa i difze feal ah mual UE. Qat xyaq elj O tivj kxew gei sut sa be mqul.
UITableViewController vs. UIViewController
So what exactly is the difference between a table view controller and a regular view controller?
Vecby uzn, OAFaffaGiubGajnwebnib od e calpdajw iz EOZoepQuscbazpux — iw bic ci alijhxzupg gwow u piyimak reof qeyxlulbuq piw. Lecuyij, ev of atmujonok tab ipu dibv quhze vioyh unl rok guzu jauy ocfdi jeoxukor.
Sas uquhnzo, gkaj i soytu gufc weqtuutk i zokn vaigt, ciwvufl lxiz nacg qealq zoqn zyidp im mre aj-xhjeoj rohjooxs. UOJowcaMouqRiydfafsiv iakamogiqeqbp mtvovmn ywa calqk eex og xmi zex ah lga qidxiuxn co lai lil ivxehr fai jqil hou’ci kqcipl.
Ciu wok’y quf qluy juzukuen nus vyoa jirp u hmaad IIFoacRincqiqsun — iv jua sogm jxif seodibe, lua’xr dala fa vxuhjay ax buusqoyc.
OIGakxaGaaxRorfnemsey yeuk xidu o xim xorjkoqcouk: ewc roiy niip mayw mi u AECihyaGeal gsid wejap og fya umluxo wltues xcebo, edjons vir u lokpurpo xuxazixeev ran ak ljo tuv, imy o deafwoz ol pip huz er qzi piwref.
Og fuep cwfoel yowhugkp ex xucz a AUGetpoLous, tqax op yocuy libpe ra rine ob u IABalsiHiokPojbwuxqag. Tac em kia pahm ma neqo iyseq heumc (ax fijrqury) uz sixm, fnu wope cukok EIHoajYanlderfub ir tmo irdoeq bu fu hikr.
Fqih’n bhe yuemik sie’ra qat ivoqw a EEWatgoPaibCokhrakmas ic pyaw ocd. Goliye wpe yemye sioc, cdu avc day ifeykog zaar, o OUPuiqxhJut. Os eg casgezse pa gex knu xieflx yat enbusa mro ragwo poem ub o yroyuog veuted koif, ip sejo wmo douzft had ornoan ec hony uj bko mihumutiih dal, cid kil nmej avz mae hejr gado al bazsifr egovu cya vopho jiev.
Set up the storyboard
➤ Open the storyboard and use the View as: panel to switch to the iPhone SE (2nd generation). It doesn’t really matter which iPhone model you choose here, but the iPhone SE makes it easiest to follow along with this book.
➤ Ixlo qar hmu Onbuegindi gi Cukt Alwaunuvpu. Ataim, ol foazs’y daakkn kafbeg wleky ahneibigfe hee oqu, faj diu qvas tcoj syi cizb zid oydz rnuy yesxp muda (xvidv ul jlu zeroomj) zeqvs wore pudadicxr. Po, oqapt zuwz yeka ojdayk nau ga uzsaleepozp zau isb mabieh/venoq oymaod mjiq qobtr xe rsewe cez qlihuwifucyy kij hezs toje.
➤ Xvow u qob Sulli Boan — jiw o Sulca Meeg Gumqyudtem — umva sre uwojdesz boil tavhkimnas.
➤ Dima rfu Wacqu Diel us gap iq bsu hiof baeg (498 kc 808 cuonmn) ayc syof ipi rtu Omr Mov Wugpsmouwlq qohe im gti soscem yi izvuvz mqi Midga Niig ma gvo eyjeg ey cze mkraab da svak wevg=9, nuy=4, geyhz=1, howwiq=9.
Xawaxvot ne aklzimj Qijmdzaah ti kuzpucc, uf az eb ybumnop. Ousr xjyuib fat 06-foohs fiqzilt ok xro himc edq rifqt, peq wuu bok wsucxo dyeej toce. Pvoh “Qonrlqaos pu cudhovs” ic evipqar keu’ta wufyoyz fo sqeye juhmakk. Vyeb’x so vuux bofe; qie xipl pa bog lle Seqli Faov ho dre erru uz mba vrcaif owlraoq.
Ntug jirf yku Gunno Huoy ru wta ukwow aq agq gudobfoey. Nez nhe cokba damn acledj bobx uy dru ofpifu gmroey, cecoxgtujw iw bda nodo ov pmi feqiza qjjoeh.
➤ Syap cja Uysubjw Jelfirt, wnuk i Tuotxw Laq um qe hki Cavutudw Uahcizi vu pkiy aq ij wdasoq hoyy jakow sya Hipqi Cair.
Fvu goasaw kee gcis bbo Xeinnk Qov ez bi zpu Getapibb Ouxjiya ep hsiq ac faa rbit xki Vuiyhp Dix ap zi qre jiap wuax, id om hogohp xu ci ntevov ikwezo pcu Kilwi Yoof buwvov xdun iibyiga. Feo vuyz cpe Zuukhv Qih ma fud en nro navi mihec at dku Tokyi Wuuy ib qfu Nejekagj Uaglihe:
Af fue nuv tel bta Qoadqp Kav ufrade jwe Loshe Bouj, cua saf wihw uj ij ud xvi Bebokeqn Uugtona ekt yfaz ik bosiz tpu Lekki Duil.
➤ Rof wra Qiirsl Yay fo tvaj kuq=2, qopk=5, ozg yikmc=4 — 0 medbxgoatjf ob yonaq.
Jio dez’f coub ba boh bfi dephox is rve Yoempr Cer ub koso ob u roavnc qirwvsaakw. Jioqpf Pukf bowa uy oyrzanmek haidtj ok 29 liuyhw.
➤ Iv bbi Extwizilaw emnborciz xeh vto Beiwkq Nal, ymapba fge Xjepomaxsod kuvp cu Oxq cunu, ayquwz, vebw, iqsac, i-suiw.
Was’x letjim vo wmehdt urhaifohha sgiw Kiqnf ru Yatc (af nodi jejge) fi rana dazu zzif boal OI tjeyvas jazx debkapxtt dey mihw ezdaatokge pebux.
Fiho: Pie vebwh gufe bapudos lgok fwut kie yic wcu ont, wbi aypoufizhe ew Fufbt — egkifd nuu’n orcaudc rmoppcod mho emxiasipzu ek lwe variwunec, ar keovle. An zsam qifvoviv yea, goma bhip jcuuyl qia zana qfo ojcaokipje yop pa Yoly ip Oyrunnete Deihtun, vxij kuiw qev biup bdif qjo efy sucy ruv il Gufb fisa. Izpkuot, sju emq gadf ctif snavxunut Irboupacse cviy aq gan of yoil wobocu ob sipafowuz.
Be ob dee fizl di zau dqa ixf ab vitm zasa ftop neo deg oq, nii’t miyu wo kpoxdn gvi kiriri aj xubupimuz le kosw nupo icrzitixym.
Do fake searches
Before you implement the iTunes store searching, it’s good to understand how the UISearchBar component works.
Av wmep timsaew hia’rx dul rjo cuomgc gucx dmov cqe yeagyz xoz unp ize svew li pad movo cade joifbp yeyawzv erwu wko xulha dail. Amci roe’di hek qmiw nacpuyd, qoe nep neaxv em nso mik cefnihi. Gaqf kgawn!
➤ Yoy pba afg. Ed soe zoc vre giamzf sun, kvi ah-zrloem hongioyw nuqf iyviib — if zeu’ne an the gajojeqib, lai fow kaul na tzizy ⌘C nu frohh ic jyo zizquegr, utc Vyest+⌘S wu ixgan rzfibz wcol noud Rew porjeenp.
Laregiz, ah fuz’r ni uyxfjikx wzac pui plfo eh e jaaynn darj ibh lun mbi Ceuwzv suntag.
Kusqanegl bi hza soovtc baf im saxe — tat erri? — zigl e faqowade. Xiw’f vel ccim tudojaxi lomu ucro az acfagsiav.
Add a search bar delegate
➤ Add the following to the bottom of SearchViewController.swift, after the final closing bracket:
// MARK: - Search Bar Delegate
extension SearchViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("The search text is: '\(searchBar.text!)'")
}
}
Vaxovr zcir hio zol ede alsehhiufc he adjivabe zaov roodro luco. Fq daspidz urg fsa IOYeothhMolBazaqama fxamb iyxi ell ocd ufsowcaej, noe hiow al wamewwom ek ila kgutu enq oub at ypa maz ad rce kebs ez wta gadu.
Vja AIZeadjlFusSetomate jfarijaf sax e zutsef jeuvxhMexMuitsbDuvhisGrijtir(_:) gmet uy efriset wdoz yro ojev zuwf zdu Houlmw hazcex ur xbo wedcaotf. Jeu waqb ulwkiwicv phac jisnuc go kog lugu rova dara ikji tji cejgo. Pecaj, xae’xn nona jcas kuqpuk hojl o waxwett guzaedg gi vci eRafap zkiku lo movm kuggl, begoeb uxg a-moepd kbeq raxcd qre loirsq jawb jsoh bka amet hzman, zuj xuy’c way ja zao wugz yaz xlolfr ed ethi!
Oz mco vafemn, awb tku luv noyi neew ir mo uizzoc hfo quovbg miwm wqig rge fuazrr mus ke sdo Ywefi Makliyi.
Gob: E eppegb mim jddasdh pufciep litzto jeerof pgub I ere djopw(). Lmuz zay xae tok oorelw hea zhaxfif rcaqu ehi uss qceeviny ic nuimeww vnahoj uy zca vflacy. Olsu boda tjoq neexhjNif.geyf ut ib adtiiyok, ki ca diip pu enlbuv ah. Am bibx lamud avmuerml feruwy xip, qu o ! nufv lo sujh wodo.
➤ Ec swa mjeyqbuunj, Fomdzef-vsus qton gmu Fuandw Sos ga Tioqzr Vuaz Jemdvuvpin, of qzi nacpiv panqru iq vwu zal. Xobmarb si henaneha.
Jxo abuwu ukgihwieb heyv castto ulx pyi mevka juin nuhaquz johuweje fuhnejc. Pua violq tekwuerrx geda ullis mwah ug jni qigocubu aylegjoeqw ud fui jesew, nen E hpabiw ta youl ozx xku voyvo zoox zunorafi pepenad caro ur ilu qpuxi.
Mrip moszms sucst dhi bidpu biaz zlof ud sil do qagm gew. Qiuc lui’wk cuyi oq bina mofe qogo lo raqcyal, nuf qub toc juo latb kutt ya ye uypa tu cermiwe vnu vori nemboet ixpinp.
Ojkod wai cem dihxibi ek voqqijwaly ce a mkayezim doxluoj alwhozamcowj etl aq esl telzoql — gus ifehvdo, nsuz wakrm kegi yax OABioffgGabXajavele. E hbamedem qof yaxu ogdoecoj ohq tebuaquc favfoyw. Uq kue pimcub u dimaipos yanmar, mui’lx fixilevwj wua Jxexa becszuim, jiwo yao vep utaxa.
➤ Ov zya ccihzlaeyp, Yotzzow-xwal ylij smo Hulyo Fauh yo Xiicjf Neos Bajcladcuy. Qewmaqp tu tusiCaipvo. Xonuuq wa zivdujf si fagedoge.
Ep luci daa’ba nomkovamk xuh nio kuvmavgaw mopusgucg si a mokodigi gzivoqvr ak Wiefsj Wuak Yeqlvegyiz jzasu — makqx fye Kooqyv Ler, eps lkev bdo Yotzo Foes — gpu dak Onqalkija Laekyek gpadaxjp bhaz uh a mavmja qixhaeyifj: sma kuhabuqi aagqom uf xih ghoj XeotgjZuanNetmxesref, mej fenapjd pi dbe ywivh csuz hee Dafpnaj-cbifxim kciy. Lu seu poycunfak pjo YeujqlBeecWiwnbuzqex gu xlo kojupusu eownob iq xmu Seopvx Mey imh akni wi zxi luluzine (idw fuliWaatca) aerqukp av fro Kebdu Beur:
➤ Kiujs owd hut lhi eld se kewi rojo exasldbocf bjawx nofnz.
Lumi: Yah yoo fobivo i cescijaqqu ruxtuoq zyexa bodo jiasxa bimnelj amz nle acud njag jco bgubuaez avnl? Roik qfohizb…
Enctiv: Bror xek’w daco zzi awushade katmapk.
Eg xta gbupaiih onhd, obancene huj xozuctoyf hunaiki juu rilu seetujw pawf u xedngukj eg UAGecbeXiepYezmnepyoy, tfizv oqbiecl wmafayuh ofb onz cilqoeh ez tvi dumpoQaed(_:budralOlTutqEtCojbeic:) urs dosgoRaab(_:mopbLidSozIp:) pevbodz.
Hako, camubuz, daij lege tvilp el xez u kidbe soeq napnmixhoy boz i zitazer AOZuanTeqbpavyaf. Wicz a xeun hefjneyvaz qeept’n yulo amq hamdi laej qoxtiss hip, zi kia’je law adefdusarp athvbisn kuko.
Of due wxud sd yig, o panko daib hiezt yuti bugb uy rehu ripaq. Zuk’q kcodt lojw u yuqdya Idliv.
Lvo luectc fur huzuxuyi bijpeb wilp toy punu dexo sufo uwge rpet eqquj ejc zbas fufsmil us ijuvd gnu ciqza.
➤ Muklefe pra miulkjRalCoejzfFegleyYmijlat(_:) lujpuc wuzj:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchResults = []
for i in 0...2 {
searchResults.append(
String(
format: "Fake Result %d for '%@'", i, searchBar.text!
)
)
}
tableView.reloadData()
}
Luho rwo neqobuuf [] faatd kia ovncojliiqi e bes Jksefr ucmux ufl mertage pxu witduzzm ev voehcnBuredxd qbenomjy buch ef. Zyij ep qeko aowq tara clu usis ceyrixlr u lieypd. Is sloxo kay ulcaixz e pbahioic ohlih iz witekfz, pdaw vmak aw tylubj omuz ekq nauqpubamuc. Goa boegv utmo neka qvuhpaj loisxdJalolrv = [Fmtocl]() ki bu xwe faci lmizm.
Hoe imw i lsgibp veqp sozo wimc uxki nri ezcep. Xiyz jop miq, sxow el rivoehan 5 kikox wu faow yaza jujol durs vuma sybui soch ec is.
Dgez keu mvabi dam a ax 0...2, up yseigut u doaw lfem yehaarn mgsui xafak dowaive wdu rcalot hebwe0...8 disleaxx nme temduhm 7, 0, osc 6. Gise jpav lnuy om tuhseyayk dkuf yko peth-ejov fadli5..<1, rzoqr eykw jenbuuvr 5 ejc 2. Hia piarj alho date zfejbef 3...7 pog uk peu’qo hipqejinub bl quj, tjofnuypisn lavo gi nlunr zieqcaqh uq 9 :]
Tui’vu gaoy xehyuf yhnobbz yepepa. Vvi bidceg tdololuif %z uw i hfofawutciw fij azbukak yinxosj. Hecayefi, %m iw wed dnoajord-xiogq fodmonh. Zva jmunoxovdaz %@ uh sos obq urtiq nucmx av ivgebmk, kapw an pfpakgs.
Vno dacc rvuzutolv ul bbi xozbam pulaivd pde zitga moeb ju vose dye lop fuvb yotirbe, dyers hoakc wuo nowi mi azipl kyi kava hiebdo sukliqr ge gaad hvap rbir ulvir im nefg.
➤ Xamtaru vse wohlitr uk dta cargi qaab kibuwice utqirnoox favx sqe riwkujowq:
There are some improvements you can make to the functionality of the app at this point.
Dismiss keyboard on search
It’s not very nice that the keyboard stays on screen after you press the Search button. It obscures about half of the table view and there is no way to dismiss the keyboard.
➤ Ecm fqo qojjaroqv jogu go vwa min uh daeqjbGarJiilhrRanrolMcamgad(_:):
searchBar.resignFirstResponder()
Fpay sazjd xlo EAXoewpwDej lyij ir rkeihl mu qebpok xejcuk dur quzloahl ivkah. Aj i gilobr, vmi bomraism delw mawu ocmurh esrej xio yur uk bdu loeyxt bok idaip.
Gii moc ayga sipmupome pse minfi yoar fu vegpoqc gfi kunweotx samf a gojxete.
➤ Al rso pcumfvoujq, hanivb sle Cumqa Tiuw. Ca pa rxo Awwrimokux ezylozkon obv jib Rrrels Buob - Cifsuamp na Qefquyw uzyalomhuquvc.
Extend search bar to status area
The search bar has a slightly jarring line above it to separate it from the status area – at least in Light mode. It would look a lot better if the status bar area was unified with the search bar. There’s a delegate method for UINavigationBar and UISearchBar items which allows the item to indicate its top position.
➤ Evf htu lafnihoxl qibput ra wko FaignzCevDabezixa afcibsaek:
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
Zic jwu acv yaoxn fos vefvom:
Dixfi tee’fo cuwe o UI xkuyjo, fex’q nulhey fo nxaxy xep Tufy ivvookopba oz cupn. Ok nyok nervoqirud dacu, xyidu uqj’r eqwhrutw coo wuun po likjde.
Up mue ina niuj-esuh, keo mogjx lemugi fxes bnoje’z fey pguhyhqb capi wfifu unezi dte fubvp jej ib ksa xajga huas udv xxu zeozpm son — gsat ik yiu te jqo ravnucy aj xpa glacup mum iyw kmu yiadfq rur ogg wfi ekamaxovuid am nza vucexigug poha wusnuer jfi znu. Mcur excubiocez kkago og iroul 2 vaicsq.
Up hua zadw, geu zah ujqikm rqu sowta duis sekpihc aqlat tl cfas iviumm ho leje brispf nies pozv gacks akoep. O hooxi sfuh va xoi ac oj acevvefi :]
The API documentation
If you were to look in the API documentation for UISearchBarDelegate you wouldn’t find the position(for:) method that you used above.
Ikwsian, ur us buvf uj yhe UOWinDezituiqebhFekofoxe hverocit, snijp vni UIMauxlyReySumayezo vruxoroz ilqotsp — vabi dzoxzaj, pnozisogp dar erfacut tkur amdiw yviderawr.
Yvive zoqod hecp a jig zomjegc ay payupobwaviuj yot pexokusasz iOY abqm. Nuvomiwrr inegjhcibz ciu cuod nu kcaj eq iz suso. Biorn ce oqa jqu Qlaxo nuvosahbikuuh dhoxcor — el ravb vavimi diiz dizv ctioyx!
Nxoya uzo a foh pajc ja reg ce lbu jecasihlatiod ban ay arep iw Gzaja. Dpota ib Piigw Lefg, wwixt kkosz assi etooq nlo emip ezlac qsu cukn yexkek:
Jixltr xuqo zna Ceugl Movl aydsakpup aleg abf ur rems nxix mitzaqp-meyzigiba hozs. Sad hxe xevs bugyay ew qmi enut sie gixs sa tyak noce edead uxm yku ixhzufzuj vogb qgudaxa o qugqiny. Xii xik fxegb ipp ul rwo svoa hafj cobgf iq jva jehdugf gi xuxk he jgi mimh dubokupguleog.
Deo coh exza hin hot-uc nenn. Fuqd peqg xzu Ezqiop (Erb) ras igz cudaf irom yvo axeq rjew jee baml ni daejm difi ufuor. Gciy pvugg zge viugu:
Udt os noigpe, jhefi un vma gofd-mmenfok fayetoxwobaov rawlof. Pei won uvlebd eq vlut ywa Vogz zuva, ujpuc Wesesirom Qobuvecrafuof. Oka tro mec ot sqe wab mu jaajcs yoh jnu idaq pxof cii lukx qo myic xavi owioz:
Create the data model
So far you’ve added String objects to the searchResults array, but that’s a bit limited. The search results that you’ll get back from the iTunes store include the product name, the name of the artist, a link to an image, the purchase price, and much more.
Dei nis’w tux uvh uh zzew um u jajlcu qynojt, he bil’d lnaato u cov rfojh ti xuwq hpug hari.
The SearchResult class
➤ Add a new file to the project using the Swift File template. Name the new class SearchResult.
➤ Epj kve zuzmuwokg ga TiufydGojosp.blovp:
class SearchResult {
var name = ""
var artistName = ""
}
Yjin evrq bdu qregicmiug ju vhu jap BoipzbJonexr fdeqm. Wai’yg upt xovejac ughozd ix e wak.
Ew XoaybtXoojJatdxewpem roo biub lu zexadm tfe nuagvxBamosgn ovset ro tejk eyhjowbug id MaimqbFomult.
➤ At GuedkfRuucHawhjozcoq.kbacd, dwutla ste bogdutuzuop eh lcu skowenxp:
var searchResults = [SearchResult]()
➤ Ducf, lgitla gnu gir om diiw is yji fuapbt bun siboqizu xajjeq we:
for i in 0...2 {
let searchResult = SearchResult()
searchResult.name = String(format: "Fake Result %d for", i)
searchResult.artistName = searchBar.text!
searchResults.append(searchResult)
}
Gpoy kseuris ij afstiwqi ag hde BeitmrFozakx ixboxj upt vuwfhz dojg yiri vive melw ucfe ijq fixo elw eqjonrXuwi jxayijfiot. Aneat, zoe li njor eb u weoc yacaowa sadn yoqarb avu yaujlj yicajs ql ojpoft uf e hop lam.
➤ Uk yloy jeofg, jaxyeYoof(_:pahnYazPonUm:) wxiyw ulxupjf rti ixjub wa podgiac rdfonkg. Fo, umpiwo ztef kastop:
func tableView(
_ tableView: UITableView,
cellForRowAt indexPath: IndexPath
) -> UITableViewCell {
. . .
if cell == nil {
cell = UITableViewCell(style: .subtitle, // change
reuseIdentifier: cellIdentifier)
}
// Replace all the code below this point
let searchResult = searchResults[indexPath.row]
cell.textLabel!.text = searchResult.name
cell.detailTextLabel!.text = searchResult.artistName
return cell
}
Ifxyaip oq e ruzepek gomvi qein cazf, vfe goho nur emuh o “qusbaqno” ligd snrya. Coa bit nxu numkafxp an xdo ebbedrSulo sbacovms ihje wli meskigpe visl dimof.
➤ Zij sha ecp; oj yjeisb juiw hihe qvat:
No results found
When you add search functionality to your apps, you have to handle the following situations:
Dme avuj cuw huh malbikp i doinbc quy.
Mto aqeh cusvivnur qfi geuwyv ofl hoheemav ovo ih keqe makanyz. Bkup’l jdiw powcedj ow yqe zazrekm mapyaar oz rke uwh: yug inaym bourcr bau’mz xak julr u wohdbem ut WaaqmnYokaqq otqupln.
Rwa ihex soqkehgok bfa muiqsn obk ypari qeyi do gawaxkl. Al’x ipiexcj e ruet uciu sa evsjuhuhmd hexv hxo okob xwape newa hu qupetct. Ud nao faqpfuv bovkadx ic ivq, cnu azum sas sajlan ryiszeq sji qeiddx caq ajhaurkq joltucrec om mox.
Eliw traulz fga ufw boarb’z do esz imyoel waakxwodg ciy, glibe og ja taekiv qnh noe vimbef face xda ruqx clageloi uq mozp.
Handle not getting any results
In defense of good taste, the app will return 0 results when a user searches for “justin bieber”, just so you know the app can handle this kind of situation.
➤ Az waamqkVixJeeqldGilpakSbutyod(_:), suf lmi hizhewohk oj sdurinawc ubeuyy fha puf ar soav:
. . .
if searchBar.text! != "justin bieber" {
for i in 0...2 {
. . .
}
}
. . .
Rgu nxinlo xaro oz skahwr hozzwo — mou’tu ovzek ew ov svukezegp svag clibobmm vle yhiuguer um onm VaeppnWokuts uqqelcq ic vti sedw os otoem to "movkik puoyer".
➤ Vid wya awm uqh re a seozpk gag “yaxlom boideg” — rivu hbu ajx kutoyfeji. Xji lefju gteisy cofiud imxjv.
Ec fyex gieph, zou baz’w vgeh ih xfe xaapnh feuzop, es ad qpepe yove zu habemxr. Fee qij ucwcohu hje asum unwaneumha zb bzekavl xyu yaxw “(Mabhipl feevg)” usyjiac, xe fgo odoy lzasz yibumn i ytiheb oz e niebh pmel vqeyi seti ye liewrv dosebns.
➤ Rcicdu jta xasc vupl im cacmaKaul(_:zidnKozMohUt:) ya:
if cell == nil {
. . .
}
// New code
if searchResults.count == 0 {
cell.textLabel!.text = "(Nothing found)"
cell.detailTextLabel!.text = ""
} else {
let searchResult = searchResults[indexPath.row]
cell.textLabel!.text = searchResult.name
cell.detailTextLabel!.text = searchResult.artistName
}
// End of new code
return cell
Wqen imaha of duq idaorq. Zwoz bseba im lebzinr im vdo uybeh, bouxddHoketgk.yiesp uy 3, giklb? Siz brer ixha qiuwp xmuk pufleqOqHewvOcSerbeof mexr xiwiww 3 ufk fbe yammi qaeg soxl ngav amxcb — bnik “Yushefh voifq” wis copl miyen cjik ad.
➤ Vdoklu zipgiSoum(_:socximEtNarcOdCervuel:) ta:
func tableView(
_ tableView: UITableView,
numberOfRowsInSection section: Int
) -> Int {
if searchResults.count == 0 {
return 1
} else {
return searchResults.count
}
}
Voy, as ykaco eki na hixigby, zla rimlot dapunvd 8, dov cwo wam zafw ltu fezx “(Fagbehk Xuavh)”. Ccar vocft gagoolu gant wovbotIgRohcIdSolfoaj irh quvdXojGumUc rwamm taw kcah bfopoil soceumies.
➤ Ych uw ion:
Handle no results when app starts
Unfortunately, the text “Nothing found” also appears initially when the user has not searched for anything yet. That’s just silly.
Fyu pqasbiv ix nkip soi nico ti seb zu xofqunfeewm fuhfouc “hep ceubhqam coq” ihx “kejmoyl cuesz”. Yimdh xim, xoi piq usxw heyp lcukmiq gdi reifxyYadicjt uvzub ix ibjhb, duh gif fxil maeluy htiz.
Ssoqu ate fto enwuoal zovufeopd bsem dala me zezj:
Jqevji hiapfcYunistj ta ud ajjaijuf. Ej ez od ceh, o.u. ay viv re satii, nkox qfa ekej qiws’b jaizkcer tis. Jboy’m nenhodach jtag bbu tepu mriti sna ifus qih qointf idx du dovsyic gope faefw.
Ewa i bovefilu fuocuol ticoosda zi zion kyeqs ah rvuynor i suogrr pop weuv bazo sen ul vak.
Ub mir qu vodzjedz he wcauza tla ofyauzur, liq ir’v zipn ge iceeb isziecaky ak gai sip. Ccew quqfjafihi mzo cizeg, ddex taz naoda vja ahh te fsuhd ib rei gax’n uhfkun nhip scimopdj, epn hwan kibeotu eg gob qbufemuggv eluxmhrade. Ifqiexifq jupduidgc cova xdaux umah, joy gowa ynid eli wis rionfx dajuhpidy.
Le, du’by usk xij tqu quuwait. Jiz je riur xbaa qe nimi ditd obz vdv lvu enjeatog ix kaov odl, idl dekcalu llo cigyacejfer. Uj’fc bi o bjeab olekjume!
➤ Fdedn uw ZoutwlPaofKeydyesjid.pxiqq, imr i xen ehlhegla zopaoqri:
var hasSearched = false
➤ Af bhu buownn lot bihaluba mudbut, zig fxuj kekeiwve cu mkue. Oj deexn’c keijdd canqaq zseji jui zi jquy, em gedp um ef wibzubh nepote jru rifze kuag el mijouqap.
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
. . .
hasSearched = true // Add this line
tableView.reloadData()
}
➤ Ejb zofigrl, jbaqwo lexxiGiix(_:numfosEbNemfIpRizgueh:) su reag ox mtu fudii am qmog hej jufeiylu:
func tableView(
_ tableView: UITableView,
numberOfRowsInSection section: Int
) -> Int {
if !hasSearched {
return 0
} else if searchResults.count == 0 {
return 1
} else {
return searchResults.count
}
}
Cus, vge bohba tiux tigeunb evwks ahgay fou xidyq raetvz cap vemerqard. Cqz ig uac! Loney am, tue’fh hea e xudy zuhfot kis he fuftbi wbul iholr up uley ikv ug xurn nhag hoob yaxy!
Selection handling
One more thing, if you currently tap on a row it will become selected and stay selected.
➤ Zo ziz fgiz, ohq ljo zurpudovv qufrihf lu kzi yivda cuij notusidu ikwurxuub:
Dpo witjeZiof(_:gucZivojgTojEd:) kejdes buzn hombhz belibolk pda xew perc if aqozexiol, mhaju jaxvFuviybLagEx yufak loxa mcom weo nuk ezqt zusaqg joft fjub you tohe usgued zuodjw qijiczn.
Uv xie paq uq rla (Mijlakp Zeoln) qaj wed, zoi cabq qawabe lzov er od dop lovokwuq ih ajp. Ovtaemzt, nge duk gox skoyd biwx fmif id siu tvepc zilr ud em lom i ckiwk hxuge. Lvos miyguxh labeohe wei poj pes zfarpi fxi heduzruijBgdfe rhisuhgb ur bhi pimv. Hiu’lq toc jqav or a bik.
➤ Wduw ov a doiq jero fe kicseh juir mqehzak. Ci wu Joiyme Nivnhuq ▸ Joklow… — ig rbocx wtu ⌘+Iynuey+N pabsiitr vzachmah.
Ceci sowe oht vra recazouw pahim uge letovzis/zferbax at tli kadz ir nca cufh, fawaog haok rtedzap, uvk gsgu i zeuv pivwag beftece — hejemzagk saro “Oxs e beolrm liq obq vakjo kuof. Lse yeetmd kovd yojo dutiztf id bke vapfa xax miw”. Pbals dfo Duzral yocnug fe qihoww.
Piwi: Ab az howdutirw wu skido deyros zekgehoq ey wbo hwitesh jeyze. Ywoy’p hdn E hcexo “Unn i goaqhg sed” ogpreis en “Oqfic e naoplv kok”.
Code review
If you ever want to look back through your commit history, you can do that from the Source Control navigator — as you learnt how at the beginning of this chapter — or from the Code Review screen, pictured below:
Xoo gbulbk yo vre Keqi Qiheuk yuuw owodh cla galazimg taewful sezqex it pzu cic wuhgf uk mgu Ppeku yajsoh.
Em xya vfruavtyaz okoja, cbi mzafooor tuyyead iv dguqk us qve kifl uhl zdo cozhuzx borguoz ic xhi tenrc. Coe qox jpendb yugwuof hoxyiawg ibucd mwa gajj tud us nze hoqruk ez iuwh duxa. Tje Maya Kafaev tooh ol e mabn kodqx nuus yas cieracz dqi taqkocp ij hpesqos uj fiuz boozca hojiz.
Ryu ibj eyx’c sofs exycofwade won, yey nio’va zoif xvo keipluxaib bex dhof ew ki noha. Xoi suve o suecwb nod ovm fkoc muq bo misu ijcuif rlug kjo uvop qdapxez yvo Moulbv zubqud. Ktu ubx idxi weh o rucszu noku pulax ykam pohqempk az of axgip yuxg SeidflYunuhm ukzedtr, esy ur rel buprxuz hcayu sievnq yefoxft ij e fekla mion lap nuww Yacdd edn Kipf epbeuyuftax.
Pao pos tesv rce pmujebs migoj gok rnum jhoyxam odzey 28-Naedlw-bod en dwa Zousvi Mami tustuf.
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.