Вы находитесь на странице: 1из 254

Coepanne

Bnaroapnoc+n .. ,.. ....., .. , ,"


Hac+r 1. Hepnre WilFH ............................................
Hac+r I. Hepnre marn
Hac+r I. Hepnre marn
Fnana 1. Br n .+a knnra..................................................................................................................... 7
C n Objective-C....................................................................................................................... 7
Kak pa6o+ae+ .+a knnra.......................................................................................................... 8
Kak nny+ nporpammnc+r..................................................................................................... 9
Fnana 2. Bama nepna nporpamma.................................................................................................... 10
Yc+anonka cpec+n paspa6o+nnka....................................................................................... 10
Bnakomc+no c Xcode............................................................................................................. 10
C nero nana+r?...................................................................................................................... 12
Kak sanyc+n+r nporpammy?.................................................................................................. 15
H+o e +akoe nporpamma?.................................................................................................... 17
Hac+r II. Kak pa6o+ae+ nporpammnponanne
Fnana 3. Hepemennre n +nnr........................................................................................................... 20
Tnnr...................................................................................................................................... 20
Hporpamma c nepemennrmn................................................................................................. 22
Ynpanenne.......................................................................................................................... 23
Fnana 4. if/else................................................................................................................................... 24
hornneckne nepemennre...................................................................................................... 26
else if..................................................................................................................................... 27
n nm6osna+enrnrx: ycnonnrn (+epnapnrn) onepa+op.................................................. 27
Ynpanenne.......................................................................................................................... 28
Fnana 5. dynknnn............................................................................................................................. 29
Kora ncnonrsona+r qynknnm?........................................................................................... 29
Kak nannca+r n ncnonrsona+r qynknnm?........................................................................... 30
Kak qynknnn pa6o+am+ pyr c pyrom............................................................................... 32
hokanrnre nepemennre, kapr n c+ek............................................................................... 35
Pekypcn............................................................................................................................... 36
Hpocmo+p kapon n o+nannke............................................................................................. 38
Return.................................................................................................................................... 40
Fno6anrnre n c+a+nneckne nepemennre............................................................................. 41
Ynpanenn......................................................................................................................... 43
Fnana 6. Hncna.................................................................................................................................. 44
printf()................................................................................................................................... 44
Henre nncna ........................................................................................................................ 45
Banonnn+enn n nrnoa nenrx nncen............................................................................... 46
Onepannn c nenrmn nncnamn............................................................................................... 47
Henonncnennoe enenne....................................................................................................... 48
Cokpamenna sanncr............................................................................................................ 49
Bemec+nennre nncna........................................................................................................... 50
Ynpanenne.......................................................................................................................... 51
Fnana 7. nnknr.................................................................................................................................. 52
Hnkn while............................................................................................................................ 52
Hnkn for................................................................................................................................. 53
break...................................................................................................................................... 54
continue................................................................................................................................. 56
Hnkn do-while....................................................................................................................... 57
Ynpanenne.......................................................................................................................... 57
Fnana 8. Apeca n ykasa+enn............................................................................................................ 59
Honynenne apecon............................................................................................................... 59
Xpanenne apecon n ykasa+enx.......................................................................................... 60
O6pamenne k annrm no apecy.......................................................................................... 61
Copepxauue
________________________________________________________________________________________________
2
NULL.................................................................................................................................... 63
Xopomnn c+nnr o6nonnenn ykasa+enen............................................................................ 63
Ynpanenn......................................................................................................................... 64
Fnana 9. Hepeana no ccrnke.......................................................................................................... 65
Hporpammnponanne qynknnn c nepeanen aprymen+on no ccrnke................................... 66
Hs6eran+e pasrmenonann NULL....................................................................................... 68
Fnana 10. C+pyk+ypr....................................................................................................................... 69
Ynpanenn......................................................................................................................... 71
Fnana 11. Kyna................................................................................................................................. 72
Hac+r III. Objective-C n Foundation
Fnana 12. O6rek+r........................................................................................................................... 76
Cosanne n ncnonrsonanne o6rek+on.................................................................................. 76
Ana+omn coo6mennn........................................................................................................... 78
O6rek+r n nam+n................................................................................................................ 79
id............................................................................................................................................ 80
Ynpanenne.......................................................................................................................... 80
Fnana 13. Coo6menn....................................................................................................................... 82
Bnoenna o+npanka coo6mennn........................................................................................ 82
Mnoec+nennre aprymen+r................................................................................................ 83
O+npanka coo6mennn nil...................................................................................................... 84
Ynpanenne.......................................................................................................................... 85
Fnana 14. NSString............................................................................................................................ 86
Ynpanenne.......................................................................................................................... 87
Fnana 15. NSArray............................................................................................................................ 88
NSMutableArray.................................................................................................................... 90
Ynpanenne.......................................................................................................................... 91
Fnana 16. okymen+ann paspa6o+nnka.......................................................................................... 93
Cnpanonnre c+pannnr......................................................................................................... 94
Brc+pa cnpanka.................................................................................................................. 96
pyrne nosmonoc+n n pecypcr......................................................................................... 98
Fnana 17. Ham nepnrn knacc........................................................................................................... 99
Mec+o oc+yna.................................................................................................................... 101
Tonenna sanncr................................................................................................................. 103
Cnonc+na............................................................................................................................. 103
Ynpanenne........................................................................................................................ 105
Fnana 18. Hacneonanne................................................................................................................. 106
Hepeonpeenenne me+oon................................................................................................. 109
Ynpanenne........................................................................................................................ 110
Fnana 19. O6rek+nre nepemennre .ksemnnpon.......................................................................... 112
Bnaenrnr o6rek+on n ARC.............................................................................................. 114
o6annenne o+nomenn onn ko mnornm n Employee.................................................. 116
Ynpanenne........................................................................................................................ 120
Fnana 20. Hpeo+npamenne y+enkn nam+n................................................................................ 121
nnknnneckoe nnaenne........................................................................................................ 123
Cna6re ccrnkn................................................................................................................... 125
O6nynenne cna6rx ccrnok................................................................................................ 126
n nm6osna+enrnrx: pynnon nocne+ ccrnok n nc+opn ARC..................................... 128
Hpannna nocne+a ccrnok.................................................................................................. 129
Fnana 21. Knaccr konneknnn......................................................................................................... 131
NSArray/NSMutableArray.................................................................................................. 131
Hensmenemre o6rek+r.................................................................................................... 132
Copepxauue
________________________________________________________________________________________________
3
Cop+nponka......................................................................................................................... 133
dnnr+pann......................................................................................................................... 134
NSSet/NSMutableSet.......................................................................................................... 135
NSDictionary/NSMutableDictionary.................................................................................. 138
Hpnmn+nnnre +nnr C........................................................................................................ 139
Konneknnn n nil.................................................................................................................. 140
Ynpanenne........................................................................................................................ 140
Fnana 22. Konc+an+r...................................................................................................................... 141
npnk+nnr npenponeccopon.............................................................................................. 141
Fno6anrnre nepemennre................................................................................................... 143
enum.................................................................................................................................... 145
Fnana 23. Banncr n qann c ncnonrsonannem NSString n NSData................................................ 147
Banncr NSString n qann..................................................................................................... 147
NSError................................................................................................................................ 148
H+enne qannon c ncnonrsonannem NSString.................................................................... 149
Banncr o6rek+a NSData n qann........................................................................................ 150
H+enne NSData ns qanna................................................................................................... 151
Fnana 24. O6pa+nrn nrson............................................................................................................ 153
Moenr npnemnnk/enc+nne........................................................................................... 154
Bcnomora+enrnre o6rek+r............................................................................................... 157
Ononemenn........................................................................................................................ 160
H+o ncnonrsona+r?............................................................................................................. 161
O6pa+nre nrsonr n nnaenne o6rek+amn........................................................................ 161
Fnana 25. Hpo+okonr..................................................................................................................... 163
Fnana 26. Cnnckn cnonc+n.............................................................................................................. 165
Ynpanenne........................................................................................................................ 168
Hac+r IV. Co6r+nnnoe nporpammnponanne
Fnana 27. Hepnoe npnnoenne iOS............................................................................................... 170
Hannem pa6o+y na iTahDoodle......................................................................................... 171
Moenr-Hpec+annenne-Kon+ponnep................................................................................ 175
enera+ npnnoenn.......................................................................................................... 177
Horo+onka npec+annennn................................................................................................ 178
Brnonnenne n iOS Simulator............................................................................................. 179
Cnsrnanne +a6nnnnoro npec+annenn........................................................................... 180
o6annenne nonrx saan.................................................................................................... 183
Coxpanenne saan............................................................................................................... 184
n camrx nm6onr+nrx: kak nacne+ main()?.................................................................. 185
Fnana 28. Hepnoe npnnoenne Cocoa........................................................................................... 186
Peak+nponanne qanna BNRDocument.h.......................................................................... 187
Bnakomc+no Interface Builder............................................................................................. 188
Peak+nponanne qanna BNRDocument.xib....................................................................... 189
Cnsrnanne npec+annennn............................................................................................... 193
Cnona o MVC...................................................................................................................... 196
Peak+nponanne qanna BNRDocument.m......................................................................... 197
Ynpanenenne.................................................................................................................... 199
Hac+r V. Pacmnpennre nosmonoc+n Objective-C
Fnana 29. init................................................................................................................................... 201
Hanncanne me+oon init...................................................................................................... 201
Hpoc+enmnn me+o init...................................................................................................... 202
Hcnonrsonanne me+oon oc+yna....................................................................................... 204
Copepxauue
________________________________________________________________________________________________
4
init c aprymen+amn.............................................................................................................. 204
da+anrnrn nrson init......................................................................................................... 210
Fnana 30. Cnonc+na......................................................................................................................... 211
A+pn6y+r cnonc+n.............................................................................................................. 212
Hopo6nee o konnponannn................................................................................................. 214
Banncr knmn-snanenne................................................................................................... 216
Fnana 31. Ka+eropnn...................................................................................................................... 219
Fnana 32. Bnokn.............................................................................................................................. 221
Onpeenenne 6nokon.......................................................................................................... 221
Hcnonrsonanne 6nokon....................................................................................................... 222
O6rnnenne 6nonnon nepemennon..................................................................................... 223
Hpncnannanne 6noka.......................................................................................................... 224
typedef................................................................................................................................. 228
Bosnpamaemre snanenn.................................................................................................... 228
Ynpannenne nam+rm......................................................................................................... 229
Byymee 6nokon................................................................................................................. 230
Ynpanenne........................................................................................................................ 230
Hac+r VI. He+pnnnanrnre nosmonoc+n C
Fnana 33. Hopaspnre onepannn................................................................................................. 233
Hopaspna onepann HhH............................................................................................. 234
Hopaspna onepann H................................................................................................... 235
pyrne nopaspnre onepa+opr....................................................................................... 237
ononnenne......................................................................................................................... 238
Bonrme 6an+on................................................................................................................... 240
Ynpanenne........................................................................................................................ 240
Fnana 34. C+pokn C........................................................................................................................ 241
char...................................................................................................................................... 241
char *................................................................................................................................... 242
C+pokonre nn+epanr......................................................................................................... 244
Hpeo6posonann k NSString n o6pa+no............................................................................. 246
Ynpanenne........................................................................................................................ 246
Fnana 35. Maccnnr C..................................................................................................................... 247
Fnana 36. Aprymen+r komannon c+pokn..................................................................................... 250
Fnana 37. Komana switch.............................................................................................................. 253
Copepxauue
________________________________________________________________________________________________
5
I
Hepnre marn
!" $% & '() *+&,)
anan+e nemnoro noronopnm o nac. Br Xo+n+e nnca+r npnnoenn n iOS nnn Mac OS,
no npomnrn onr+ nporpammnponann y nac nenennk (a moe+, ero n nonce ne+). Bamn
pysr xnannnn pyrne mon knnrn (iOS Programming: 'The Big Nerd Ranch Guide n
Cocoa Programming for Mac OS X),no onn nanncanr n onr+nrx nporpammnc+on. H+o
ena+r? Hn+an+e .+y knnry.
Ec+r pyrne noxone knnrn, no nam c+on+ nponn+a+r nmenno .+y. Honemy? B ye
anno yny nmen nnca+r npnnoenn n iOS n Mac, n ycnen non+r, n+o nam
neo6xonmo sna+r na .+on c+ann namero ny+emec+nnn. B ycepno +pynnc na +em,
n+o6r co6pa+r .+n snann n o+6pocn+r nce nnmnee. B .+on knnre onenr mnoro
nonesnoro n onenr mano menyxn.
Hoanyn, komy-+o mon noxo nokae+c neo6rnnrm. Bmec+o +oro n+o6r
nopo6no pasenrna+r cnn+akcnc Objective-C, pacckay nam, kak pa6o+ae+
nporpammnponanne n n+o o nem ymam+ onr+nre nporpammnc+r.
Hs-sa nr6pannoro noxoa ye na o+nocn+enrno pannnx c+pannnax knnrn
nsnarae+c ononrno cepresnrn ma+epnan. He n+e, n+o .+a knnra c+ane+ nerknm
n+nnom. Kpome +oro, non+n kaa ne conponoae+c .kcnepnmen+om n
nporpammnponannn.
Cone+anne yne6nrx konnennnn n nx nemenennoro npak+nneckoro npnmenenn -
nynmnn cnoco6 nsynenn nporpammnponann.
- & ./012345167
Kora nr sanyckae+e nporpammy qann konnpye+c ns qannonon cnc+emr n nam+r
komnrm+epa, n komnrm+ep nrnonne+ nnc+pyknnn, coepamnec n qanne. Henoneky
.+n nnc+pyknnn conepmenno nenon+nr, no.+omy nmn nnmy+ komnrm+epnre
nporpammr na cnennanrnrx srkax nporpammnponann. Bsrk nporpammnponann
camoro nnskoro yponn nasrnae+c accem6nepom. Ha accem6nepe nporpammnc+y
npnxon+c onncrna+r kaoe enc+nne, nrnonnemoe nponeccopom (+o ec+r mosrom
komnrm+epa). Honynennrn ko npeo6pasye+c n mamnnnrn ko (ponon srk
komnrm+epa).
Hporpammr na accem6nepe ckynnr, nnnnr n sanncnmr o+ nponeccopa (no+omy
n+o mosr namero nonenrkoro iMac moe+ ocnona+enrno o+nnna+rc o+ mosra nm6nmoro,
no nsnomennoro PowerBook). Hnane ronop, ecnn nr saxo+n+e sanyc+n+r nporpammy na
komnrm+epe pyroro +nna, nam npne+c nepennca+r accem6nepnrn ko.
H+o6r nporpammnrn ko mono 6rno nerka nepenocn+r c onoro +nna
komnrm+epon na pyron, 6rnn paspa6o+anr, nrcokoyponnenre srkn
nporpammnponann. C nrcokoyponnenrmn srkamn nporpammnc+y ne nyno yma+r o
Fnaaa 1. Bm u s1a kuura
________________________________________________________________________________________________
7
cnennqnke konkpe+noro nponeccopa - on nnme+ o6o6mennre nnc+pyknnn, a
cnennanrna nporpamma (nasrnaema komnnn+opom) npeo6pasye+ .+o+ ko n
nrcokoon+nmnsnponannrn mamnnnrn ko n konkpe+noro nponeccopa. Onnm ns +aknx
srkon nne+c srk C. Hporpammnc+r C nnmy+ ko na srke C, a komnnn+op C
npeo6pasye+ ko C n mamnnnrn ko.
Bsrk C 6rn cosan n nanane 1970-x roon n qnpme AT&T. Onepannonna
cnc+ema Unix, sanoenna n ocnony Mac OS X n Linux, 6rna nanncana na C c
ne6onrmnmn nc+ankamn accem6nepnoro koa n camrx nnskoyponnenrx onepannn.
Onepannonna cnc+ema Windows +oe 6onrmen nac+rm nanncana na C.
Bsrk nporpammnponann Objective-C ocnonan na C, no n nem o6annena
noepka o6rek+no-opnen+nponannoro nporpammnponann. Hmenno na Objective-C
nnmy+c npnnoenn n onepannonnrx cnc+em Apple iOS n Mac OS X.
8)* 9):;()<( '() *+&,)
B .+on knnre nr ysnae+e o srkax nporpammnponann C n Objectivc-C oc+a+onno, n
+oro n+o6r naynn+rc nnca+r npnnoenn n yc+ponc+n iOS n Mac.
Honemy 6yy cnanana ynn+r nac C? Karn rpamo+nrn nporpammnc+ Objective-
C onen oc+a+onno rny6oko nonnma+r C. Kpome +oro, mnorne nen, ko+opre n
Objective-C nrrn+ ononrno cnonrmn, yxo+ kopnmn k npoc+rm konnennnm C.
Hac+o 6yy npec+ann+r neko+opym nem na C, a sa+em nanpann+r nac k
conpemennomy ananory +on e nen na Objective-C.
Hpenonarae+c, n+o nr 6ye+e nn+a+r .+y knnry nepe Mac, Br6ye+e nn+a+r
o6rcnenn pasnnnnrx nen n nponon+r npak+nneckne .kcnepnmen+r, ko+opre
nocnm+ .+n nen. 3kcnepnmen+r - o6sa+enrna nac+r o6ynenn. Bes nnx nr ne
cmoe+e non+r ma+epnan. hynmnn cnoco6 nsynenn nporpammnponann - nnon+r
nporpammnrn ko, ena+r onena+kn, ncnpann+r nx n noc+enenno npnnrka+r k
ocnonnrm sakonomepnoc+m srka. O+ npoc+oro n+enn koa n +eope+nneckoro
nsynenn nen nn nam, nn namnm nanrkam nporpammnponann oco6oro npoky ne 6ye+.
B konne kaon rnanr npnno+c ynpanenn. Onn nosnon+ nam
ononnn+enrno no+pennpona+rc n sakpenn+r +onrko n+o nonynennre snann. B
nac+o+enrno pekomenym nrnonnn+r c+onrko ynpanennn, ckonrko cmoe+e.
Take n konne neko+oprx rnan nc+penam+c pasenr, osarnannennre. n
nm6osna+enrnrx. B nnx coepn+c yrny6nennoe o6rcnenne nonpocon,
paccmo+pennrx n rnane. 3+n pasenr ne nnm+c a6conm+no neo6xonmrmn n
oc+nenn namnx nenen, no naemcr, n+o npnneenna nnqopmann nokae+c nam
nn+epecnon n nonesnon.
Y cepnn Big Nerd Ranch nmee+c qopym, na ko+opom nn+a+enn o6cyam+ knnrn
n coepamnec n nnx ynpanenn. dopym mono nan+n no apecy http://
forums.bignerdranch.com/.
H .+a knnra, n nporpammnponanne noo6me nokay+c nam namnoro 6onee
npn+nrmn, ecnn nr nnaee+e nanrkamn cnenon nena+n. Kpome TOFO, n+o cnena
nena+r namnoro yckope+ nno, ona eme n nosnone+ nam cmo+pe+r na .kpan n n knnry
Fnaaa 1. Bm u s1a kuura
________________________________________________________________________________________________
8
nmec+o knanna+ypr. 3+o cymec+nenno ynpomae+ nrnnenne omn6ok. Hanrkn cnenon
nena+n npnro+c nam na npo+ennn ncen namen kaprepr.
8)* =&>?( @9;,9)AA&B(%
O+kprn .+y knnry, nr pemnnn c+a+r nporpammnc+om. A pas +ak, nr onnr sna+r, n+o
nac e+.
Xnsnr nporpammnc+a n ocnonnom coc+on+ ns nenpec+annon 6opr6r. Pemenne
saan n noc+onno menmmemc +exnnneckom okpyennn osnanae+, n+o nporpammnc+r
ncera nsynam+ n+o-+o nonoe. Bnponem, nsynenne nero-+o nonoro ckasano ckopee n
npnnnnn: n ocnonnom penr ne+ o 6opr6e c co6c+nennrm neneec+nom. ae ecnn
nporpammnc+ pa6o+ae+ co snakomon +exnonornen, cosanaemre nporpammr nopon
nac+onrko cnonr, n+o na o6rnnrn nonck omn6kn nopon yxon+ nenrn enr.
Ecnn nr co6npae+ecr nporpammnpona+r, nac e+ 6opr6a. Bonrmnnc+no
npoqeccnonanrnrx nporpammnc+on npnynae+c 6opo+rc nac sa nacom, enr sa nem,
+epnennno n 6es (oco6oro) paspaenn. 3+o eme onn nanrk, ko+oprn nam cnnrno
npnron+c. Ecnn nac nn+epecye+ nsnr nporpammnc+on n neenne conpemennrx
nporpammnrx npoek+on, pekomenym knnry Cko++a Posen6epra (Scott RosenBerg)
Dreamil1g in Code .
A +enepr nopa ns+rc sa eno n nannca+r namy nepnym nporpammy.
Fnaaa 1. Bm u s1a kuura
________________________________________________________________________________________________
9
C" $)D) @<9>)E
@9;,9)AA)
H+ak, +enepr nr snae+e, kak yc+poena .+a knnra. anan+e nocmo+pnm, kak e
nponcxon+ nporpammnponanne n Mac, iPhone n iPad. n .+oro nam npec+on+:
yc+anonn+r cpec+na paspa6o+nnka qnpmr Apple;
cosa+r npoc+on npoek+ c ncnonrsonannem .+nx cpec+n;
nosnakomn+rc c +em, kak pa6o+ae+ nnc+pymen+apnn paspa6o+nnka. n+o6r o6ecnenn+r
pa6o+ocnoco6noc+r namero npoek+a.
K konny .+on rnanr nr ycnemno nannme+e cnom nepnym nporpammy n Mac.
cvanonka cpe(cvn paspa6ovunka Apple
n nanncann npnnoennn n Mac OS (Macintosh) nnn iOS (iPhone n iPad) nr
6ye+e ncnonrsona+r cpec+na paspa6o+nnka Apple. Hporpammr mono sarpysn+r no
apecy http://developer.apple.com/ nnn npno6pec+n nx n Mac App Store.
Hocne yc+anonkn cpec+n paspa6o+nnka nann+e nanky /Developer n kopnenom
ka+onore cnoero ec+koro ncka. B .+on nanke coepn+c nce neo6xonmoe n
paspa6o+kn npnnoennn n nac+onrnrx cnc+em Mac OS X n mo6nnrnrx yc+poc+n iOS.
Hon+n nc nama pa6o+a n .+on knnre 6ye+ nrnonn+c n onom npnnoennn -
Xcode ns nankn /Developer/Applications. (B 6r pekomenonan nepe+amn+r snanok Xcode
na ok-nanenr; nr 6ye+e onenr nac+o ero ncnonrsona+r)
3nakoucvno c Xcode
Xcode - !"#$%&!&'()"")* +&$,) &)-&).'#/! (IDE, Integrated Development
Environment) qnpmr Apple. 3+o osnanae+, n+o n Xcode npncy+c+nye+ nce neo6xonmoe
n nanncann, noc+poenn n sanycka nonrx npnnoennn n Xcode.
Ono samenanne no nonoy +epmnnonornn: nm6re ncnonnemre qannr na
komnrm+epe nasrnam+c nporpammamn. Heko+opre nporpammr nmem+ rpaqnnecknn
nn+epqenc; nx mr 6yem nasrna+r 0&!1'2$"!*3!.
pyrne nporpammr ne nmem+ rpaqnneckoro nn+epqenca n nenrmn nmn
pa6o+am+ n qononom penme: onn nasrnam+c ,$3'")3!. Tepmnn snynn+ yc+pamamme,
no n camnx nporpammax nnnero c+pamnoro ne+. Bepo+no, npmo cennac na namem Mac
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
10
nrnonne+c okono 60 emonon. Onn y+ cnoero naca, n+o6r nomonr nam n
nrnonnennn kakon-nn6o onepannn, Hanpnmep, onn ns emonon, pa6o+ammnx n namen
cnc+eme, nasrnae+c pboard. Kora nr nrnonne+e onepannm konnponann/nc+ankn,
emon pboard xpann+ konnpyemre annre.
Heko+opre nporpammr ne nmem+ rpaqnneckoro nn+epqenca n nrnonnm+c n
+enenne nenpoonn+enrnoro npemenn n +epmnnanrnom okne; mr 6yem nasrna+r nx
0&'%&)33)3! /'3)","'4 +#&'/!. B knnre mr n ocnonnom 6yem nnca+r nporpammr
komannon c+pokn, n+o6r cocpeo+onn+rc na ocnonax nporpammnponann, ne
o+nnekacr na cosannn n ynpannennn nonrsona+enrcknm nn+epqencom.
H+ak, cennac mr nannmem n Xcode npoc+ym nporpammy komannon c+pokn,
n+o6r nr n o6mnx nep+ax nonnn, kak .+o enae+c.
B xoe nanncann nporpammr nam npnxon+c cosana+r n peak+npona+r
neko+opre qannr. Xcode xpann+ nnqopmannm o6 .+nx qannax n npoek+e. Banyc+n+e
Xcode, nr6epn+e n menm File komany New, a sa+em New Project...
H+o6r nam 6rno npome npnc+ynn+r k pa6o+e, Xcode npenarae+ neckonrko
nosmonrx ma6nonon npoek+on. Ea6non nr6npae+c n sanncnmoc+n o+ +oro, kakym
nporpammy nr co6npae+ecr nnca+r. B nenom c+on6ne nr6epn+e n pasene Mac OS X
c+poky Application. Ba+em nr6epn+e snanok Command Line Tool cpen napnan+on n
o6nac+n cnpana.
Hamn+e knonky Next.
Hpncnon+e npoek+y nm AGoodStart. Hen+nqnka+op komnannn n ynpanennx
namen knnrn ponn ne nrpae+, no nam npne+c nnec+n ero n npoonenn pa6o+r.
Bnen+e c+poky BigNerdRanch nnn pyroe o6osnanenne no namemy ycmo+pennm. B
packprnammemc cnncke Type nr6epn+e c+poky C, no+omy n+o nporpamma 6ye+
nanncana na srke C. Hakonen, npocnen+e sa +em, n+o6r qnaok Use Automatic
Reference Counting 6rn yc+anonnen.
Pnc 2.1 Br6op ma6nona
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
11
Pnc 2.2 Hac+ponka konqnrypannn

Hamn+e knonky Next.
Tenepr nr6epn+e nanky, n ko+opon 6ye+ cosan ka+anor npoek+a. Penosn+opnn
n kon+pon nepcnn nam ne nonao6n+c, .+o+ qnaok mono cn+r. Oc+ae+c
menkny+r no knonke Create.
Bam npec+on+ cosana+r npoek+r .+oro +nna n neckonrknx 6nnanmnx rnanax.
B 6yymem npoc+o nannmy: Cosan+e nonrn npoek+ nporpammr komannon c+pokn C
c nmenem #)/!3-#' - nrnonnn+e +y e nocneona+enrnoc+r enc+nnn.
(Honemy C? Bcnomnn+e, n+o Objective-C noc+poen na 6ase srka
nporpammnponann C. Br onnr ycnon+r ocnonnre non+n C, npee nem mr
cmoem nepen+n k cnennqnke Objective-C.)
C uero nauav?
Hocne cosann npoek+a na .kpane nonne+c okno c nnqopmannen
!"##$%&'(&.
B .+om okne npnno+c pasnre nopo6noc+n: n kaknx nepcnx Mac OS X 6ye+
sanycka+rc name npnnoenne, kakne konqnrypannn onnr ncnonrsona+rc npn
komnnnnnn nanncannoro koa, kakne nokannsannn onnr npnmen+rc n npoek+e. Ho
noka ne 6yem o6pama+r na nnx nnnmann n nanem npoc+ym o+npannym +onky n
nanana pa6o+r.
Hann+e n nepxnen nac+n nenon nanenn qann c nmenem main.c n menknn+e na nem.
(Ecnn nr ne nnn+e c+poky main.c, menknn+e na +peyronrnnke pom c nankon
AGoodStart, n+o6r o+kpr+r coepnmoe nankn.)
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
12
Pnc. 2.3. Hepnrn nsrn na npoek+ AGoodStart
Pnc. 2.4. dann main.c n rpynne AGoodStart
O6pa+n+e nnnmanne: ncxonoe npec+annenne c nnqopmannen o npnnoennn
samene+c coepnmrm qanna main.c. dann main.c coepn+ qynknnm c nmenem main.
56"/7!* npec+anne+ co6on cnncok nnc+pyknnn, ko+opre onen nrnonnn+r
komnrm+ep. Y kaon qynknnn ec+r nm. B nporpammax na srke C nnn Objective-C
qynknn main nrsrnae+c npn sanycke nporpammr.
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=> ?
@@ *+2/(& ,#$/ 4/(/333
A(*+&B7CD/--#9 E#(-$FG+C>H
(/&.(+ IH
J
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
13
B .+on qynknnn npncy+c+nye+ na nna nnqopmannn: nporpammnrn ko n
kommen+apnn

Hporpammnrn ko - na6op nnc+pyknnn, ko+opre npnkasrnam+ komnrm+epy


nrnonnn+r neko+opre enc+nn.

Kommen+apnn nrnopnpye+c komnrm+epom .Mr, nporpammnc+r, ncnonrsyem nx


n okymen+nponanna nanncannoro namn koa. Hem +pynee saana, ko+opym nr
nr+ae+ecr pemn+r, +em 6onrme nonrsr o+ kommen+apnen, onncrnammnx ee pemenne.
Hnqopmann c+anonn+c oco6enno nanon, kora nr nepne+ecr cnoen pa6o+e nepes
neckonrko mecnen, nocmo+pn+e na ko, ko+oprn nr sa6rnn npokommen+npona+r, n
noymae+e: Hanepnka onenr ymna m+yka, no+ +onrko nannc+o sa6rn, kak ona
pa6o+ae+.
B C n Objective-C kommen+apnn mono o+nnnn+r o+ koa no nym npnsnakam:

Ecnn c+poka nannnae+c c cnmnonon //, +o nce, nannna o+ .+nx cnmnonon n o


kanna c+pokn, cnn+ae+c kommen+apnem. Hpnmep - kommen+apnn insert code here o+
qnpmr Apple.

Ecnn nr saxo+n+e o6ann+r 6onee o6mnpnoe onncanne, ncnonrsye+c +eo6o


snanenn /* n */ n nome+kn nanana n konna kommen+apnen, sannmammnx neckonrko
c+pok
3+n npannna nome+kn kommen+apnen nnm+c nac+rm +!"#)/+!+) C.
Cnn+akcncom nasrnae+c na6op npannn, ko+opre onpeenm+, kak onen 6r+r nanncan
ko na annom srke nporpammnponann. Hpannna cnn+akcnca n nrcmen c+enenn +onnre
n konkpe+nre; ecnn nr napymn+e nx, +o nama nporpamma pa6o+a+r ne 6ye+.
Cnn+akcnc kommen+apnen o+nocn+enrno npoc+, no cnn+akcnc nporpammnoro koa
cnnrno sanncn+ o+ +oro, n+o n kak enae+ nam ko. Bnponem,ona oco6ennoc+r oc+ae+c
nensmennon: kaa komana sanepmae+c cnmnonom +onka c san+on (;). (Bckope mr
paccmo+pnm npnmepr koman n nporpammnom koe). Ecnn sa6r+r o6 .+om cnmnone, n
nporpamme nonn+c c cnn+akcnnecka omn6ka, n ona pa6o+a+r ne 6ye+.
K cnac+rm, Xcode c+apae+c npeynpen+r nac o noo6nrx omn6kax.
Co6c+nenno, ona ns nepnrx npo6nem, c ko+oprmn nr c+onkne+ecr kak nporpammnc+, -
ymenne non+r, o nem coo6mae+ Xcode npn o6napyennn omn6ok, n ncnpann+r .+n
omn6kn. Ha c+pannnax .+on knnrn mr ynnnm npnmepr coo6mennn Xcode o
c+anap+nrx omn6kax.
anan+e nnecem n main.c neko+opre nsmenenn. n nanana nam nonao6n+c
cno6onoe mec+o. Hann+e qnrypnre cko6kn({ n }), o+menammne nanano n konen
qynknnn main. Yann+e nce, n+o naxon+c mey nnmn.
Tenepr o+peak+npyn+e qann main.+, n+o6r on nrrnen +ak, kak nokasano nne.
o6anr+e n qynknnm main kommen+apnn, ne c+pokn koa n eme onn kommen+apnn. He
6ecnokon+ecr, ecnn nr noka ne nonnmae+e cmrcn nnonmrx cnmnonon - mr +onrko
nannnaem. Bnepen nena knnra, n nr ckopo nonme+e, n+o nce .+o osnanae+.
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
FF G<9>%< B(9;*& 9;A)+)
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
14
HI4J3KLMN3 OPQ 3R1 /1Q3 SK 34T1Q"UJMVW
HI4J3KLMN3 OPQ 3R1 OSIQ3 SK 34T1Q"UJMVW
FXY)* Z& ?= [;9;D; @;Z?\&Z;B]^
$;_A;=+;` B(;&( @<9<@&B)(]" XF
I13aIJ bW
J
(O6pa+n+e nnnmanne: nonrn ko, ko+oprn nam nyno nnec+n, nrenen npnrm
mpnq+om. O6rnnrm mpnq+om nrnon+c ko, ko+oprn ye naxon+c n qanne. 3+o
npannno 6ye+ co6nma+rc no ncen knnre).
Bepo+no, nr same+n+e, n+o no npem nnoa Xcode nr+ae+c nomonr nonesnrm
cone+om. 3+a npesnrnanno yo6na qynknn nasrnae+c )(#'-)($&8$"!$3. Hoka nr
moe+e ne o6pama+r nnnmann na nockaskn n nnon+r necr +ekc+ camoc+o+enrno. B
nocneymmnx rnanax knnrn nonpo6yn+e no.kcnepnmen+npona+r c an+osanepmennem n
y6en+ecr n +om, n+o c nnm ko nnme+c yo6nee n +onnee. Pasnnnnre napame+pr
an+osanepmenn saam+c n nac+ponkax Xcode, nrsrnaemrx ns menm Xcode.
Take o6paman+e nnnmanne na nne+ mpnq+a. Xcode ncnonrsye+ pasnonne+nre
mpnq+r, n+o6r nam 6rno npome ysna+r kommen+apnn n pasnre nac+n koa. (Ckaem,
kommen+apnn nrenm+c senenrm nne+om.) Hne+onoe nrenenne yo6no: nepes
neko+opoe npem pa6o+r c Xcode nr nanne+e nnc+nnk+nnno samena+r, kora nne+a
nrrn+ neec+ec+nenno. Hac+o .+o ykasrnae+ na cnn+akcnneckym omn6ky n
nanncannom koe (nanpnmep, nponymennrn sanepmn+enr +onka c san+on). H nem
ckopee nr ysnae+e o onymennon cnn+akcnneckon omn6ke, +em npome ee 6ye+
ncnpann+r.
Pasnonne+nre mpnq+r - ncero nnmr onn ns cnoco6on, ko+oprm Xcode coo6mae+
o (nosmono) onymennrx omn6kax.
Kak sanycvnv nporpauuy?
Kora coepnmoe qanna main.+ 6ye+ coo+ne+c+nona+r npnneennomy panee,
nopa sanyc+n+r nporpammy n nocmo+pe+r. n+o ona enae+. 3+o+ nponecc coc+on+ ns nyx
maron: Xcode cnanana c+pon+ namy nporpammy, a no+om sanyckae+ ee. B xoe
noc+poenn Xcode ro+onn+ nporpammnrn ko k nrnonnennm. B nac+noc+n, npn .+om
nponepm+c cnn+akcnneckne n pyrne nosmonre omn6kn.
Cnona nsrnnn+e na nenym nanenr okna Xcode. 3+a o6nac+r nasrnae+c
'.1)+#9: ")(!%)7!. B nepxnen nac+n o6nac+n nannrannn naxon+c rpynna knonok. B
nac+omee npem na nanenn o+o6paae+c ")(!%)#'& 0&'$/#), n ko+opom
npec+annenr qannr npoek+a. Knonka nannra+opa npoek+a o+menena snankom
ptg999
How do I run my program?
13
These color differences are just one way in which Xcode lets you know when you (may) have done
something wrong.
How do l run my program7
When the contents of your ma1h.c file match what you see above, it`s time to run your program and see
what it does. This is a two-step process. Xcode builds your program and then runs it. When building
your program, Xcode prepares your code to run. This includes checking for syntax and other kinds of
errors.
Look again at the lefthand area of the Xcode window. This area is called the navigator area. At the
top of the navigator area is a series of buttons. You are currently viewing the project navigator, which
shows you the files in your project. The project navigator`s icon is .
Now find and click the button to reveal the log navigator. The log is Xcode`s way of communicating
with you when it is building and running your program.
You can also use the log for your own purposes. For instance, the line in your code that reads
pJ1h11{"T1 was 1he bes1 o1 11mes.\h"),
is an instruction to display the words 'It was the best of times. in the log.
Since you haven`t built and run your program yet, there isn`t anything in the log navigator. Let`s fix
that. In the upper lefthand corner of the project window, find the button that looks suspiciously like the
Play button in iTunes or on a DVD player. If you leave your cursor over that button, you`ll see a tool tip
that says Build and then run the current scheme. That is Xcode-speak for 'Press this button, and I will
build and run your program.
If all goes well, you`ll be rewarded with the following:
If not, you`ll get this:
What do you do then? Carefully compare your code with the code in the book. Look for typos and
missing semicolons. Xcode will highlight the lines it thinks are problematic. After you find the
problem, click the Run button again. Repeat until you have a successful build.
(Don`t get disheartened when you have failed builds with this code or with any code you write in the
future. Making and fixing mistakes helps you understand what you`re doing. In fact, it`s actually better
than lucking out and getting it right the first time.)
.
Tenepr nann+e knonky
ptg999
How do I run my program?
13
These color differences are just one way in which Xcode lets you know when you (may) have done
something wrong.
How do l run my program7
When the contents of your ma1h.c file match what you see above, it`s time to run your program and see
what it does. This is a two-step process. Xcode builds your program and then runs it. When building
your program, Xcode prepares your code to run. This includes checking for syntax and other kinds of
errors.
Look again at the lefthand area of the Xcode window. This area is called the navigator area. At the
top of the navigator area is a series of buttons. You are currently viewing the project navigator, which
shows you the files in your project. The project navigator`s icon is .
Now find and click the button to reveal the log navigator. The log is Xcode`s way of communicating
with you when it is building and running your program.
You can also use the log for your own purposes. For instance, the line in your code that reads
pJ1h11{"T1 was 1he bes1 o1 11mes.\h"),
is an instruction to display the words 'It was the best of times. in the log.
Since you haven`t built and run your program yet, there isn`t anything in the log navigator. Let`s fix
that. In the upper lefthand corner of the project window, find the button that looks suspiciously like the
Play button in iTunes or on a DVD player. If you leave your cursor over that button, you`ll see a tool tip
that says Build and then run the current scheme. That is Xcode-speak for 'Press this button, and I will
build and run your program.
If all goes well, you`ll be rewarded with the following:
If not, you`ll get this:
What do you do then? Carefully compare your code with the code in the book. Look for typos and
missing semicolons. Xcode will highlight the lines it thinks are problematic. After you find the
problem, click the Run button again. Repeat until you have a successful build.
(Don`t get disheartened when you have failed builds with this code or with any code you write in the
future. Making and fixing mistakes helps you understand what you`re doing. In fact, it`s actually better
than lucking out and getting it right the first time.)
n menknn+e na nen, n+o6r o+kpr+r nannra+op ypnana.
Xcode ncnonrsye+ ypnan (log) n nrnoa coo6mennn n xoe noc+poenn n sanycka
namen nporpammr.
Br +ake moe+e ncnonrsona+r ypnan n cnonx co6c+nennrx nenen.
Hanpnmep, c+poka nporpammnoro koa
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
15
A(*+&B 7CK& L'2 &4/ M/2& #B &*6/23G+C>H
npnkasrnae+ nrnec+n c+poky It was the best of times. n ypnane.
Tak kak nr eme ne noc+ponnn n ne sanyc+nnn nporpammy, n nannra+ope ypnana
nnnero ne+. B nenom nepxnem yrny okna npoek+a nann+e knonky, noospn+enrno
noxoym na knonky Play n iTunes nnn na DVD-nponrprna+ene. Ecnn nanec+n ykasa+enr
mrmn na knonky, nonne+c nockaska c nanncrm BuiId and then run the current
scheme. B +epmnnonornn Xcode .+o osnanae+: Hamn .+y knonky, n noc+pom n
sanymy +nom nporpammy.
Ecnn nce 6rno cenano npannnrno, nr 6ye+e nosnarpaenr cneymmnm
pesynr+a+om:
A ecnn ne+, pesynr+a+ 6ye+ +aknm:
H+o +ora ena+r? Tma+enrno cpannn+e cnon ko c npnneennrm n knnre.
Honmn+e onena+kn n nponymennre cnmnonr +onka c san+on. Cpea Xcode nrene+
c+pokn, n ko+oprx, no ee mnennm, ec+r omn6kn. O6napynn omn6ky, ncnpanr+e ee n
cnona namn+e knonky N.+. Hon+opn+e, noka nporpamma ne 6ye+ noc+poena ycnemno.
(He o+nannan+ecr, ecnn noc+poenne .+oro koa nnn nm6on pyron nporpammr n
knnre ne npomno c nepnoro pasa. Kora nr conepmae+e n ncnpanne+e omn6kn, .+o
nomorae+ nam nynme non+r, n+o nr enae+e. 3+o ae nynme, nem ecnn nam nonese+
npannnrno nce cena+r c nepnoro pasa.)
Kora noc+poenne npone+ ycnemno, nann+e n nepxnen nac+n nannra+opa
ypnana o6rek+ c me+kon O/M.8 !"##$%&'(&. Eenknn+e na nem, n+o6r nrnec+n
coepnmoe ypnana n nocnenero sanycka nporpammr.
Coepnmoe ypnana moe+ 6r+r ononrno nnnnrm. n nac nana nn+a+a ns
nkkenca n camom konne nrnoa. 3+o pesynr+a+ nrnonnenn namero koa!
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
16
"PQ 8$M R3S3TIUVIITIWXT 7!AA-/ ;/(2*#+ 8$MUXYIT> 7Z./ [.- T IY\SR\]T QZ^ VIXX>
^#A_(*84& VII] `(// %#B&L'(/ `#.+$'&*#+9 K+,3
"Oa *2 B(// 2#B&L'(/9 ,#;/(/$ M_ &4/ "PQ "/+/('- b.M-*, c*,/+2/9 '+$ _#. '(/
L/-,#6/ &# ,4'+8/ *& '+$@#( $*2&(*M.&/ ,#A*/2 #B *& .+$/( ,/(&'*+ ,#+$*&*#+23
Z_A/ C24#L ,#A_*+8C &# 2// &4/ ,#+$*&*#+23
Z4/(/ *2 'M2#-.&/-_ +# L'(('+&_ B#( "Oa3 Z_A/ C24#L L'(('+&_C B#( $/&'*-23
Z4*2 "Oa L'2 ,#+B*8.(/$ '2 CdWReR]U'AA-/U$'(L*+C3&&_ @$/;@&&_2IIX
<%L*&,4*+8 &# A(#,/22 VYVS &4(/'$ IdI=
K& L'2 &4/ M/2& #B &*6/23
K& L'2 &4/ L#(2& #B &*6/23
(Ha momen+ nanncann knnrn qnpma Apple pa6o+ana na nonrm o+nannkom
LLDB. Bckope on onen samenn+r GDB, +ekymnn o+nannk. Ecnn nr ne nnn+e ncm
nnqopmannm GDB, .+o osnanae+, n+o c+anap+nrm o+nannkom Xcode +enepr nne+c
LLDB. B nam sannym; +ak sopono okasa+rc n 6yymem!)
Hvo we vakoe nporpauua?
H+ak, nr noc+ponnn n sanyc+nnn cnom nepnym nporpammy. Tenepr anan+e
sarnnem nonny+pr. Hporpamma coc+on+ ns na6opa qynknnn. dynknn npec+anne+
co6on na6op onepannn, nrnonnemrx nponeccopom. Y kaon qynknnn ec+r nm;
+onrko n+o nanncanna namn qynknn nasrnae+c main. Take n nporpammnom koe
ncnonrsye+c pyra qynknn - A(*+&B. Br ne nncann ee, no ncnonrsonann n cnoem
koe. (O +om, o+kya nsnacr qynknn A(*+&B, 6ye+ pacckasano n rnane 5).
n nporpammnc+a qynknn nanomnnae+ kynnnapnrn penen+: onec+n o
knnenn nn+p nor. o6ann+r namky mykn. Hoana+r ropnnm. B cepenne 1970-x
roon nonapenna knnra Be++n Kpokep c+ana npoana+rc n nne na6opa kap+onek c
penen+amn. Kap+onka c penen+amn - ononrno nennoxa me+aqopa n qynknnn. Kak n y
qynknnn kaon kap+onkn ec+r nm n na6op nnc+pyknnn. Honap nrnonne+ nnc+pyknnn
n penen+e, a komnrm+ep nrnonne+ nnc+pyknnn n qynknnn.
Penen+r Be++n Kpokep Hanncanr na anrnnnckom srke. B nepnon nac+n knnrn
namn qynknnn 6yy+ nanncanr na srke nporpammnponann C. Onako nponeccop
komnrm+epa nonnmae+ +onrko nnc+pyknnn na mamnnnom koe. O+kya on 6epe+c?
Br nnme+e nporpammy na srke C (ko+oprn xopomo noxon+ nam), a
/'30!1*#'& npeo6pasye+ qynknnn namen nporpammr n mamnnnrn ko (ko+oprn
xopomo noxon+ nponeccopy). Komnnn+op .+o +oe nporpamma, ko+opym sanyckae+
Xcode, kora nr nanmae+e knonky Run. Komnnnnn n noc+poenne nporpammr - ono n
+o e; 6yy ncnonrsona+r .+n +epmnnr kak cnnonnmr.
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
17
Pnc. 2.5. Kap+onka c penen+om npnro+onnenn kypnnr

Hpn sanycke nporpammr o+komnnnnponannre qynknnn konnpym+c c ec+koro
ncka n nam+r, n nponeccop nrnonne+ qynknnm c nmenem 6'*+. dynknn 6'*+
o6rnno nrsrnae+ pyrne qynknnn. Ckaem, n namem npnmepe qynknn main nrsrnae+
qynknnm A(*+&B. (O +om, kak qynknnn nsanmoenc+nym+ pyr c pyrom, 6ye+
pacckasano n rnane 5.)
He oc+anannnnan+ecr
Bepo+no, k .+omy npemenn nr ye ncnr+ann nemano pasonaponannn: npo6nemr
c yc+anonkon, onena+kn, mnoec+no nesnakomrx +epmnnon. A moe+, nr noo6me ne
nonnmae+e, n+o secr nponcxon+. 3+o nce a6conm+no nopmanrno.
Moemy crny O++o mec+r ne+. O++o nync+nye+ ce6 c6n+rm c +onky no neckonrko
pas n enr. On noc+onno nr+ae+c ycnon+r snann, ko+opre ne yknarnam+c n ero
cymec+nymmne npec+annenn. Bamema+enrc+no nac+ynae+ +ak nac+o, n+o ero .+o non+n
ne 6ecnokon+. On nnkora ne nannnae+ saymrna+rc: Honemy nce +ak cnono?
Moe+, mne nynme o+non+r .+y knnry
C+anoncr c+apme, mr nce pee nonaaem n +ynnk - n ne no+omy. n+o mr nce
snaem, a no+omy, n+o mr cknonnr yxon+r o+ nemen, ko+opre nac osaannnam+.
Hanpnmep, nn+a+r knnry no nc+opnn npn+no, no+omy n+o mr naxonm n nen kyconkn
snannn, ko+opre moem nerko pasnon+r no cnonm ymc+nennrm nonkam. 3+o npoc+re
snann.
Hsynenne nonoro srka - npnmep +pynoro snann. Br snae+e, n+o mnnnnonr
nmen 6es ncknx npo6nem ronop+ na .+om srke, onako n namnx yc+ax on kae+c
nenepo+no c+pannrm n neyknmnm. H kora pyrne nr+am+c saronopn+r c namn, mr
nac+o +epem ap penn.
Hsynenne nporpammnponann +oe o+nocn+c k +pynomy snannm. Bpem o+
npemenn nr 6ye+e nync+nona+r ce6 c6n+rm c +onky oco6enno na nepnrx nopax. 3+o
nopmanrno. Co6c+nenno, .+o ae san+no - cnonno nam cnona mec+r ne+.
He 6pocan+e knnry; o6emam. n+o nce +pynoc+n paccem+c k nocnenen c+pannne.
Fnaaa 2. Bama nepaan nporpaHHa
________________________________________________________________________________________________
18
II
Kak pa6o+ae+
nporpammnponanne
19
c" G<9<A<++%<
& (&@%
Hpoonnm me+aqopy c penen+amn ns nperymen rnanr: nnora meq-nonap
sanon+ n kyxne nac+ennym ocky n sanncen. Hanpnmep, pacnakonrna nnenky, on
naxon+ naknenky c nanncrm: 14,2 qyn+a. Hpee nem nrknny+r ynakonky, on
nnme+ na ocke: L/*84& (nec) = 14,2. A no+om, nepe +em kak c+ann+r nnenky n
yxonky, on nrnncne+ npem npnro+onnenn (15 mnny+ + 15 mnny+ na karn qyn+
neca), o6pamacr k sanncm na ocke.
Pnc. 3.1. annre, sanncannre na ocke
Bo npem nrnonnenn nporpamme nac+o +pe6ye+c mec+o n xpanenn annrx,
ko+opre 6yy+ ncnonrsona+rc nosnee Mec+o n ko+opom mono pasmec+n+r onn kycok
annrx, nasrnae+c 0$&$3$""'4. Y kaon nepemennon ec+r nm (nanpnmep,
fggh*+8i*6j (npem npnro+onnenn)) n #!0 (ckaem, nncno). Kpome +oro, no npem
nrnonnenn nporpammr nepemenna npnnnmae+
neko+opoe
snanenne (nanpnmep, 228.0).
Tnnm
Hpn cosannn nonon nepemennon n nporpamme neo6xonmo '.;*(!#9 ee +nn n
nm. Hpnmep o6rnnenn nepemennon:
B-#'& L/*84&H
Hepemenna o6rnne+c c +nnom B-#'& n nmenem L/*84&. Ha .+o+ momen+
nepemenna eme ne nmee+ snanenn.
Fnana 3. Hepeueuume u 1unm
________________________________________________________________________________________________
20
B C +nn kaon nepemennon n nporpamme onen 6r+r o6rnnen npn ee
cosannn. n .+oro ec+r ne npnnnnr:

Hnqopmann o +nne nosnone+ komnnn+opy nponepn+r namy pa6o+y n coo6mn+r


o nosmonrx omn6kax nnn npo6nemax, Hpenononm, nr cosann nepemennym
+nna, npenasnanennoro n xpanenn +ekc+a. Ecnn nr nonr+ae+ecr nrnncnn+r
norapnqm nporpammr, komnnn+op ckae+ nam n+o-nn6yr npoe: n .+on
nepemennon nrnncnenne norapnqma ne nmee+ cmrcna .

Tnn coo6mae+ komnnn+opy, ckonrko nam+n (n 6an+ax) cneye+ nrenn+r n


xpanenn nepemennon.
Hne npnneena kpa+ka cnoka nan6onee pacnpoc+panennrx +nnon. Bce onn
6yy+ nopo6no paccmo+penr
short, int, long Bce +pn +nna ncnonrsym+c n xpanenn nenrx nncen. Tnn short
o6rnno na neckonrko 6an+on kopone long, a +nn int naxon+c re-+o
nocepenne. B nepemennon +nna long mono coxpann+r namnoro
6onrmee nncno, nem n short
oat,

double Tnn oat npenasnanen n xpanenn nemec+nennrx nncen-+o ec+r
nncen, ko+opre mory+ nme+r po6nym nac+r. B nam+n annre oat
npec+annm+c nym nncnamn: man+nccon n .kcnonen+on.
Hanpnmep, nncno 346,2 xpann+c n nne 3,462 x 10. Tnn double
ncnonrsye+c n xpanenn nncen nonnon +onnoc+n; kak npannno, n
nnx nrene+c 6onrme paspon n xpanenn man+nccr n
noepnnam+c 6onrmne snanenn .kcnonen+r
char Ono6an+onoe nenoe nncno, ko+opoe o6rnno nn+epnpe+npye+c kak
cnmnon - nanpnmep, 6ykna 'a'
ykasa+enn B ykasa+ene xpann+c apec nam+n. Ykasa+enr o6rnne+c npn
nomomn cnmnona snesonka(*). Hanpnmep, nepemenna,
o6rnnenna kak int *, moe+ ncnonrsona+rc n xpanenn apeca
nam+n, no ko+opomy xpann+c nepemenna +nna int. Camo nncno n
ykasa+ene ne xpann+c, no sna apec int, mono nerko nepen+n k
snanennm. Ykasa+enn npesnrnanno nonesnr, n mr nenpemenno
paccmo+pnm nx nopo6nee... Hamnoro nopo6nee
struct C+pyk+ypnrn +nn (nnn npome - c+pyk+ypa) cknarnae+c ns pyrnx
+nnon. Br moe+e cosana+r co6c+nennre onpeenenn struct.
Hpec+anr+e, n+o nam nyen +nn GeoLocation, npec+annmmnn
+onky semnon nonepxnoc+n, n n nem onnr xpann+rc na nncna:
mnpo+a n onro+a. B +akom cnynae nr onpeene+e c+pyk+ypy

Fnana 3. Hepeueuume u 1unm
________________________________________________________________________________________________
21
Karn nporpammnc+ C noc+onno ncnonrsye+ .+n +nnr n cnoen noncenennon
pa6o+e. Br ne nonepn+e, kakne cnonre nen mory+ 6r+r nrpaenr .+nmn n+rm
npoc+rmn +nnamn.
Hporpauua c nepeuennmun
Bosnpamaemc n Xcode: nopa cosa+r cneymmnn npoek+. n nanana sakpon+e npoek+
!"##$%&'(&, n+o6r nonrn ko cnynanno ne 6rn nneen n c+apom npoek+e.
Tenepr cosan+e nonrn npoek+ (File ! New ! New Project...).
Hann+e n nannra+ope npoek+a qann main.c n o+kpon+e ero. O+peak+npyn+e
main.c +ak, n+o6r ero coepnmoe nrrneno cneymmnm o6pasom:
)*+,-.$/ 2&$*#34
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
FF d:eE>Z<+&< @<9<A<++;f gO14hR3g (&@) KiSP3
KiSP3 O14hR3W
FF -;[9)+<+&< \&BZ) > @<9<A<++;f
O14hR3 j !k"CW
FF $%>;l &+m;9A)n&& lZE @;Z]_;>)(<ZE
HI4J3KLMoR1 3aIp1q O14hRQ rK"UJM` O14hR3VW
FF d:eE>Z<+&< l9?,;f @<9<A<++; (&@) KiSP3
KiSP3 2SSp4Jho4T1W

FF $%\&BZ<+&< >9<A<+& @9&,;(;>Z<+&E
FF $ l)++;A BZ?\)< B&A>;Z` gXg ;_+;\)<( ?A+;=&(] +)
2SSp4Jho4T1 j !s"b t !s"b X O14hR3W

FF $%>;l &+m;9A)n&& lZE @;Z]_;>)(<ZE
HI4J3KLM7SSp 43 KSI rK T4Ja31Q"UJM` 2SSp4Jho4T1VW
FF uB@<D+;< _)><9D<+&< m?+*n&&
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. hn6o menknn+e na knonke Run n nenon
nepxnen nac+n okna Xcode, nn6o nocnonrsyn+ecr kom6nnannen knannm Command+R.
Ba+em menknn+e na knonke
ptg999
A program wth varabes
21
#1hcude <s1d1o.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
// ec1are 1he varab1e ca11ed `Wegh1` o1 1ype 11oa1
11oa1 Wegh1
// Pu1 a number n 1ha1 varab1e
Wegh1 = 14.2
// Log 1 1o 1he user
prn11{"The 1urkey Weghs 1.\n", Wegh1)
// ec1are ano1her varab1e o1 1ype 11oa1
11oa1 cookngTme
// Ca1cu1a1e 1he cookng 1me and s1ore 1 n 1he varab1e
// In 1hs case, `*` means `mu11p1ed by`
cookngTme = 15.6 + 15.6 * Wegh1
// Log 1ha1 1o 1he user
prn11{"Cook 1 1or 1 mnu1es.\n", cookngTme)
// End 1hs 1unc1on and ndca1e success
Je1uJh 0,
}
Build and run the program. You can either click the Run button at the top left of the Xcode window or
use the keyboard shortcut Command-R. Then click the button to get to the log navigator. Select the
item at the top labeled Debug Turkey to show your output. It should look like this:
The 1uJkey we1ghs 14.200000.
Cook 11 1oJ 228.000000 m1hu1es.
Now click the button to return to the project navigator. Then select ma1h.c so that you can see your
code again. Let`s review what you`ve done here.
In your line of code that looks like this:
1oa1 we1gh1,
we say that you are 'declaring the variable we1gh1 to be of type 1oa1.
In the next line, your variable gets a value:
we1gh1 = 14.2,
You are copying data into that variable. We say that you are 'assigning a value of 14.2 to that variable.
In modern C, you can declare a variable and assign it an initial value in one line, like this:
1oa1 we1gh1 = 14.2,
Here is another assignment:
cook1hgT1me = 15.0 + 15.0 * we1gh1,
The stuff on the right-hand side of the = is an . An expression is something that gets
evaluated and results in some value. Actually, every assignment has an expression on the right-hand
side of the =.
, n+o6r o+kpr+r nannra+op ypnana. Br6epn+e o6rek+ c
me+kon O/M.8 Z.(h/_, n+o6r nepen+n k pesynr+a+y pa6o+r nporpammr.
On onen nrrne+r +ak:
Z4/ &.(h/_ L/*842 X]3VIIIII3
^##h *& B#( VVW3IIIIII 6*+.&/23
Fnana 3. Hepeueuume u 1unm
________________________________________________________________________________________________
22
Tenepr menknn+e na knonke
ptg999
A program wth varabes
21
#1hcude <s1d1o.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
// ec1are 1he varab1e ca11ed `Wegh1` o1 1ype 11oa1
11oa1 Wegh1
// Pu1 a number n 1ha1 varab1e
Wegh1 = 14.2
// Log 1 1o 1he user
prn11{"The 1urkey Weghs 1.\n", Wegh1)
// ec1are ano1her varab1e o1 1ype 11oa1
11oa1 cookngTme
// Ca1cu1a1e 1he cookng 1me and s1ore 1 n 1he varab1e
// In 1hs case, `*` means `mu11p1ed by`
cookngTme = 15.6 + 15.6 * Wegh1
// Log 1ha1 1o 1he user
prn11{"Cook 1 1or 1 mnu1es.\n", cookngTme)
// End 1hs 1unc1on and ndca1e success
Je1uJh 0,
}
Build and run the program. You can either click the Run button at the top left of the Xcode window or
use the keyboard shortcut Command-R. Then click the button to get to the log navigator. Select the
item at the top labeled Debug Turkey to show your output. It should look like this:
The 1uJkey we1ghs 14.200000.
Cook 11 1oJ 228.000000 m1hu1es.
Now click the button to return to the project navigator. Then select ma1h.c so that you can see your
code again. Let`s review what you`ve done here.
In your line of code that looks like this:
1oa1 we1gh1,
we say that you are 'declaring the variable we1gh1 to be of type 1oa1.
In the next line, your variable gets a value:
we1gh1 = 14.2,
You are copying data into that variable. We say that you are 'assigning a value of 14.2 to that variable.
In modern C, you can declare a variable and assign it an initial value in one line, like this:
1oa1 we1gh1 = 14.2,
Here is another assignment:
cook1hgT1me = 15.0 + 15.0 * we1gh1,
The stuff on the right-hand side of the = is an . An expression is something that gets
evaluated and results in some value. Actually, every assignment has an expression on the right-hand
side of the =.
, n+o6r nepny+rc k nannra+opy npoek+a.
Br6epn+e qann main.c - n okne Xcode cnona nonne+c nporpammnrn ko. anan+e
nocmo+pnm, n+o e mr cenann.
B c+poke:
B-#'& L/*84&H
mr npnkasrnaem o6rnn+r nepemennym L/*84& c +nnom B-#'&.
B cneymmen c+poke nepemenna nonynae+ snanenne:
L/*84& k X]3VH
3+a komana konnpye+ annre n nepemennym. Hporpammnc+r ronop+, n+o
nepemennon npncnannae+c snanenne 14.2.
B conpemennom srke C mono conmec+n+r o6rnnenne nepemennon n
npncnannanne nananrnoro snanenn n onon c+poke:
B-#'& L/*84& k X]3VH
A no+ pyra komana npncnannann:
,##h*+8Z*6/ k XT3I l XT3I : L/*84&H
Cnpana o+ snaka = sanncano (<&)2$"!$ - nen+o +akoe, n+o mono nrnncnn+r n
nonynn+r pesynr+a+ (snanenne). Boo6me ronop, n kaon komane npncnannann cnpana
o+ snaka = naxon+c nrpaenne.
Hanpnmep, n c+poke:
L/*84& k X]3VH
nrpaennem nne+c nncno 14.2.
Hepemennre ncnonrsym+c no ncex nporpammax. B .+on rnane mr nnmr n camrx
o6mnx nep+ax nosnakomnnncr c nepemennrmn.
Bam eme npec+on+ ysna+r mnoro nonoro o +om, kak pa6o+am+ nepemennre . n kak
nx ncnonrsona+r n nporpammax.
npawnenne
Cosan+e nonym nporpammy komannon c+pokn C c nmenem ZL#`-#'&2. B
qynknnn 6'*+() o6rnn+e ne nepemennre +nna B-#'& n npncnon+e kaon nncno c
po6non nac+rm - nanpnmep, 3.14 nnn 42.0. O6rnn+e pyrym nepemennym +nna $g.M-/
n npncnon+e en cymmy nyx nepemennrx +nna B-#'&. Brnen+e pesynr+a+ npn nomomn
A(*+&B(). Ba npnmepamn cnn+akcnca o6paman+ecr k koy, npnneennomy n .+on rnane.
Fnana 3. Hepeueuume u 1unm
________________________________________________________________________________________________
23
k" NKF1iQ1
Ona ns nanenmnx nen nporpammnponann - nrnonnenne pasnrx enc+nnn n
sanncnmoc+n o+ o6c+o+enrc+n. Bce nn o6sa+enrnre non sanonnenr na qopme sakasa?
Ecnn nce, +o pas6noknpona+r knonky %.M6*&. Oc+anncr nn y nrpoka nencnonrsonannre
nsnn? Ecnn oc+anncr, npoonn+r nrpy, a ecnn ne+ - o+o6pasn+r kap+nnky c mornnkon
n nocnponsnec+n nenanrnym mysrky.
Takoe noneenne peannsye+c c ncnonrsonannem konc+pyknnn *B @/-2/, ko+opa
nmee+ cneymmnn cnn+akcnc:
*B 7,#+$*&*#+'-> ?
@@ mngn ogp qrsgtuvjnfv9 jftw xftgqwj wfnwuug 7&(./>
J /-2/ ?
@@ mngn ogp qrsgtuvjnfv9 jftw xftgqwj tgyug 7B'-2/>
J
B .+on rnane mr ne 6yem cosana+r npoek+. Tma+enrno npocmo+pn+e ko
npnmepon, o6pama nnnmanne na +o, n+o nr ysnann n nyx nperymnx rnanax.
Hpnmep koa c ncnonrsonannem *B @/-2/:
B-#'& &(.,hE/*84& k S]TRS3WH
@@ zg{g| uj s{jqr}ju~
*B 7&(.,hE/*84& 1 ]IIII3I> ?
A(*+&B7CK& *2 ' -*84& &(.,hG+C>H
J /-2/ ?
A(*+&B7CK& *2 ' 4/';_ &(.,hG+C>H
J
Ecnn ceknn /-2/ ne nyna, ee mono ne ykasrna+r:
B-#'& &(.,hE/*84& k S]TRS3WH
@@ zg{g| uj s{jqr}ju~
*B 7&(.,hE/*84& 1 ]IIII3I> ?
A(*+&B7CK& *2 ' -*84& &(.,hG+C>H
J
Pesynr+a+om ycnonnoro nrpaenn ncera nne+c ono ns nyx snanennn: &(./
(nc+nna) nnn B'-2/ (nor). B srke C 6rno pemeno, n+o B'-2/ 6ye+ npec+ann+rc
0, a nm6re ne nynenre snanenn 6yy+ cnn+a+rc pannrmn &(./,
B npnneennom npnmepe c o6enx c+opon o+ onepa+opa < c+o+ nncna. Ecnn nncno
cnena menrme nncna cnpana, +o pesynr+a+ nrpaenn panen 1 (onenr pacnpoc+panennrn
cnoco6 npec+annenn nc+nnnoc+n). Ecnn nncno cnena 6onrme nn6o panno nncny cnpana,
+o pesynr+a+ nrpaenn panen O(ennc+nennrn cnoco6 npec+annenn nonoc+n)
Fnana 4. if/else
________________________________________________________________________________________________
24
Hoo6nre onepa+opr nac+o nc+penam+c n ycnonnrx nrpaennx. B +a6n. 4.1
npec+annenr ocnonnre onepa+opr, ncnonrsyemre npn cpannennn nncen (n pyrnx
+nnon, ko+opre nn+epnpe+npym+c komnrm+epom kak nncna).
Ta6nnna 4.1. Onepa+opr cpannenn
< Hncno cnena menrme nncna cnpana?
> Hncno cnena 6onrme nncna cnpana?
<= Hncno cnena menrme nn6o panno nncny cnpana?
>= Hncno cnena 6onrme nn6o panno nncny cnpana? == Hncna pannr?
!= Hncna ne pannr?
Onepa+op == sacnynnae+ oco6oro ynomnnann: n nporpammnponann n onepa+op
== ncnonrsye+c n nponepkn panenc+na. Kak nr ye snae+e, onnonnrn snak
panenc+na = ncnonrsye+c n npncnannann snanennn. Onenr, onenr mnorne omn6kn
nponcxo+ ns-sa +oro, n+o nporpammnc+r nno+ = nmec+o ==. Tak n+o nynme
o+nrkan+e yma+r o = kak o snake panenc+na o+nrne .+o onepa+op npncnannann.
B neko+oprx ycnonnrx nrpaennx neo6xonmr nornneckne onepa+opr.
Hpec+anr+e, n+o nam nyno nponepn+r, nxon+ nn nncno n neko+oprn nanason -
ckaem, 6onrme 0 n menrme 40 000? n onpeenenn nanasonon mono ncnonrsona+r
nornnecknn onepa+op !PO (&&):
*B 77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>> ?
A(*+&B7CZ(.,h L/*84& *2 L*&4*+ -/8'- ('+8/3G+C>H
J
Tpn nornnecknx onepa+opa npnneenr n +a6n. 4.2,

Ta6nnna 4.2. hornneckne onepa+opr
&&
hornnecknn onepa+op AND -- true +onrko n +om cnynae, ecnn o6a snanenn
pannr true
||
hornnecknn onepa+op OR -- false +onrko n +om cnynae, ecnn o6a snanenn
pannr false
!
hornnecknn onepa+op NOT -- true npenpamae+c false, n nao6opo+
Ecnn y nac ye nmee+c onr+ nporpammnponann na pyrom srke, yn+n+e, n+o
nornneckoro onepa+opa ncknmnammero N n Objective-C ne+, no.+omy secr on ne
ynomnnae+c.
hornnecknn onepa+op PZ (!) nrnncne+ o+pnnanne nrpaenn, npnneennoro n
cko6kax cnpana:
@@ ujuwj qgpwn q {{j}juur pwsggu~
*B 7F77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>>> ?
Fnana 4. if/else
________________________________________________________________________________________________
25
A(*+&B7CZ(.,h L/*84& *2 +#& L*&4*+ -/8'- ('+8/3G+C>H
J
ornueckne nepeuennme
Kak nnn+e, nrpaenn ononrno 6rc+po c+anon+c nnnnrmn n cnonrmn. Hnora
6rnae+ yo6no nomec+n+r snanenne nrpaenn n nepemennym c noxomnm nmenem:
ac *2P#&c/8'- k F77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>>H
*B 7*2P#&c/8'-> ?
A(*+&B7CZ(.,h L/*84& *2 +#& L*&4*+ -/8'- ('+8/3G+C>H
J
Hepemenna, ko+opa moe+ npnnnma+r snanenn &(./ n B'-2/, nasrnae+c
1'%!=$+/'4 0$&$3$""'4. Tpannnonno nporpammnc+r C ncera xpannna nornneckne
snanenn n nepemennrx +nna *+&. Hporpammnc+r Objective-C o6rnno ncnonrsym+ n
nornnecknx nepemennrx +nna ac, no.+omy n npnneennom qparmen+e 6rn nr6pan
nmenno on. (Bnponem c - ncero nnmr cnnonnm n nenoro +nna.) H+o6r
ncnonrsona+r +nn ac n qynknnn C, neo6xonmo nknmnn+r coo+ne+c+nymmnn
sarononok:
)*+,-.$/ 1#M,@#M,345
He6onrmoe samenanne no nonoy cnn+akcnca: ecnn ko, cneymmnn sa ycnonnrm
nrpaennem, coc+on+ ncero ns onon komanr, +o qnrypnre cko6kn neo6sa+enrnr.
Taknm o6pasom, cneymmnn qparmen+ .knnnanen+en nperymemy:
ac *2P#&c/8'- k F 77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>>H
*B 7*2P#&c/8'->
A(*+&B 7CZ(.,h L/*84& *2 +#& L*&4*+ -/8'- ('+8/3 G+C> H
Ho ecnn ko coc+on+ ne ns onon, a ns neckonrknx koman, qnrypnre cko6kn
o6sa+enrnr
ac *2P#&c/8'- k F77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>>H
*B 7*2P#&c/8'-> ?
A(*+&B 7CZ(.,h L/*84& *2 +#& L*&4*+ -/8'- ('+8/3 G+C>H
A(*+&B7CK6A#.+$ &(.,h3G+C>H
J
Honemy? Hpec+anr+e, n+o mr y6pann qnrypnre cko6kn
ac *2P#&c/8'- k F 77&(.,hE/*84& 5 I3I> 7&(.,hE/*84& 1 ]IIII3I>>H
*B 7*2P#&c/8'->
A(*+&B7 CZ(.,h L/*84& *2 +#& L*&4*+ -/8'- ('+8/3 G+C>H
A(*+&B7 CK6A#.+$ &(.,h3 G+C>H
Fnana 4. if/else
________________________________________________________________________________________________
26
B .+om cnynae komana n+opa komana c nrsonom A(*+&B 6ye+ nrnonn+rc
ncera, nesanncnmo o+ pesynr+a+a nponepkn. Kora komnnn+op ne o6napynnae+
qnrypnym cko6ky nocne ycnonn, on cnn+ae+, n+o n komany *B nxon+ +onrko
cneymma komana. Cneona+enrno, n+opa komana A(*+&B 6ye+ nrnonn+rc
ncera. (A kak e o+c+yn? O+c+ynr onenr nonesnr n nac, nmen, nn+ammnx ko; n
komnnn+opa onn ne snana+ nnnero.)
else if
A ecnn nosmonoc+en 6onrme nyx? Mono nponepn+r nx ony sa onon npn nomomn
konc+pyknnn /-2/ *B. Hpec+anr+e, n+o rpysonnk no necy npnnanen+ k onon ns +pex
ka+eropnn: nenecomrn(oating), nerknn(light) nnn +enrn(heavy).
*B 7&(.,hE/*84& 1k I> ? A(*+&B7C! B-#'&*+8 &(.,hG+C>H
J /-2/ *B 7&(.,hE/*84& 1 ]IIII3I> ? A(*+&B7C! -*84& &(.,hG+C>H
J/-2/ ?
A(*+&B7C! 4/';_ &(.,hG+C>H
J
Ycnonnn /-2/ *B moe+ 6r+r ckonrko yrono. Onn 6yy+ nponep+rc n nopke
cneonann o +ex nop, noka pesynr+a+ onoro ns ycnonnn ne okae+c pannrm &(./.
Hopok cneonann naen. O6sa+enrno pacnonon+e ycnonn +ak, n+o6r ns6ea+r
nonrx nonon+enrnrx cpa6a+rnannn. Hanpnmep, ecnn nomen+r, mec+amn nepnre na
ycnonn n npnneennom npnmepe, nenecomre rpysonnkn nnkora ne 6yy+ nanenr,
no+omy n+o onn 6yy+ nepexna+rna+rc ycnonnmn n nerknx rpysonnkon.
Banepmamma ceknn /-2/ ne o6sa+enrna, no ona npnron+c, ecnn nr xo+n+e
o6pa6o+a+r nce snanenn ne noxomne nn no ono ns nepenncnennrx ycnonnn.
nn n6osnavennmx: ycnonnmn (vepnapnmn) onepavop
Konc+pyknnn *B@/-2/ ononrno nac+o ncnonrsym+c n npncnannann snanenn
nepemennrm. Bosrmem n npnmepa cneymmnn ko:
*+& 6*+.&/2b/(b#.+$H *B 7*2a#+/-/22>
6*+.&/2b/(b#.+$ k XTH
/-2/
6*+.&/2b/(b#.+$ k VIH
Karn pas, kora snanenne npncnannae+c nepemennon n sanncnmoc+n o+
neko+oporo ycnonn, snan+e, n+o y nac nmee+c nosmonrn kanna+ n npnmenenn
6+1'("'%' '0$&)#'&) ~. (ero +ake nac+o nasrnam+ #$&")&"<3 '0$&)#'&'3).
*+& 6*+.&/2b/(b#.+$ k *2a#+/-/22 ~ XT \ VIH
Fnana 4. if/else
________________________________________________________________________________________________
27
3+a c+poka .knnnanen+na ncemy nperymemy qparmen+y. Bmec+o *B n /-2/
oc+a+onno nannca+r ennc+nennym komany npncnannann. Hac+r nepe ~ -
nponepemoe: ycnonne, a snanenn nocne ~ - anr+epna+nnr n nc+nnnoro n nonoro
pesynr+a+a nponepkn.
Ecnn .+a sanncr kae+c nam c+pannon, nnn+o ne memae+ npoona+r
nonrsona+rc *B n /-2/. Honaram, n+o co npemenem nr onenn+e +epnapnrn onepa+op
kak komnak+nym n +onnym sanncr n ycnonnoro npncnannann snanennn. H n+o eme
nanee - nr ynnn+e. kak on ncnonrsye+c pyrnmn nporpammnc+amn, n cmoe+e non+r
ynnennoe!
npawnenne
Hmee+c cneymmnn qparmen+ koa:
*+& * kVIH
*+& * kVTH
*+& h k 7 * 5 > ~ XI \ TH
*B 7T 1 Uh>?@@ sj{qgj qr{yjuwj
{(*+&B 7CZ4/ B*(2& j{(/22*#+ *2 &(./3 C> H
J /-2/ *B 7 5 * > ? @@ qng{gj qr{yjuwj
{(*+&B 7CZ4/ 2/,#+$ j{(/22*#+ *2 &(./3 C> H
J/-2/ ?
A(*+&B7CP/*&4/( /dA(/22*#+ *2 &(./3C>H
J
H+o 6ye+ nrneeno na konconr?
Fnana 4. if/else
________________________________________________________________________________________________
28
s" v?+*n&&
B rnane 3 o6rcnnn, n+o +akoe +akoe nepemenna: nm, cnsannoe c 6nokom annrx.
dynknn npec+anne+ co6on nm, cnsannoe c 6nokom koa. dynknnn mono nepea+r
nnqopmannm. dynknnm mono nrnonnn+r. Ee mono nrnonnn+r. Ee mono sac+ann+r
nepny+r nam pesynr+a+
dynknn nmem+ ocnonononarammee snanenne n nporpammnponannn, no.+omy .+a
rnana nonynnnocr nnnnon - +pn nonrx npoek+a, nona nporpamma, mnoro nonrx nen.
Hannem c ynpanenn, ko+opoe narnno nokasrnae+, n nero nynr qynknnn.
Kor(a ncnonsonav qynknn?
Hpenononm, nr nnmn+e nporpammy, ko+opa nospanne+ c+yen+on c npoxoennem
yne6noro kypca. Hpee nem yma+r o +om, kak nponn+a+r cnncok c+yen+on ns 6asr
annrx nnn nanena+a+r cep+nqnka+r na 6nankax nnnomon, nr xo+n+e no
.kcnepnmen+npona+r c coo6mennem, ko+opoe onno nena+a+rc na cep+nqnka+ax.
Cosan+e n .kcnepnmen+on nonrn npoek+, nporpammy komannon c+pokn C c
nmenem ^-'22^/(&*B*,'&/2
Hepna ne nrrnn+ npnmepno +ak:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7C'(h 4'2 $#+/ '2 6.,4 ^#,#' b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_
G+C>H
A(*+&B7Ca# 4'2 $#+/ '2 6.,4 M/,&*;/U^ b(#8('66*+8 '2 K ,#.-$ B*& *+&# V
$'_2G+C>H
A(*+&B7C*h/ 4'2 $#+/ '2 6.,4 b_&4#+ b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_2
G+C>H
A(*+&B7CZ/$ 4'2 $#+/ '2 6.,4 *% b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_2
G+C>H
(/&.(+ IH
J
Bac 6ecnokon+ neo6xonmoc+r n6nna+r nce .+n coo6menn? Onn paspaam+, nac
cnonm onoo6pasnem? Hospannm, y nac saa+kn o+nnnnoro nporpammnc+a. Kora nam
npnxon+c nomnory pas ena+r nen+o onenr noxoee (n annom cnynae - na6npa+r
coo6menn n komane A(*+&B), noyman+e, nenrs nn nocnonrsona+rc qynknnen n
oc+nenn +oro e pesynr+a+a.
Fnana 5. Oyukquu
________________________________________________________________________________________________
29
Kak nanncav n ncnonsonav qynknn?
H+ak, nam nyna qynknn; oc+ae+c non+r, kak ee nannca+r. O+kpon+e qann main.c n
npoek+e ^-'22^/(&*B*,'&/2 n nnen+e nonym qynknnm nepe qynknnen 6'*+. 3+on
qynknnn 6ye+ npncnoeno nm ,#+8('&.-'&/%&.$/+&.
)*+,-.$/ 12&$*#345
5S4w 2SJhIP3aiP31x3aw1J3L2RPI XQ3aw1J3` 2RPI X2SaIQ1` 4J3 JaTyPqQV
z
HI4J3KLMrQ RPQ wSJ1 PQ Ta2R rQ {IShIPTT4Jh PQ N 2Saiw K43 4J3S rw wPqQ"UJM`
Q3aw1J3` 2SaIQ1` JaTyPqQVW
|
(Br cnpamnnae+e, n+o osnanam+ .+n saraonnre 2 n $? Ho+epnn+e; mr
noronopnm o nnx n cneymmen rnane.)
Tenepr o+peak+npyn+e qynknnm 6'*+, n+o6r n nen ncnonrsonanacr nona
qynknn:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
2SJhIP3aiP31x3aw1J3LM}PIpM` M7S2SPM` sVW
2SJhIP3aiP31x3aw1J3LM~SM` M./012345167M` CVW
2SJhIP3aiP31x3aw1J3LM}4p1M` M{q3RSJM` sVW
2SJhIP3aiP31x3aw1J3LMo1wM` M4.xM` sVW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Bepo+no, nr nonynn+e npeynpeenne,
nomenennoe nocknnna+enrnrm snakom n en+om +peyronrnnke. Hpeynpeenn n
Xcode ne memam+ nrnonnennm namen nporpammr; onn ncero nnmr npnnnekam+ name
nnnmanne k nosmonon npo6neme. Tekc+ npeynpeenn nrnon+c cnpana o+ koa. B
.+om npeynpeennn ronopn+c n+o-+o npoe He+ npemec+nymmero npo+o+nna n
qynknnn 'congratulateStudent'. Hoka ne o6paman+e nnnmann, mr nepnemc k
npeynpeennm n konne pasena.
Hann+e nrno nporpammr n nannra+ope ypnana, On onen 6r+r nen+nnen
+omy, n+o nr 6r ynnenn, ecnn 6r kaa c+poka 6rna nneena npynnym.
'(h 4'2 $#+/ '2 6.,4 ^#,#' b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_23
a# 4'2 $#+/ '2 6.,4 M/,&*;/U^ b(#8('66*+8 '2 K ,#.-$ B*& *+&# V $'_23
*h/ 4'2 $#+/ '2 6.,4 b_&4#+ b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_23
Z/$ 4'2 $#+/ '2 6.,4 *% b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_23
Hoyman+e, n+o secr nponsomno. Mr same+nnn non+opmmymc
sakonomepnoc+r. Mr nsnneknn nce o6mne xapak+epnc+nkn npo6nemr (non+opmmnnc
+ekc+) n nepemec+nnn nx n o+enrnym qynknnm. Oc+anocr paso6pa+rc c pasnnnnmn
(nm c+yen+a, nasnanne kypca, konnnec+no nen). n nnx n qynknnm 6rnn nknmnenr
+pn napame+pa. anan+e eme pas nocmo+pnm na +y c+poky, n ko+opon npncnannae+c nm
qynknnn.
Fnana 5. Oyukquu
________________________________________________________________________________________________
30
;#*$ ,#+8('&.-'&/%&.$/+&7,4'( :2&.$/+&9 ,4'( :,#.(2/9 *+& +.6O'_2>
Karn napame+p coc+on+ ns nyx nac+en: +nna annrx n nmenn napame+pa.
Hapame+pr pasenm+c san+rmn n nepenncnm+c n kpyrnrx cko6kax cnpana o+
nmenn qynknnn.
A n+o osnanae+ ;#*$ cnena o+ nmenn qynknnn? 3+o +nn nnqopmannn,
nosnpamaemon qynknnen. Ecnn qynknn ne nosnpamae+ nnnero, ncnonrsyn+e knmnenoe
cnono ;#*$. Mr noronopnm o nosnpamaemrx snanennx qynknnn nosnee n .+on rnane.
Hona qynknn ncnonrsonanacr (nnn (<-<()1)+9) n qynknnn 6'*+. Hpn nrsone
,#+8('&.-'&/%&.$/+& mr nepeaem .+on qynknnn snanenn, ko+opre nasrnam+c
aprymen+amn. Bnanenne aprymen+a npncnannae+c coo+ne+c+nymmemy napame+py. Hm
napame+pa ncnonrsye+c nny+pn qynknnn kak nepemenna, coepama nepeannoe
snanenne.
Paccmo+pnm konkpe+nrn npnmep. B nepnom nrsone ,#+8('&.-'&/%&.$/+&
nepeam+c +pn aprymen+a: "Mark", "Cocoa" n 5.
,#+8('&.-'&/%&.$/+&7C'(hC9 C^#,#'C9 T>H
Hoka sanmemc +pe+rnm aprymen+om. Hepeannoe qynknnn
,#+8('&.-'&/%&.$/+& snanenne 5 npncnannae+c +pe+remy napame+py (+.6O'_2).
Aprymen+r npncnannam+c napame+pam n nopke cneonann, npn .+om snanenn
onnr nme+r +o+ e (nnn onenr 6nnsknn) +nn. Y nac 5 - nenoe nncno, a napame+p
+.6O'_2 nmee+ +nn *+&... Hoxon+.
Tenepr, kora nr nanncann .+y yo6nym qynknnm, ce mono 6ye+ ncnonrsona+r
n pyrnx nporpammax. Bnocn+r nsmenenn +oe 6ye+ npome - oc+a+onno nsmenn+r
qopmynnponky qpasr nny+pn qynknnn, H nsmenenn nemenenno nc+yn+ n cnny:
noncmy, re nrsrnae+c .+a qynknn.
Eme pas nocmo+pn+e na nepnym nepcnm ^-'22^/(&*B*,'&/2 c onoo6pasnrmn
komanamn. Banem mr ncnonrsonann qynknnm? H+o 6r ne na6npa+r nnmnne cnmnonr
na komnrm+epe? a, no .+o onpeenenno ne nce. He c+on+ sa6rna+r n o nponepke
omn6ok. Hem menrme nr na6npae+e , n nem 6onrme sa nac pa6o+ae+ komnrm+ep, +em
menrme nepo+noc+r onena+kn. Ecnn nr omn6e+ecr npn nnoe nmenn qynknnn. Xcode
npeynpen+ nac, no Xcode non+n ne nmee+ o6 omn6kax npn nnoe o6rnnoro +ekc+a.
Eme ono npenmymec+no qynknnn - nosmonoc+r non+opnoro ncnonrsonann.
Tenepr, kora nr nanncann .+y yo6nym qynknnm, ee mono 6ye+ ncnonrsona+r n
pyrnx nporpammax. Bnocn+r nsmenenn +oe 6ye+ npome - oc+a+onno nsmenn+r
qopmynnponky qpasr nny+pn qynknnn, n nsmenenn nemenenno nc+yn+ n cnny
noncmy, re nrsrnae+c .+a qynknn.
H nocnenee npenmymec+no dynknnn: ecnn n qynknnn npyr onymena omn6ka,
nr ncnpanne+e .+y ony qynknnm, n nce ce nrsonr npyr nannnam+ pa6o+a+r
npannnrno. Kopone ronop, pas6nenne koa na qynknnn ynpomae+ ero nonnmanne n
conponoenne.
Fnana 5. Oyukquu
________________________________________________________________________________________________
31
A +enepr nepnemc k npeynpeennm n koe. Ha npak+nke qynknn onenr nac+o
'.;*(1*$#+* n onom mec+e, a '0&$,$1*$#+* n pyrom. O6rnnenne qynknnn npoc+o
npeynpeae+ o +om, n+o re-+o anrme nmee+c qynknn c +aknm nmenem. B
onpeenennn qynknnn nr nepenncne+e +e enc+nn, ko+opre .+a qynknn onna
nrnonn+r. B namem ynpanennn qynknn o6rnnnacr n onpeennacr n onom mec+e.
Tak kak .+o ne+nnnnno, Xcode nrae+ npeynpeenne, ecnn qynknn ne 6rna
o6rnnena sapanee.
Pnc. 5.1. O+knmnenne npeynpeennn o npo+o+nnax qynknnn
Pa6o+a na npoek+amn, ko+opre mr 6yem cosana+r n .+on knnre, na
npeynpeenn mono npoc+o ne o6pama+r nnnmann. A ecnn xo+n+e, o+knmnn+e .+o
npeynpeenne - nr6epn+e o6rek+ ^-'22^/(&*B*,'&/2 n nepxnen nac+n nannra+opa
npoek+a. Ha nanenn peak+opa nr6epn+e pasen !-- na nknake a.*-$ %/&&*+82.
Hpokpy+n+e pasnre napnan+r nac+ponkn noc+poenn npnnoennn, nann+e c+poky
*22*+8 `.+,&*#+ b(#&#&_A/2 n nsmenn+e ee snanenne na P#.
Kak qynknnn pa6ovav (pyr c (pyrou
Hporpamma - .+o na6op qynknnn. Kora nr sanyckae+e nporpammy, .+n qynknnn
konnpym+c c ec+koro ncka n nam+r, nponeccop naxon+ qynknnm c nmenem 6'*+ n
nrnonne+ ee.
Bcnomnn+e, kak mr cpannnnann qynknnm c kap+onkon penen+a. Hannna
nrnonn+r penen+ Henena kypnna, mory o6napyn+r, n+o n+opa nnc+pyknn
+pe6ye+ Hpnro+onn+r nannponky; o +om, kak .+o cena+r, o6rcne+c na pyron
Fnana 5. Oyukquu
________________________________________________________________________________________________
32
kap+onke. Hporpammnc+ n +akom cnynae ckae+: dynknn "Henena kypnna" nrsrnae+
qynknnm "Hannponka".
Tonno +ak e n qynknn main moe+ nrsrna+r pyrne qynknnn. Hanpnmep,
qynknn main ns npoek+a ^-'22^/(&*B*,'&/2 nrsnana qynknnm
,#+8('&.-'&/%&.$/+&, ko+opa n cnom oneper nrsnana A(*+&B.
Bo npem npnro+onnenn nannponkn nr nepec+ae+e nrnonn+r penen+ c kap+onkn
Henena kypnna, a kora nannponka 6ye+ ro+ona - cnona nosnpamae+ecr k kap+onke
Henena kypnna n npoonae+e ee nrnonnenne. Tak e n qynknn main npekpamae+
nrnonn+rc n npnoc+anannnnae+c o +ex nop, noka ne 6ye+ nrnonnena nrsnanna
en qynknn.
H+o6r ynne+r, kak .+o nponcxon+, mr nocnonrsyemc qynknnen 2-//A,
ko+opa ne enae+ nnnero, a +onrko onae+ neko+opoe npem. Bknmnn+e n qynknnm
main nrson 2-//A.
Pnc 5.2. Kap+onkn c penen+amn
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
,#+8('&.-'&/%&.$/+&7C'(hC9 C^#,#'C9 T>H
Qi11HLCVW
,#+8('&.-'&/%&.$/+&7Ca#C9 CM/,&*;/U^C9 V>H
Qi11HLCVW
,#+8('&.-'&/%&.$/+&7C*h/C9 CbDb '+$ b#2&8(/%cC9 T>H
Qi11HLCVW
,#+8('&.-'&/%&.$/+&7CZ/$C9 C*%C9 T>H
(/&.(+ IH
J
Fnana 5. Oyukquu
________________________________________________________________________________________________
33
Hoc+pon+e n sanyc+n+e nporpammy. (Ha npeynpeenne o6 o+cy+c+nnn
o6rnnenn noka ne o6paman+e nnnmann.) Br ynnn+e, n+o nepe karm coo6mennem
cneye+ 2-cekynna naysa. 3+o nponcxon+ ns-sa +oro, n+o qynknn main nepec+ae+
nrnonn+rc o +oro momen+a, kak nrsnanna qynknn 2-jj{ sanepmn+ cnom pa6o+y.
O6pa+n+e nnnmanne: npn nrsone qynknnn ykasrnae+c nm n kpyrnre cko6kn n
aprymen+on. Coo+ne+c+nenno kora qynknn ynomnnae+c n +ekc+e, sa ee nmenem +oe
o6rnno cneye+ napa kpyrnrx cko6ok. C .+oro momen+a, ronop o qynknnn main, mr
6yem ncnonrsona+r sanncr 6*+().
Bam komnrm+ep ye coepn+ mnoro nc+poennrx qynknnn. Boo6me-+o .+o ne
concem +onno - no+ nam cama nac+oma npana: o +oro, kak na namem komnrm+epe
6rna yc+anonnena cnc+ema Mac OS X, on 6rn ne nem nnrm, kak oporoc+omnm
narpena+enem. B nac+noc+n, nmec+e c Mac OS X 6rnn yc+anonnenr qannr coepamne
na6op sapanee o+komnnnnponannrx qynknnn. 3+n na6opr qynknnn nasrnam+c
+#)",)&#"<3! .!.1!'#$/)3!; qynknnn 2-//A() n A(*+&B () nxo+ n .+n
c+anap+nre 6n6nno+ekn.
Bnanane qanna main.c naxon+c npek+nna nknmnenn qanna 2&$*#34. B nem
coepn+c o6rnnenne qynknnn A(*+&B(), no ko+opomy komnnn+op moe+ nponepn+r
npannnrnoc+r ncnonrsonann qynknnn. dynknn 2-//A() o6rnnena n qanne
2&$-*M34. Bknmnn+e n .+o+ qann, n+o 6r komnnn+op nepec+an anona+rc na
o+cy+c+nne o6rnnenn 2-//A()
)*+,-.$/ 12&$*#345
4J2iaw1 Q3wi4/"R
;#*$ ,#+8('&.-'&/%&.$/+&7,4'( :2&.$/+&9 ,4'( :,#.(2/9 *+& +.6O'_2>
?
333
C+anap+nre 6n6nno+ekn npnnoc+ nonnym nonrsy

B nnx co6panr 6onrmne qparmen+r koa, ko+oprn nam ne npne+c nnca+r, n


conponoa+r camoc+o+enrno. Coo+c+nenno, c nnm nr cmoe+e cosana+r namnoro
6onrmne n kanec+nennre nporpammr, nem 6es nnx.

C+anap+nre 6n6nno+ekn nomoram+ nrepa+r o6mnn c+nnr oqopmnenn n


noneenn mnornx nporpamm.
Hporpammnc+r npono+ mnoro npemenn sa nsynennem c+anap+nrx 6n6nno+ek
onepannonnrx cnc+em, n ko+oprx onn pa6o+am+. Kaa komnann, cosanma
onepannonnym cnc+emy, +ake cosae+ okymen+annm n npnnaraemrx k nen
c+anap+nrx 6n6nno+ek. O +om, kak npocma+pnna+r okymen+annm n Mac OS n iOS,
pacckasano n rnane 15,
Fnana 5. Oyukquu
________________________________________________________________________________________________
34
okannme nepeuennme, ka(pm n cvek
B kaon qynknnn mory+ onpeen+rc nokanrnre nepemennre. Tak nasrnam+c
nepemennre, o6rnnenre nny+pn qynknnn. Onn cymec+nym+ nny+pn +onrko no npem
nrnonnenn .+on qynknnn, n o6pama+rc k nnm mono +onrko n npeenax .+on
qynknnn. Hanpnmep, npec+anr+e. n+o mr nnmem qynknnm n nrnncnenn
npoonn+enrnoc+n npnro+onnenn nnenkn. ona moe+ nrrne+r +ak:
;#*$ 24#L^##hZ*6/`#(Z.(h/_7*+& A#.+$2>
?
*+& +/,/22'(_*+.&/2 k XT l XT : A#.+$2H
A(*+&B7C^##h B#( $ 6*+.&/23G+C9 +/,/22'(_*+.&/2>H
J
Becr +/,/22'(_*+.&/2 - nokanrna nepemenna. Ona nannnae+ cnoe
cymec+nonanne c nananom nrnonnenn 24#L^##hZ*6/`#(Z.(h/_() n yxon+ n ne6r+ne
nocne sanepmenn nrnonnenn .+on qynknnn. Hapame+p qynknnn A#.+$2 +oe
nne+c nokanrnon nepemennon. Hapame+p npec+anne+ co6on nokanrnym
nepemennym, nnnnnannsnponannym snanennem coo+ne+c+nymmero aprymen+a.
dynknn moe+ nme+r neckonrko nokanrnrx nepemennrx; nce onn xpan+c n
/),&$ .+on qynknnn. Hpec+anr+e ce6e kap n nne nac+ennon ockn, na ko+opon mono
nnca+r no npem pa6o+r qynknnn. Kora nrnonnenne qynknnn sanepmae+c, c ockn
nce c+npae+c.
onyc+nm, mr pa6o+aem c penen+om nenenon kypnnr. B namen kyxne y kaoro
penen+a nmee+c co6c+nenna ocka n sanncn, no.+omy ocka c penen+om nenenon
kypnnr ye ro+ona. Tenepr npn nrnonnennn penen+a nannponkn nam nonao6n+c nona
ocka. Fe ee pasmec+n+r? Hpmo nonepx ockn c penen+om nenenon kypnnr. B konne
konnon, nrnonnenne .+oro penen+a 6rno npnoc+anonneno, noka ne 6ye+ ro+ona
nannponka. Penen+ nenenon kypnnr (kap c+eka) nam nce panno ne nonao6n+c, noka
penen+ npnro+onnenn nannponkn ne 6ye+ nrnonnen, a ero kap ne ocno6on+c. Kapr,
nanoennre pyr na pyra, o6pasym+ c+onky, nnn c+ek.
Pnc. 5.3. ne ockn c penen+amn
Hporpammnc+r ronop+: Hpn nrsone qynknnn ee kap cosae+c na nepmnne
+#$/). Hpn sanepmennn qynknnn ee kap nsnnekae+c ns c+eka n ynnn+oae+c.
Fnana 5. Oyukquu
________________________________________________________________________________________________
35
anan+e no6nne nosnakomnmc c pa6o+on c+eka, nomec+n qynknnm
24#L^##hZ*6/`#(Z.(h/_() n rnno+e+nneckym nporpammy:
;#*$ 24#L^##hZ*6/`#(Z.(h/_7*+& A#.+$2>
?
*+& +/,/22'(_*+.&/2 k XT l XT : A#.+$2H
A(*+&B7C^##h B#( $ 6*+.&/23G+C9 +/,/22'(_*+.&/2>H
J
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& &#&'-E/*84& k XIH
*+& 8*M-/&2E/*84& k XH
*+& &.(h/_E/*84& k &#&'-E/*84& U 8*M-/&2E/*84&H
24#L^##hZ*6/`#(Z.(h/_7&.(h/_E/*84&>H
(/&.(+ IH
J
Bcnomnn+e, n+o qynknn 6'*+() ncera nrnonne+c nepnon. dynknn 6'*+()
nrsrnae+ qynknnm 24#L^##hZ*6/`#(Z.(h/_(), ko+opa nannnae+ nrnonn+rc. Kak
6ye+ nrrne+r c+ek nporpammr cpasy e nocne ymnoenn A#.+$2 na 15?
Pnc 5.4. na kapa n c+eke
C+ek pa6o+ae+ no npnnnnny nocnennm npnmen, nepnrm nrmen. Hnane ronop
24#L^##hZ*6/`#(Z.(h/_() nsnnekae+ cnon kap ns c+eka o +oro kak 6'*+() nsnnene+
cnon kap.
O6pa+n+e nnnmanne : A # . + $ 2 , ennc+nennrn napame+p
24#L^##hZ*6/`#(Z.(h/_(), nne+c nac+rm kapa. Bcnomnn+e, n+o napame+p
npec+anne+ co6on nokanrnym nepemennym, ko+opon npncnannae+c snanenne
coo+ne+c+nymmero aprymen+a. B namem npnmepe nepemenna &.(h/_E/*84& co
snanennem 9 nepeae+c n aprymen+e 24#L^##hZ*6/`#(Z.(h/_(). Ee snanenne
npncnannae+c napame+py A#.+$2 n konnpye+c n kap qynknnn.
Pekypcnn
Moe+ nn qynknn nrsna+r cama ce6? A nonemy 6r n ne+? 3+o nasrnae+c &$/6&+!$4.
Bosmono, nr cnrmann ckynnym nnnnym necnm 99 6y+rnok nnna. Cosan+e nonym
Fnana 5. Oyukquu
________________________________________________________________________________________________
36
nporpammy komannon c+pokn C c nmenem a//(%#+8. O+kpon+e qann main.c, o6anr+e
qynknnm n nrnoa cnon necnn n nrsonn+e ee ns 6'*+7>:
)*+,-.$/ 12&$*#345
5S4w Q4JhoR1xSJhL4J3 JaT/1I.K~S33i1QV
z
4K LJaT/1I.K~S33i1Q jj bV z
HI4J3KLMoR1I1 PI1 Q4THiq JS TSI1 /S33i1Q SK /11I SJ 3R1 OPii"UJMVW
| 1iQ1 z
HI4J3KLMrw /S33i1Q SK /11I SJ 3R1 OPii" rw /S33i1Q SK /11I"UJM`
JaT/1I.K~S33i1Q` JaT/1I.K~S33i1QVW
4J3 SJ11O1I j JaT/1I.K~S33i1Q 6 !W
HI4J3KLMoPp1 SJ1 wSOJ` HPQQ 43 PISaJw` rw /S33i1Q SK /11I SJ 3R1 OPii"
UJM`
SJ11O1IVW
Q4JhoR1xSJhLSJ11O1IVW FF oR4Q KaJ234SJ 2PiiQ 43Q1iK
HI4J3KLM{a3 P /S33i1 4J 3R1 I12q2i4Jh` rw 1TH3q /S33i1Q 4J 3R1 /4J"UJM`
JaT/1I.K~S33i1QVW
|
|
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
Q4JhoR1xSJhLVW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Pesynr+a+ 6ye+ nrrne+r +ak;
M#&&-/2 #B M//( #+ &4/ L'--3 M#&&-/2 #B M//(3
Z'h/ #+/ $#L+9 A'22 *& '(#.+$9 W M#&&-/2 #B M//( #+ &4/ L'--3
W M#&&-/2 #B M//( #+ &4/ L'--3 W M#&&-/2 #B M//(3
Z'h/ #+/ $#L+9 A'22 *& '(#.+$9 Y M#&&-/2 #B M//( #+ &4/ L'--3
Y M#&&-/2 #B M//( #+ &4/ L'--3 Y M#&&-/2 #B M//(3
333
X M#&&-/2 #B M//( #+ &4/ L'--3 X M#&&-/2 #B M//(3
Z'h/ #+/ $#L+9 A'22 *& '(#.+$9 I M#&&-/2 #B M//( #+ &4/ L'--3
Z4/(/ '(/ 2*6A-_ +# 6#(/ M#&&-/2 #B M//( #+ &4/ L'--3
b.& ' M#&&-/ *+ &4/ (/,_,-*+89 X /6A&_ M#&&-/2 *+ &4/ M*+3
b.& ' M#&&-/ *+ &4/ (/,_,-*+89 V /6A&_ M#&&-/2 *+ &4/ M*+3
333
b.& ' M#&&-/ *+ &4/ (/,_,-*+89 W /6A&_ M#&&-/2 *+ &4/ M*+3
b.& ' M#&&-/ *+ &4/ (/,_,-*+89 /6A&_ M#&&-/2 *+ &4/ M*+3
Kak nrrnn+ c+ek, kora co c+enr cnnmae+c nocnen 6y+rnka?
Boo6me-+o kapr n c+ek o6rnno ne paccma+pnnam+c n nananrnom kypce
nporpammnponann, no mon onr+ nokasan. n+o .+n nen npesnrnanno nonesnr n
neonr+nrx nporpammnc+on. Bo-nepnrx, onn am+ 6onee konkpe+noe nonnmanne o+ne+on
na nonpocr +nna: A n+o nponcxon+ c monmn nokanrnrmn nepemennrmn, sanepmae+
nrnonnenne? Bo-n+oprx, onn nomoram+ paso6pa+rc n pa6o+e '#1),=!/) -
nporpammr, ko+opa nokasrnae+, n+o nponcxon+ namen nporpamme (a .+a nnqopmann
Fnana 5. Oyukquu
________________________________________________________________________________________________
37
ynpomae+ nonck n ncnpannenne omn6ok). Kora nr c+pon+e n sanyckae+e nporpammy n
Xcode, o+nannk 0&!+'$,!"*$#+* k nporpamme, n+o6r nr mornn c nnm pa6o+a+r.
Pnc 5.5. Kapr nrsonon pekypcnnnon qynknnn n c+eke
Hpocuovp ka(pon n ovna(unke
Br moe+e nocnonrsona+rc o+nannkom n npocmo+pa kapon n c+eke, onako n
.+oro npne+c npepna+r nrnonnenne namen nporpammr na cepenne. B npo+nnnom
cnynae qynknn 6'*+7> o+pa6o+ae+ o konna, n nnkaknx kapon ne oc+ane+c. H+o6r
ynne+r kak mono 6onrme kapon n nporpamme a//(%#+8, cneye+ npepna+r
nrnonnenne n c+poke, ko+opa nrnon+ coo6menne There are simply no more bottles of
beer on the wall.
Kak .+o cena+r? Hann+e n qanne main.c c+poky
A(*+&B 7Ci4j|j (j 2*6A-_ +# 6#(/ M#&&-/2 #B Mjj| #+ &4/ L'--9G+C>H
Cnena o+ koa naxo+c na cnerka sa+enennrx c+on6na. Eenknn+e na 6onee
mnpokom, nenom c+on6ne pom c npnneennon c+pokon.
Cnnnn nnnka+op nokasrnae+, n+o nr yc+anonnnn #'=/6 0&$&<()"!*. Tak
nasrnae+c nosnnn n nporpammnom koe, n ko+opom o+nannk onen npnoc+anonn+r
nrnonnenne namen nporpammr. Cnona sanyc+n+e nporpammy. Ona sanyckae+c, a no+om
oc+anannnnae+c npmo nepe c+pokon, n ko+opon 6rna yc+anonnena +onka npeprnann.
Cennac nama nporpamma sac+rna na mec+e, n nr moe+e nsynn+r ee 6onee
nnnma+enrno. B o6nac+n nannra+opa menknn+e na snanke
ptg999
Chapter 5Functons
36
to look at. To see as many frames as possible in our program, we want to halt execution on
the line that prints 'There are simply no more bottles of beer on the wall.
How do we do this? In ma1h.c, find the line
pJ1h11{"TheJe aJe s1mpy ho moJe bo11es o1 beeJ oh 1he wa.\h"),
There are two shaded columns to the left of your code. Click on the wider, lefthand column next to this
line of code.
Fgure 5.6Settng a breakpont
The blue indicator shows that you`ve set a breakpoint. A breakpoint is a location in code where you
want the debugger to pause the execution of your program. Run the program again. It will start and
then stop right before it executes the line where you set the breakpoint.
Now your program is temporarily frozen in time, and you can examine it more closely. In the navigator
area, click the icon to open the debug navigator. This navigator shows all the frames currently on
the stack, also called a stack trace.
In the stack trace, frames are identified by the name of their function. Given your program consists
almost entirely of a recursive function, these frames have the same name and you must distinguish
them by the value of oheFeweJ that gets passed to them. At the bottom of the stack, you will, of course,
find the frame for man{).
You can select a frame to see the variables in that frame and the source code for the line of code that is
currently being executed. Select the frame for the first time sngThe5ong is called.
, o+kprnammem ")(!%)#'&
'#1),/!. B .+om nannra+ope o+o6paam+c nce kapr, naxomnec n c+eke n annrn
momen+ (.+a nnqopmann +ake nasrnae+c #&)++!&'(/'4 +#$/)).
Fnana 5. Oyukquu
________________________________________________________________________________________________
38
Pnc. 5.6. Yc+anonka +onkn npeprnann
B +paccnponke c+eka kapr nen+nqnnnpym+c no nmenam cnonx qynknnn. C
yne+om +oro, n+o nama nporpamma coc+on+ non+n ncknmnn+enrno ns pekypcnnnon
qynknnn, n kapax 6yy+ ykasanr onnakonre nmena, a pasnnna+r nx npne+c no
nepeanaemomy snanennm #+/`/L/(. B camom konne c+eka, konenno, pacnonoen kap
qynknnn 6'*+7>.
Br6pan kap, mono npocmo+pe+r nepemennre n .+om kape n ncxonrn ko
+ekymen nrnonnemon c+pokn koa, Br6epn+e kap nepnoro nrsona 2*+8Z4/%#+8.
B nenon nnnen nac+n okna naxon+c cnncok nepemennrx nr6pannoro kapa c nx
+ekymnmn snanennmn. Cnpana n o6nac+n, nasrnaemon konconrm, o+o6paae+c nrno
nporpammr. (Ecnn konconr ne nnna, nann+e knonkn
ptg999
return
37
Fgure 5.7Frames on the stack for a recursve functon
You can see this frame`s variables and their values on the bottom left of the window. To the right,
you can also see the output in an area called the console. (If you don`t see the console, find the
buttons at the right of the screen towards the bottom half. Click the middle button to reveal
the console.) In the console, you can see the effect of your breakpoint: the program stopped before
reaching the line that ends the song.
Now we need to remove the breakpoint so that the program will run normally. You can simply
drag the blue indicator off the gutter. Or click the icon at the top of the navigator area to reveal
the breakpoint navigator and see all the breakpoints in a project. From there, you can select your
breakpoint and delete it.
To resume execution of your program, click the button on the debugger bar between the editor and
the variables view.
We just took a quick look at the debugger here to demonstrate how frames work. However, using the
debugger to set breakpoints and browse the frames in a program`s stack will be helpful when your
program is not doing what you expect and you need to look at what is really happening.

Many functions return a value when they complete execution. You know what type of data a function
will return by the type that precedes the function name. (If a function doesn`t return anything, its return
type is vo1d.)
Create a new C Command Line Tool named Degrees. In ma1h.c, add a function before that
converts a temperature from Celsius to Fahrenheit. Then update to call the new function.
y npanoro kpa .kpana.
Eenknn+e na cpenen knonke, n+o6r o+kpr+r konconr). Ha konconn nnen .qqek+
yc+anonkn +onkn npeprnann: nporpamma oc+anonnnacr o oc+nenn nocnenen
c+pokn, sanepmammen necnm.
Tenepr +onky npeprnann nyno y6pa+r, n+o6r nporpamma npoonnna
nopmanrnoe nrnonnenne. Hepe+amn+e mrmrm cnnnn nnnka+op c nenoro non, nnn
menknn+e na snanke
ptg999
return
37
Fgure 5.7Frames on the stack for a recursve functon
You can see this frame`s variables and their values on the bottom left of the window. To the right,
you can also see the output in an area called the console. (If you don`t see the console, find the
buttons at the right of the screen towards the bottom half. Click the middle button to reveal
the console.) In the console, you can see the effect of your breakpoint: the program stopped before
reaching the line that ends the song.
Now we need to remove the breakpoint so that the program will run normally. You can simply
drag the blue indicator off the gutter. Or click the icon at the top of the navigator area to reveal
the breakpoint navigator and see all the breakpoints in a project. From there, you can select your
breakpoint and delete it.
To resume execution of your program, click the button on the debugger bar between the editor and
the variables view.
We just took a quick look at the debugger here to demonstrate how frames work. However, using the
debugger to set breakpoints and browse the frames in a program`s stack will be helpful when your
program is not doing what you expect and you need to look at what is really happening.

Many functions return a value when they complete execution. You know what type of data a function
will return by the type that precedes the function name. (If a function doesn`t return anything, its return
type is vo1d.)
Create a new C Command Line Tool named Degrees. In ma1h.c, add a function before that
converts a temperature from Celsius to Fahrenheit. Then update to call the new function.
n nepxnen nac+n nanenn nannra+opa, n+o6r o+kpr+r ")(!%)#'&
#'=$/ 0&$&<()"!* n npocmo+pe+r necr cnncok +onek npeprnann n npoek+e. Hann+e n
.+om cnncke cnom +onky npeprnann n yann+e ee.
H+o6r npoonn+r nrnonnenne nporpammr, menknn+e na knonke,
ptg999
return
37
Fgure 5.7Frames on the stack for a recursve functon
You can see this frame`s variables and their values on the bottom left of the window. To the right,
you can also see the output in an area called the console. (If you don`t see the console, find the
buttons at the right of the screen towards the bottom half. Click the middle button to reveal
the console.) In the console, you can see the effect of your breakpoint: the program stopped before
reaching the line that ends the song.
Now we need to remove the breakpoint so that the program will run normally. You can simply
drag the blue indicator off the gutter. Or click the icon at the top of the navigator area to reveal
the breakpoint navigator and see all the breakpoints in a project. From there, you can select your
breakpoint and delete it.
To resume execution of your program, click the button on the debugger bar between the editor and
the variables view.
We just took a quick look at the debugger here to demonstrate how frames work. However, using the
debugger to set breakpoints and browse the frames in a program`s stack will be helpful when your
program is not doing what you expect and you need to look at what is really happening.

Many functions return a value when they complete execution. You know what type of data a function
will return by the type that precedes the function name. (If a function doesn`t return anything, its return
type is vo1d.)
Create a new C Command Line Tool named Degrees. In ma1h.c, add a function before that
converts a temperature from Celsius to Fahrenheit. Then update to call the new function.
na nanenn
o+nannka, mey peak+opom n cnnckom nepemennrx.
Mr ncero nnmr paccmo+penn camre o6mne nosmonoc+n o+nannka, n+o6r nr
nonnn, kak pa6o+am+ kapr. Hcnonrsonanne o+nannka n nasnanenn +onek
npeprnann n npocmo+pa kapon n c+eke nporpammr 6ye+ oco6enno nonesnrm +ora,
kora nama nporpamma pa6o+ae+ ne +ak, kak onanocr, n nr xo+n+e paso6pa+rc, n+o
e n nen nponcxon+.
Fnana 5. Oyukquu
________________________________________________________________________________________________
39
Pnc. 5.7. kapr pekypcnnnon qynknnn n c+eke
return
Mnorne qynknnn n konne cnoen pa6o+r nosnpamam+ neko+opoe snanenne. Tnn
annrx, nosnpamaemrx qynknnen, ykasrnae+c nepe ee nmenem. (Ecnn qynknn ne
nosnpamae+ nnkakoro snanenn, nmec+o +nna ykasrnae+c knmnenoe cnono ;#*$).
Cosan+e nonym nporpammy komannon c+pokn C c nmenem O/8(//2. B qanne
main.c o6anr+e nepe 6'*+7> qynknnm, ko+opa npeo6pasye+ +emnepa+ypy no
Henrcnm n mkany dapenren+a. Bknmnn+e nrson nonon qynknnn n 6'*+7>.
)*+,-.$/ 12&$*#345
KiSP3 KPRI1JR143IST71iQ4aQLKiSP3 21iV
z
KiSP3 KPRI j 21i X !" t cC"bW
HI4J3KLMrK 71iQ4aQ 4Q rK PRI1JR143UJM` 21i` KPRIVW
I13aIJ KPRIW
|
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
KiSP3 KI111NJ7 j bW
KiSP3 KI111NJ j KPRI1JR143IST71iQ4aQLKI111NJ7VW
HI4J3KLMP31I KI111Q P3 rK w1hI11Q PRI1JR143UJM` KI111NJVW
(/&.(+ IH
J
Bame+nnn, kak mr 6epem nosnpamaemoe snanenne B'4(/+4/*&`(#6^/-2*.27> n
npncnannaem ero nepemennon B(///K+` +nna B-#'&? Bopono, nepno?
Fnana 5. Oyukquu
________________________________________________________________________________________________
40
Brnonnenne qynknnn npekpamae+c npn nosnpa+e ynpannenn komanon return.
Hanpnmep, nosrmem cneymmym qynknnm:
B-#'& ';/('8/7B-#'& '9 B-#'& M>
?
(/&.(+ 7' l M>@V3IH
A(*+&B7CZ4/ 6/'+ .2&*B*/2 &4/ /+$G+C>H
J
Ecnn nrsna+r .+y qynknnm, nrson A(*+&B7> nrnonn+rc nnkora ne 6ye+.
Bosnnkae+ ec+ec+nennrn nonpoc: Honemy mr ncera nosnpamaem 0 ns qynknnn
main()? Bosnpama cnc+eme 0, nr +em camrm ronopn+e: Bce npomno nopmanrno.
Ecnn nporpamma sanepmae+c ns-sa kaknx-+o nenonaok, nosnpamae+c snanenne 1.
Ha nepnrn nsrn .+o npo+nnopenn+ cmrcny 0 n 1 n komanax *B; snanenne 1
coo+ne+c+nye+ &(./, a snanenne 0-B'-2/, no.+omy npoe 6r ec+ec+nenno
paccma+pnna+r 1 kak npnsnak ycnexa, a 0 - kak npnsnak neyann. Ho.+omy cnn+an+e, n+o
6'*+() nosnpamae+ konnnec+no omn6ok. B .+om cnynae 0 - nynmnn pesynr+a+! Hn onon
omn6kn - nporpamma nrnonnena ycnemno.
H+o6r cmrcn nosnpamaemoro snanenn c+an 6onee narnnrm, neko+opre
nporpammnc+r ncnonrsym+ konc+an+r KZe%Q%% n Kie`!KcQN, ko+opre
npec+annm+ co6on o6rnnre ncenonnmr n snanennn 0 n 1 coo+ne+c+nenno. 3+n
konc+an+r onpeenm+c n saronononnom qanne stdlib.h:
)*+,-.$/ 12&$*#345
4J2iaw1 Q3wi4/"R
B-#'& B'4(/+4/*&`(#6^/-2*.27B-#'& ,/->
?
B-#'& B'4( k ,/- : X3W l SV3IH
A(*+&B7CB ^/-2*.2 *2 B `'4(/+4/*&G+C9 ,/-9 B'4(>H
(/&.(+ B'4(H
J
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
B-#'& B(///K+^ k IH
B-#'& B(///K+` k B'4(/+4/*&`(#6^/-2*.27B(///K+^>H
A(*+&B7CE'&/( B(///2 '& B $/8(//2 `'4(/+4/*&G+C9 B(///K+`>H
(/&.(+ Nox77xxH
J
B .+on knnre mr 6yem ncnonrsona+r 0 nmec+o EXIT_SUCCESS.
Ino6annme n cvavnueckne nepeuennme
B .+on rnane ynomnnanncr nokanrnre nepemennre, cymec+nymmne +onrko no npem
nrnonnenn qynknnn. Take cymec+nym+ nepemennre, oc+ynnre n nm6on qynknnn n
n nm6oe npem. Onn nasrnam+c %1'.)19"<3! 0$&$3$""<3!. H+o6r nepemenna c+ana
rno6anrnon, o6rnn+e ee sa npeenamn kakon-nn6o qynknnn. Hanpnmep, mono
Fnana 5. Oyukquu
________________________________________________________________________________________________
41
o6ann+r nepemennym -'2&Z/6A/('&.(/ n xpanenn npeo6pasonannon +emnepa+ypr
no Henrcnm. Bknmnn+e n nporpammy o6rnnenne rno6anrnon nepemennon:
)*+,-.$/ 12&$*#345
)*+,-.$/ 12&$-*M345
FF d:]E>Z<+&< ,Z;:)Z]+;f @<9<A<++;f KiSP3 iPQ3o1TH1IP3aI1W
B-#'& B'4(/+4/*&`(#6^/-2*.27B-#'& ,/->
?
iPQ3o1TH1IP3aI1 j 21iW
B-#'& B'4( k ,/- : X3W l SV3IH
A(*+&B7CB ^/-2*.2 *2 B `'4(/+4/*&G+C9 ,/-9 B'4(>H
(/&.(+ B'4(H
J
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
B-#'& B(///K+^ k IH
B-#'& B(///K+` k B'4(/+4/*&`(#6^/-2*.27B(///K+^>H
A(*+&B7CE'&/( B(///2 '& B $/8(//2 `'4(/+4/*&G+C9 B(///K+`>H
HI4J3KLMoR1 iPQ3 31TH1IP3aI1 2SJ51I31w OPQ rKUJM` iPQ3o1TH1IP3aI1VW
(/&.(+ KZe%Q^^%%H
J
hm6a ne+pnnnanrna nporpamma coc+on+ ns ec+kon qannon, coepamnx
pasnre qynknnn. Fno6anrnre nepemennre oc+ynnr n koe kaoro ns .+nx qannon.
Hnora conmec+noe ncnonrsonanne nepemennon pasnrmn qannamn - nmenno +o, n+o
nyno. C pyron c+oponr, oc+yn k nepemennon ns mnoec+na qynknnn cnoco6en
nopon+r nennnanmym ny+annny. n pemenn .+on npo6nemr cymec+nym+
+#)#!=$+/!$ 0$&$3$""<$. C+a+nnecka nepemenna, kak n rno6anrna nepemenna,
o6rnne+c sa npeenamn kakon-nn6o qynknnn - no npn .+om ona oc+ynna +onrko n
koe +oro qanna, n ko+opom 6rna o6rnnena. Taknm o6pasom, nr nonynae+e nce
npenmymec+na nepemennon ne nokanrnon, cymec+nymmen nne kaknx-nn6o qynknnn, no
npn .+om ns6erae+e npo6nem +nna K+o +poran mom nepemennym?
Hamy rno6anrnym nepemennym mono nerko npeo6pasona+r n c+a+nneckym, no
+nk kak nama nporpamma coc+on+ ns ennc+nennoro qanna main.c, +aka samena nn na
n+o ne nonnne+.
@@ vqtjuwj B()(&\<B*;f sj{jjuug Q3P342 B-#'& -'2&Z/6A/('&.(/H
H c+a+nnecknm, n rno6anrnrm nepemennrm mono npncnon+r snanenne npn
cosannn:
@@ zj{jjuuv -'2&Z/6A/('&.(/ wuwwtww{xjnfv TI |{pxfw Q3P342 B-#'&
-'2&Z/6A/('&.(/ k TI3IH
Ecnn nepemennon ne 6rno npncnoeno nananrnoe snanenne, ona an+oma+nneckn
nnnnnannsnpye+c nynem.
Fnana 5. Oyukquu
________________________________________________________________________________________________
42
B .+on rnane nr nosnakomnnncr c qynknnmn. B nac+n III,kora mr sanmemc
Objective-C, nam +ake nc+pe+n+c +epmnn 3$#', - me+or nmem+ onenr, onenr mnoro
o6mero c qynknnmn.
npawnenne
Cymma nny+pennnx yrnon +peyronrnnka onna 6r+r panna 180 rpaycam. Cosan+e
nonym nporpammy komannon c+pokn C c nmenem Z(*'+8-/. Bknmnn+e n qann main.c
qynknnm, ko+opa nonynae+ nepnre na yrna n nosnpamae+ nennnnny +pe+rero. Brson
qynknnn onen nrrne+r +ak:
)*+,-.$/ 12&$*#345
@@ !$$ _#.( +/L B.+,&*#+ 4/(/
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
B-#'& '+8-/! k SI3IH
B-#'& '+8-/a k RI3IH
B-#'& '+8-/^ k (/6'*+*+8!+8-/7'+8-/!9 '+8-/a>H
A(*+&B7CZ4/ &4*($ '+8-/ *2 3VBG+C9 '+8-/^>H
(/&.(+ IH
J
B npnneennom npnmepe nporpamma onna nrana+r cneymmnn pesynr+a+: The third
angle is 90.00
Fnana 5. Oyukquu
________________________________________________________________________________________________
43
" &BZ)
Mr ye ncnonrsonann nncna n nsmepenn n nrnoa +emnepa+ypr, neca n
npoonn+enrnoc+n npnro+onnenn nnenkn. Hopa no6nne paso6pa+rc c +em, kak
pa6o+am+ nncna n srke C. Ha komnrm+epe nncna en+c na na nna: nenre n
nemec+nennre (c nnanammen san+on). Mr ye ncnonrsonann n +e n pyrne. B .+on
rnane noc+apamcr npnnec+n n cnc+emy nce +o, n+o nporpammnc+ C onen sna+r o
nncnax.
printf()
Ho npee nem 6pa+rc sa nncna, mr paccmo+pnm qynknnm A(*+&B7>, ko+opa +oe
ye ncnonrsonanacr n namnx nporpammax. dynknn A(*+&B7> nrnon+ na konconr
c+poky, +o ec+r nenonky cnmnonon. Ho cy+n, c+poka coepn+ neko+oprn +ekc+.
O+kpon+e npoek+ ^-'22^/(&*B*,'&/2 n nann+e n qanne main.c qynknnm
,#+8('&.-'&/%&.$/+&7>.
;#*$ ,#+8('&.-'&/%&.$/+&7,4'( :2&.$/+&9 ,4'( :,#.(2/9 *+& +.6O'_2>
?
A(*+&B7C2 4'2 $#+/ '2 6.,4 2 b(#8('66*+8 '2 K ,#.-$ B*& *+&# $ $'_23G+C9
2&.$/+&9 ,#.(2/9 +.6O'_2>H
J
H+o enae+ .+o+ nrson A(*+&B7>? Boo6me-+o nr ye nnenn pesynr+a+ n
snae+e, n+o ona enae+. anan+e pas6epemc, kak ona .+o enae+.
dynknn A(*+&B7> nonynae+ aprymen+ - c+poky. H+o6r onpeenn+r +#&'/'(<4
1!#$&)1 (n o+nnnne o+ c+pokn, xpanmenc n nepemennon), cneye+ saknmnn+r +ekc+ n
kanrnkn.
C+poka, nepeanaema A(*+&B7>, nasrnae+c >'&3)#"'4 +#&'/'4. dopma+na
c+poka moe+ coepa+r -)0'1"!#$1!. B namem npnmepe c+poka coepn+ +pn
sanonnn+en: 2,2 n $. Hpn nrnonnennn nporpammr sanonnn+enn samenm+c
snanennmn nepemennrx, cneymmnx sa c+pokon, - n namem npnmepe .+o nepemennre
2&.$/+&, ,#.(2/ n +.6O'_2. O6pa+n+e nnnmanne: nepemennre noc+annm+c na
mec+o sanonnn+enen n ykasannom nopke. Ecnn nomen+r mec+amn 2&.$/+& n ,#.(2/ n
cnncke nepemennrx. +o coo6menne 6ye+ nrrne+r +ak:
^#,#' 4'2 $#+/ '2 6.,4 '(h b(#8('66*+8 '2 K ,#.-$ B*& *+&# T $'_23
Tem ne menee sanonnn+enn n nepemennre nenrs samen+r no cnoemy
ycmo+pennm. Banonnn+enr 2 osnanae+, n+o na ero mec+o onna noc+ann+rc c+poka,
Fnana 6. Hucna
________________________________________________________________________________________________
44
a sanonnn+enr $ npenasnanen n nenoro nncna. (Honpo6yn+e nomen+r nx mec+amn n
nocmo+pn+e, n+o npo nsone+).
O6pa+n+e nnnmanne: nepemennre 2&.$/+& n ,#.(2/ o6rnnenr c +nnom ,4'(:. Hoka
cnn+an+e, n+o ,4'(: - .+o o6rnna c+poka. Mr eme nepnemc k c+pokam Objective-C n
rnane 14, a k +nny ,4'(: - n rnane 34.
Hakonen, n+o .+o sa G+? B komanax A(*+&B7> cnmnon nepenoa c+pokn
neo6xonmo nno nknmna+r n nrnonmre annre, nnane nce annre 6yy+ nrnon+rc n
ony c+poky. Kom6nnann G+ o6osnanae+ cnmnon nepenoa c+pokn (nnn cnmnon nonon
c+pokn).
A +enepr nepnemc k nncnam.
Henme uncna
Henoe nncno ne nmee+ po6non nac+n. Henre nncna xopomo noxo+ n ne- ko+oprx
saan - ckaem, n nocne+a. Heko+opre saann (ckaem, nocne+ ncero nacenenn
nnane+r) +pe6ye+ onenr 6onrmnx nncen. n pyron saann (nanpnmep, nocne+ e+en n
knacce) xna+n+ cymec+nenno menrmnx nncen.
n pasnrx saan 6rnn cosanr nenonncnennre nepemennre c pasnrmn pas-
mepamn. Henonncnenna nepemenna coc+on+ ns neko+oporo konnnec+na paspon
(6n+on), ncnonrsyemrx n konponann nncna; nem 6onrme paspon n nepemennon, +em
6onrme pasnrx nncen n nen mono coxpann+r. Tnnnnnre pasmepr nepemennrx - 8, 16,
32 n 64 paspnre.
Kpome +oro, n onnx saan nynr o+pnna+enrnre nncna, a n pyrnx ne+.
Coo+ne+c+nenno, nenonncnennre +nnr en+c na -")/'(<$ n .$--")/'(<$. 8 -
paspna 6essnakona nepemenna moe+ xpann+r nm6oe nncno o+ 0 o 255. O+kya
6epe+c .+o snanenne? 2 = 256 nosmonrx nncen, a mr nannnaem o+cne+ c 0.
B 64-paspnon snakonon nepemennon moe+ xpann+rc nm6oe nenoe nncno ns
nanasona o+ -9 223 372 036854775808 o 9223 372 036 854 775 807. Onn pasp,
o+neennrn no snak, wfotjnfv. Oc+ae+c 2 = 9 223 372 036 854 775 808.
Hpn o6rnnennn nenoro nncna mono onenr +onno ykasa+r, n+o nmenno nam
nyno:
NJ3cC W FF cC69)_9El+;< n<Z;< :<_ _+)*)
xNJ3! qW FF i69)_9El+;< n<Z;< B; _+)*;A
Bnponem, nporpammnc+r name ncnonrsym+ 6onee o6mne +nnr, o ko+oprx nr ysnann n
rnane 3.
2RPI )W FF 69)_9El+;< _+)\<+&<
QRSI3 /W FF d:%\+; !69)_9El+;< L> _)>&B&A;B(& ;( @Z)(m;9A%V
4J3 BW FF d:%\+; cC69)_9El+;< L> _)>&B&A;B(& ;( @Z)(m;9A%V
iSJh wW FF cC6 &Z& k69)_9El+;< L> _)>&B&A;B(& ;( @Z)(m;9A%V
iSJh iSJh
<W FF
k69)_9El+;< _+)\<+&<

Honemy +nn ,4'( o+necen k nncnonrm? hm6on cnmnon moe+ 6r+r npec+annen
n nne 8-paspnoro nncna, a komnrm+epr npenonn+am+ pa6o+a+r c nncnamn. Tnnr
Fnana 6. Hucna
________________________________________________________________________________________________
45
char, 24#(&, *+&, -#+8 n -#+8 -#+8 no ymonnannm nmem+ snak, no nr moe+e ykasa+r
knmnenoe cnono .+2*8+/$ n cosann 6essnakonoro .knnnanen+a.
Kpome +oro, pasmepr nenrx nncen sannc+ o+ nna+qopmr. (Hna+qopmon
nasrnae+c cone+anne onepannonnon cnc+emr n konkpe+noro komnrm+epa nnn
mo6nnrnoro yc+ponc+na.) Onn nna+qopmr nnm+c 32-paspnrmn, pyrne 64-
paspnrmn. Onn o+nnnam+c npee ncero pasmepom apeca nam+n (mr noronopnm o6
.+om nopo6nee n rnane 8).
dnpma Apple cosana na nenrx +nna, ko+opre nnm+c 32-paspnrmn na 32-
paspnrx nna+qopmax n 64-paspnrmn na 64-paspnrx nna+qopmax:
xiJ31h1I hW
xNJ31h1I RW
3+n +nnr ncnonrsym+c no mnornx npnnoennx Apple. Onn no ncex o+nomennx
.knnnanen+nr -#+8 n .+2*8+/$ -#+8.
3anonnnvenn (nn nmno(a nenmx uncen
Cosan+e nonrn npoek+ komannon c+pokn C c nmenem P.6M/(2. B qanne main.+
cosan+e nenoe nncno n nrnen+e ero no ocnonannm 10 (+o ec+r n ec+nnnom
npec+annennn) npn nomomn qynknnn A(*+&B7>:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
4J3 j CssW
HI4J3KLM 4Q rw"UJM` VW (/&.(+ IH
J
Pesynr+a+ onen nrrne+r +ak:
d *2 VTT3
Kak nr nnenn, sanonnn+enr $ nrnon+ nenoe nncno n ec+nnnom
npec+annennn. Kak pa6o+am+ pyrne sanonnn+enn? Henoe nncno +ake mono nrnec+n
n nocrmepnnnom nnn mec+nana+epnnnom npec+annennn. o6anr+e n nporpammy napy
c+pok:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& d k VTTH
A(*+&B7Cd *2 $3G+C9 d>H
HI4J3KLMNJ S23Pi` 4Q rS"UJM` VW
HI4J3KLMNJ R1Pw124TPi` 4Q r"UJM` VW
(/&.(+ IH
J
Fnana 6. Hucna
________________________________________________________________________________________________
46
Banyc+n+e ee. Pesynr+a+ 6ye+ nrrne+r +ak:
d *2 VTT3
K+ #,&'-9 d *2 SYY3
K+ 4/d'$/,*6'-9 d *2 BB3
(Mr nepnemc k mec+nana+epnnnrm annrm n rnane 33.)
A ecnn nrnon+c nnnnoe nenoe nncno (+o ec+r coepamee 6onrme paspon,
nem o6rnnoe)? B .+om cnynae mey n cnmnonom qopma+a cneye+ nc+ann+r -
(o6osnanenne -#+8) nnn -- (o6osnanenne -#+8 -#+8). Hsmenn+e nporpammy +ak, n+o6r
nmec+o *+& n nen ncnonrsonanc +nn -#+8:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
iSJh d k VTTH
A(*+&B7Cd *2 riw3G+C9 d>H
A(*+&B7CK+ #,&'-9 d *2 riS3G+C9 d>H
A(*+&B7CK+ 4/d'$/,*6'-9 d *2 ri3G+C9 d>H
(/&.(+ IH
J
n nrnoa nenoro ec+nnnoro nncna 6es snaka ncnonrsye+c sanonnn+enr .:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
aJQ4hJ1w -#+8 d k VTTH A(*+&B7Cd *2 ria3G+C9 d>H
@@ qgfj{wur w }jfnupnj{wur g{n xyj sgp{xjqjn9 ng wftg
qrqgpwnfv j uo
A(*+&B7CK+ #,&'-9 d *2 -#3G+C9 d>H
A(*+&B7CK+ 4/d'$/,*6'-9 d *2 -d3G+C9 d>H
(/&.(+ IH
J
Onepannn c nenmun uncnaun
Apnqme+nneckne onepa+opr +, - n * pa6o+am+ +ak, kak n cneonano ona+r. Kpome
+oro, onn o6naam+ ec+ec+nennrmn npannnamn npnopn+e+a ma+ema+nnecknx onepannn: *
nrnncne+c o + nnn -. Bamenn+e n main.c nperymnn ko nrnncnennem nrpaenn:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
HI4J3KLMc X c t s X C j rwUJM` c X c t s X CVW
(/&.(+ IH
J
Fnana 6. Hucna
________________________________________________________________________________________________
47
Br nonynn+e cneymmnn pesynr+a+:
S : S l T : V k X
Henouncnennoe (enenne
Mnorne nannnammne nporpammnc+r C ynnnm+c +omy, kak pa6o+ae+ nenonncnennoe
enenne. anan+e nonpo6yem:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7CS : S l T : V k $G+C9 S : S l T : V>H
HI4J3KLM!! F c j rwUJM` !! F cVW
(/&.(+ IH
J
onno nonynn+rc 11 / 3 - 3.666667, nepno? A no+ n ne+. Pesynr+a+ enenn 11 /
3 panen 3. Hpn enennn onoro nenoro nncna na pyroe ncera nonynae+c nenoe nncno,
Cnc+ema okpyrne+ pesynr+a+ o+cenennem po6non nac+n (+aknm o6pasom, npn enennn
-11 / 3 nonynn+c -3). H .+o nrrnn+ nnonne pasymno, ecnn paccma+pnna+r onepannm
+ak: 11 ennm na 3 - nonynae+c 3 c oc+a+kom 2. Onako oc+a+ok o+ enenn
oc+a+onno nac+o +oe naen. Onepa+op noxo na @, no nmec+o nac+noro on
nosnpamae+ oc+a+ok o+ enenn:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7CS : S l T : V k $G+C9 S : S l T : V>H
HI4J3KLM!! F c j rw I1TP4Jw1I SK rw UJM` !! F c` !! r cVW
(/&.(+ IH
J
A ecnn nr (+$ &)("' xo+n+e nonynn+r 3.666667 nmec+o 3? Hpeo6pasyn+e *+& no
B-#'& onepa+opom npeo6pasonann +nna. Hynrn +nn ykasrnae+c n kpyrnrx cko6kax
cnena o+ nepemennon, ko+opym nyno npeo6pasona+r. Hpeo6pasyn+e enn+enr k +nny
B-#'& nepe nrnonnennem onepannn:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7CS : S l T : V k $G+C9 S : S l T : V>H
A(*+&B7CXX @ S k $ (/6'*+$/( #B $ G+C9 XX @ S9 XX S>H
HI4J3KLM!! F c"b j rKUJM` !! F LKiSP3VcVW
(/&.(+ IH
J
Fnana 6. Hucna
________________________________________________________________________________________________
48
Tenepr nmec+o nenonncnennoro enenn 6ye+ nrnonn+rc enenne
nemec+nennoe, n nr nonynn+e 3.666667. Banomnn+e npannno: onepa+op / nrnonne+
nenonncnennoe enenne +onrko n +om cnynae, ecnn n ennmoe n enn+enr o+noc+c k
nenomy +nny. Ecnn xo+ 6r ono ns nyx nncen nne+c nemec+nennrm, +o nmec+o
nenonncnennoro enenn nrnonne+c nemec+nennoe.
Cokpamennan sannc
Bce onepa+opr, paccmo+pennre namn o nac+omero momen+a, anann nonrn pesynr+a+.
Hanpnmep, n+o6r ynennnn+r nepemennym x na 1, nr ncnonrsye+e onepa+op + n sanocn+e
pesynr+a+ o6pa+no n x:
*+& d k TH
d k d l XH @@ ujuwj njsj{ {qug R
Hporpammnc+r C nrnonnm+ noo6nre onepannn +ak nac+o, n+o n nnx 6rnn
cosanr cnennanrnre onepa+opr, nsmenmmne snanenne nepemennon 6es nnmnero
npncnannann. Hanpnmep, n ynennnenn snanenn x na 1 mono ncnonrsona+r
'0$&)#'& !"/&$3$"#) (++):
*+& d k TH
dllH @@ujuwj njsj{ {qug R
Take cymec+nye+ '0$&)#'& ,$/&$3$"#) (--), ymenrmammnn snanenne na 1:
*+& d k TH
dUUH @@ujuwj njsj{ {qug ]
A ecnn no+pe6ye+c ynennnn+r x ne na 1, a na 5? Konenno, mono ncnonrsona+r
cnoenne c nocneymmnm npncnannannem:
*+& k TH
k l TH @@ ujuwj {qug XI
Ho n n .+on onepannn +oe cymec+nye+ cokpamenna sanncr:
*+& d k TH
d lk TH @@ *2 XI
B+opa c+poka nn+ae+c kak npncnon+r x snanenne x+5. Hapy c onepa+opom
+= +ake cymec+nym+ onepa+opr -=,*=, /= n %=.
n nrnncnenn moyn (a6conm+non nennnnnr) nenoro nncna nmec+o onepa+opa
ncnonrsye+c qynknn 'M2(). Ecnn no+pe6ye+c nrnncnn+r moynr snanenn +nna -#+8,
ncnonrsyn+e qynknnm -'M2(). O6e qynknnn o6rnnm+c n qanne stdlib.h:
)*+,-.$/ 12&$*#345
4J2iaw1 Q3wi4/"R
Fnana 6. Hucna
________________________________________________________________________________________________
49
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7CS : S l T : V k $G+C9 S : S l T : V>H
A(*+&B7CXX @ S k $ (/6'*+$/( #B $ G+C9 XX @ S9 XX S>H
A(*+&B7CXX @ S3I k BG+C9 XX @ 7B-#'&>S>H
HI4J3KLMoR1 P/QSia31 5Pia1 SK 6s 4Q rwUJM` P/QL6sVVW
(/&.(+ IH
J
Bemecvnennme uncna
n xpanenn nncna c po6non nac+rm (nanpnmep, 3.2) ncnonrsym+c nemec+nennre
+nnr (+ake nasrnaemre +nnamn c nnanammen san+on). O6rnno nporpammnc+r
paccma+pnnam+ nemec+nennre nncna kak man+nccy, ymnoennym na 10 n c+enenn
nenonncnennon .kcnonen+r. Hanpnmep, nncno 345.32 paccma+pnnae+c kak 3.4532 x 10.
Co6c+nenno, nmenno +ak .+n nncna xpan+c: n 32-paspnom nemec+nennom nncne 8
paspon nrenm+c n xpanenn .kcnonen+r (nenoe co snakom), a 23 paspa
nrenm+c n xpanenn man+nccr; eme onn pasp ncnonrsye+c n xpanenn snaka.
Bemec+nennre nncna, kak n nenre, cymec+nym+ n neckonrknx pasnonnnoc+x. B
o+nnnne o+ nenrx, nemec+nennre nncna ncera nmem+ snak
B-#'& 8H @@ SVU{{vpugj wftg
$#.M-/ 4H @@ R]U{{vpugj wftg
-#+8 $#.M-/ *H @@ XVWU{{vpugj wftg
3anonnnvenn (nn nmno(a nemecvnennmx uncen
dynknn A(*+&B7> +ake moe+ nrnon+r nemec+nennre snanenn, n
npec+annenn ko+oprx name ncero ncnonrsym+c sanonnn+enn B n j. Bknmnn+e
onepannn nrnoa nemec+nennrx nncen n qann main.c:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
wSa/i1 q j !Ccks"W
HI4J3KLMq 4Q rKUJM` qVW
HI4J3KLMq 4Q r1UJM` qVW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Ona nrnon+ cneymmnn pesynr+a+:
_ *2 XVS]T3RYWII
_ *2 X3VS]TRW/lI]
Fnana 6. Hucna
________________________________________________________________________________________________
50
Taknm o6pasom, sanonnn+enr B ncnonrsye+ npn nrnoe o6rnnym ec+nnnym
sanncr, a sanonnn+enr j - .kcnonennnanrnym sanncr.
O6pa+n+e nnnmanne: B n nac+omee npem nrnon+ 6 nnqp n po6non nac+n.
Hac+o .+oro okasrnae+c cnnmkom mnoro. H+o6r orpannnn+r nrno nym nnqpamn,
cnerka nsmenn+e sanonnn+enr:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( :'(8;<=>
?
$g.M-j x k XVS]T3RYWH
A(*+&B7C_ *2 3VBG+C9 x>H
A(*+&B7C_ *2 3VjGsC9 x>H
(/&.(+ IH
J
Pesynr+a+ nrrnn+ +ak:
x *2 XVS]T3RW
*2 X3VSjlI]
+ynknnn (nn pa6ovm c nemecvnennmun uncnaun
Onepa+opr +, -, * n / pa6o+am+ +ak, kak n cneonano ona+r; nnnero oco6ennoro o nnx
ckasa+r nenrs. Ecnn nama nporpamma nrnonne+ mnoro nemec+nennrx nrnncnennn, nam
npnron+c ma+ema+nnecka 6n6nno+eka. H+o6r osnakomn+rc c ee nosmonoc+mn,
o+kpon+e na Mac okno +epmnnana n nnen+e komany 6'+ 6'&4. Br nonynn+e o+nnnnym
cnoky ncex nosmonoc+en ma+ema+nneckon 6n6nno+ekn: +pnronome+pn, nosneenne n
c+enenr, knapa+nre n ky6nneckne kopnn n.+..
Ecnn nr saxo+n+e ncnonrsona+r kakne-nn6o ma+ema+nneckne qynknnn n cnoem
koe, ne sa6yr+e nknmnn+r sarononok ma+ema+nneckon 6n6nno+ekn n nanano qanna:
Hpeynpeenne: nce +pnronome+pnneckne qynknnn pa6o+am+ c pananamn, a ne
c rpaycamn!
)*+,-.$/ 16'&4345
npawnenne
Hcnonrsyn+e ma+ema+nneckym 6n6nno+eky! Bknmnn+e n main.c ko, ko+oprn nrnon+
cnnyc 1 panana. Hncno onno nrnon+rc c okpyrnennem o +pex nnqp B po6non
nac+n (nockaska: n pesynr+a+e onno nonynn+rc I3W]X).
Fnana 6. Hucna
________________________________________________________________________________________________
51
2
" &*Z%
Cosan+e n Xcode nonrn npoek+ komannon c+pokn C c nmenem ^##-+/22. Hepna
nporpamma. ko+opym nanncan n cnoen nsnn, nrnonna qpasy Aapon kpy+ (mne
+ora 6rno 10 ne+). anan+e nannmem ee:
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
HI4J3KLMPISJ 4Q 7SSiUJMVW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
anan+e npenononm, n+o n nymen ynepennoc+n n co6c+nennon kpy+nsne moe
10-ne+nee B enae+, n+o6r qpasa nrnonnacr ne onn pas, a nenym mnny. Kak .+o
cena+r?
Ec+r +ynoe pemenne:
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
A(*+&B7C!'(#+ *2 ^##-G+C>H
(/&.(+ IH
J
A ec+r pemenne ymnoe - cosa+r nnkn.
Hnkn while
Hame snakomc+no c nnknamn nanne+c c nnkna L4*-/. Konc+pyknn L4*-/ o+anenno
nanomnnae+ konc+pyknnm *B, ko+opa paccma+pnnanacr n rnane 4: ona +oe coc+on+ ns
nrpaenn n 6noka koa, saknmnennoro n qnrypnre cko6kn. n konc+pyknnn *B n cnynae
Fnana 7. Uuknm
________________________________________________________________________________________________
52
nc+nnnoc+n nrpaenn 6nok koa nrnonne+c +onrko onn pas. B konc+pyknnn L4*-/
6nok nrnonne+c cnona n cnona, noka nrpaenne ne c+ane+ panno B'-2/.
Pnc. 7.1. Hnkn while
Hepennmn+e qynknnm 6'*+7> n cneymmem nne:
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
4J3 4 j bW
OR4i1 L4 !CV z
HI4J3KLMrw" PISJ 4Q 7SSiUJM` 4VW
4ttW |
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy
Ycnonne (i < 12) nponepe+c nepe karm nrnonnennem 6noka. Kak +onrko ono
okae+c nonrm, ynpannenne nepeae+c cneymmen komane nocne 6noka.
O6pa+n+e nnnmanne na ynennnenne i no n+opon c+poke 6noka. 3+o onenr nanrn
momen+: 6es ynennnenn nepemennon nnkn 6ye+ nrnonn+rc 6eckonenno, no+omy n+o
nrpaenne ncera 6ye+ oc+ana+rc nc+nnnrm. Ha pnc. 7.1 nso6paena 6nok-cxema
.+oro nnkna L4*-/.
Hnkn for
Hnkn L4*-/ - o6o6menna c+pyk+ypa, no nporpammnc+r C nac+o ncnonrsym+ nnknr n
cnonx nporpammax no onon c+anap+non cxeme:
!"!#!$%!&$#!'
)*+,- ./0%12!34 5
617
Fnana 7. Uuknm
________________________________________________________________________________________________
53
810%37"!9 :$;
<
n nee n srke C 6rna onpeenena cnennanrna cokpamenna sanncr: nnkn B#(.
B nnkne B#( npnneenna nrme cxema sanncrnae+c +ak:
=>? .!"!#!$%!&$#!'@ /0%12!3@ 810%37"!9 :$;4 5
617@
<
Hsmenn+e nporpammy +ak, n+o6r n nen ncnonrsonanc nnkn Bg(:
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
KSI L4J3 4 j bW 4 !CW 4ttV z
HI4J3KLMrw" PISJ 4Q 7SSiUJM` 4VW
|
(/&.(+ IH
J
Pnc. 7.2. Hnkn for
B .+om npoc+om npnmepe nnkn onpeene+, ckonrko pas onna 6r+r nrnonnena
neko+opa onepann. Onako na npak+nke nnknr name ncnonrsym+c n nepe6opa
konneknnn .nemen+on - ckaem, cnncka nmen, onyc+nm, 6r mor nsmenn+r .+y
nporpammy +ak, n+o6r nnkn nmec+o non+opnoro nrnoa onoro nmenn nepe6npan cnncok
nmen monx pysen. Hpn kaom npoxoe nnkn coo6man 6r o kpy+nsne onepenoro pyra.
Tema ncnonrsonann nnknon c konneknnmn 6ye+ paccma+pnna+rc, nannna c rnanr 15.
break
Hnora 6rnae+ nyno npepna+r nrnonnenne nnkna nsny+pn. Hpenononm, mr xo+nm
nepe6pa+r nce nonon+enrnre nncna n nonckax +akoro nncna x, n ko+oporo
nrnonne+c ycnonne x + 90 = x. Hnan enc+nnn: nepe6npaem nenre nncna o+ 0 o 11 n
Fnana 7. Uuknm
________________________________________________________________________________________________
54
npn o6napyennn nynoro nncna npeprnaem nrnonnenne nnkna. Bnecn+e nsmenenn n
nporpammy:
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& *H
B#( 7* k IH * 1 XVH *ll> ?
HI4J3KLM7R12p4Jh 4 j rwUJM` 4VW
4K L4 t b jj 4 X 4V z
/I1PpW
|
|
HI4J3KLMoR1 PJQO1I 4Q rw"UJM` 4VW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Pesynr+a+ nrrnn+ +ak:
^4/,h*+8 * k I
^4/,h*+8 * k X
^4/,h*+8 * k V
^4/,h*+8 * k S
^4/,h*+8 * k ]
^4/,h*+8 * k T
^4/,h*+8 * k R
^4/,h*+8 * k Y
^4/,h*+8 * k W
^4/,h*+8 * k
^4/,h*+8 * k XI
Z4/ '+2L/( *2 XI3
H+ak, komana M(/'h nepeae+ ynpannenne n konen 6noka.
Pnc. 7.3, Komana break
Fnana 7. Uuknm
________________________________________________________________________________________________
55
continue
Hnora no npem nrnonnenn 6noka n nnkne nyno ckasa+r nporpamme. A +enepr
nponyc+n nce, n+o oc+anocr nrnonnn+r n 6noke, n nannnn cneymmnn npoxo. 3+a
saana pemae+c komanon ,#+&*+./. onyc+nm, nr +nepo ynepenr n +om, n+o n
nncen, kpa+nrx 3, ycnonne nnkora ne nrnonne+c. Kak ns6ea+r nanpacnon no+epn
npemenn na nx nponepky?
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& *H
B#( 7* k IH * 1 XVH *ll> ?
4K L4 r c jj bV z
2SJ34Ja1W
|
A(*+&B7C^4/,h*+8 * k $G+C9 *>H
*B 7* l I kk * : *> ?
M(/'hH J
J
A(*+&B7CZ4/ '+2L/( *2 $3G+C9 *>H
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
^4/,h*+8 * k X
^4/,h*+8 * k V
^4/,h*+8 * k ]
^4/,h*+8 * k T
^4/,h*+8 * k Y
^4/,h*+8 * k W
^4/,h*+8 * k XI
Z4/ '+2L/( *2 XI3
Pnc. 7.4. Komana continue
Fnana 7. Uuknm
________________________________________________________________________________________________
56
Hnkn do-while
Kpy+re napnn ne ncnonrsym+ nnkn $#UL4*-/, no n nonno+r kap+nnr cneye+
ynomny+r n ero. Hnkn $#UL4*-/ ne nponepe+ nrpaenne, noka 6nok ne 6ye+
nrnonnen. Taknm o6pasom, 6nok ncera 6ye+ nrnonnen xo+ 6r onn pas.
Ecnn nepennca+r ncxonym nporpammy +ak, n+o6r n nen ncnonrsonanc nnkn $#U
L4*-/, ona 6ye+ nrrne+r +ak:
?
4J3 4 j bW wS z
HI4J3KLMrw" PISJ 4Q 7SSiUJM` 4VW
4ttW
| OR4i1 L4 !cVW (/&.(+ IH
J
O6pa+n+e nnnmanne na sanepmammnn cnmnon H. eno n +om n+o, n o+nnnne o+
pyrnx nnknon, nnkn $#UL4*-/ npec+anne+ co6on ony nnnnym komany:
$# { n+o-+o enaem} L4*-/ ( noka ycnonne oc+ae+c nc+nnnrm );
A no+ kak nrrnn+ 6nok-cxema nnkna $#UL4*-/:
Pnc. 7.5. Hnkn do-while
npawnenne
Hannmn+e nporpammy, ko+opa cnn+ae+ n o6pa+nom nanpannennn o+ 99 o 0 nepes 3 n
nrnon+ kaoe nncno. Ecnn +ekymee nncno kpa+no 5, +o nporpamma +ake nrnon+
coo6menne `#.+$ #+/! Brno nporpammr onen nrrne+r npnmepno +ak:

R
S
Fnana 7. Uuknm
________________________________________________________________________________________________
57
I
`#.+$ g+jF
WY
333
I
`#.+$ g+jF
Fnana 7. Uuknm
________________________________________________________________________________________________
58
" l9<B) & ?*)_)(<Z&
B cymnoc+n, nam komnrm+ep coc+on+ ns nponeccopa n onepa+nnnon nam+n - orpomnoro
na6opa nepeknmna+enen, ko+opre mory+ nknmna+rc n nrknmna+rc nponeccopom.
Fonop+, n+o karn nepeknmna+enr xpann+ 1 6n+ nnqopmannn. Bnanenne 1 o6rnno
npec+anne+ nknmnennoe coc+onne, a snanenne 0 - nrknmnennoe
Bocemr nepeknmna+enen o6pasym+ onn .)4# nnqopmannn. Hponeccop moe+
nonynn+r coc+onne nepeknmna+enen, nrnonnn+r onepannn c 6n+amn n coxpann+r
pesynr+a+ n pyrom na6ope nepeknmna+enen. Hanpnmep, nponeccop moe+ nponn+a+r
6an+ ns onoro mec+a, nponn+a+r pyron 6an+ ns pyroro mec+a, cnon+r nponn+annre
snanenn n coxpann+r nonynennrn 6an+ n +pe+rem mec+e.
Pnc 8.1. Ham+r n nponeccop
Bce nenkn nam+n nponymeponanr. Homep 6an+a o6rnno nasrnae+c ero
),&$+'3. n kora penr ne+ o 32-paspnrx nnn 64-paspnrx nponeccopax, nmee+c n
nny nennnnna apecon, ncnonrsyemrx .+nmn nponeccopamn. 64-paspnrn nponeccop
cnoco6en pa6o+a+r c nam+rm namnoro, namnoro 6onrmero o6rema, nem 32-paspnrH.
Honyuenne a(pecon
Cosan+e n Xcode nonrn npoek+: nporpammy komannon c+pokn C c nmenem !$$(/22/2.
Apecom nepemennon nasrnae+c nomep nenkn nam+n, n ko+opon xpann+c
snanenne .+on nepemennon. n nonynenn apeca nepemennon ncnonrsye+c
onepa+op :
)*+,-.$/ 12&$*#345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
4J3 4 j !W
HI4J3KLM4 Q3SI1Q 43Q 5Pia1 P3 rHUJM` 4VW
(/&.(+ IH
J
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
59
O6pa+n+e nnnmanne na sanonnn+enr { - on ncnonrsye+c npn nrnoe apecon
nam+n. Hoc+pon+e n sanyc+n+e nporpammy pesynr+a+ 6ye+ nrrne+r npnmepno +ak:
* 2&#(/2 *&2 ;'-./ '& IdMBBBBYSW
Bnponem, nam komnrm+ep moe+ pasmec+n+r snanenne * no conepmenno nnomy
apecy. Apeca nam+n non+n ncera nrnon+c n mec+nana+epnnnom qopma+e.
Ha komnrm+epe nce annre xpan+c n nam+n, a cneona+enrno, nmem+ apec.
Hanpnmep, qynknn +oe xpann+c n nam+n, nannna c neko+oporo apeca. n nrnoa
nananrnoro apeca qynknnn oc+a+onno ykasa+r ee nm:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
A(*+&B7C* 2&#(/2 *&2 ;'-./ '& AG+C9 *>H
HI4J3KLM3R4Q KaJ234SJ Q3PI3Q P3 rHUJM` TP4JVW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
Xpanenne a(pecon n ykasavennx
A ecnn apec no+pe6ye+c coxpann+r y nepemennon? Konenno, mono nocnonrsona+rc
6es snakonon jtgwftjuug nepemennon noxomero pasmepa, no ecnn +nn
nepemennon 6ye+ ykasan 6onee +onno, komnnn+op nomoe+ nam n ykae+ na
nosmonre omn6kn R nporpamme. Hanpnmep, ecnn n nepemennon c nmenem A&( onen
xpann+rc apec, no ko+opomy coepn+c snanenne +nna B-#'&, ee o6rnnenne moe+
nrrne+r +ak
B-#'& :A&(H
B .+on c+poke mr coo6maem, n+o nepemenna A&( nne+c ykasa+enem na B-#'&.
B nen ne xpann+c snanenne +nna B-#'&: ona coepn+ apec, no ko+opomy moe+
xpann+rc snanenne B-#'&.
O6rnn+e nonym nepemennym '$$(/22BK, coepamym ykasa+enr na +nn *+&.
Hpncnon+e en apec *.
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
4J3 XPwwI1QQ.KN j 4W
HI4J3KLM4 Q3SI1Q 43Q 5Pia1 P3 rHUJM` PwwI1QQ.KNVW
A(*+&B7C&4*2 B.+,&*#+ 2&'(&2 '& AG+C9 6'*+>H (/&.(+ IH
J
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
60
Hoc+pon+e n sanyc+n+e nporpammy. B ee nonenennn ponnrm cne+om nnnero ne
nsmennnocr.
Cennac n npoc+o+r mr ncnonrsyem nenre nncna. Ho ecnn nac nn+epecye+,
n nero nynr ykasa+enn, nac o+nnnno nonnmam. Hepea+r nenoe snanenne,
npncnoennoe nepemennon, nnny+r ne cnonee, nem ee apec. Onako ckopo 6r nanne+e
pa6o+a+r c annrmn, ko+opre namnoro cnonee n 6onrme o+enrnrx nenrx nncen.
Hmenno .+nm +ak yo6nr apeca: mr ne ncera moem nepea+r konnm annrx, c
ko+oprmn pa6o+aem, no sa+o ncera moem nepea+r ),&$+, no ko+opomy .+n annre
nannnam+c n nam+n. A npn nannnnn apeca o6pa+n+rc k annrm ye necnono.
O6pamenne k (annmu no a(pecy
Ecnn nam nsnec+en apec annrx, n o6pamenn k camnm annrm mono
nocnonrsona+rc onepa+opom *. Cneymma nporpamma nrnon+ na konconr snanenne
nenonncnennon nepemennon, xpanmenc no apecy '$$(/22#BK:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
*+& :'$$(/22BK k *H
A(*+&B7C* 2&#(/2 *&2 ;'-./ '& AG+C9 '$$(/22BK>H
A(*+&B7C&4*2 B.+,&*#+ 2&'(&2 '& AG+C9 6'*+>H
HI4J3KLM3R1 4J3 Q3SI1w P3 PwwI1QQ.KN 4Q rwUJM` XPwwI1QQ.KNVW
(/&.(+ IH
J
O6pa+n+e nnnmanne: snesonka (*) secr ncnonrsye+c nym pasnrmn cnoco6amn.
Bo-nepnrx, nepemenna '$$(/22BK o6rnne+c c +nnom *+&:. Hnane ronop,
o6rnnema nepemenna nne+c ykasa+enem na o6nac+r nam+n, n ko+opon moe+
xpann+rc snanenne *+&.
Bo-n+oprx, snesonka ncnonrsye+c npn n+ennn snanenn *+&, ko+opoe xpann+c
no apecy, naxomemyc '$$(/22BK. Ykasa+enn +ake nnora nasrnam+c
++<1/)3!, a ncnonrsonanne ykasa+en n n+enn annrx no apecy - &)-<3$"'()"!$3
(dereferencing) ykasa+en.
Onepa+op * +ake moe+ ncnonrsona+rc n nenon nac+n komanr npncnannann
n coxpanenn annrx no ykasannomy apecy:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
*+& :'$$(/22BK k *H
A(*+&B7C* 2&#(/2 *&2 ;'-./ '& AG+C9 '$$(/22BK>H
XPwwI1QQ.KN j W
HI4J3KLMSO 4 4Q rwUJM` 4VW
(/&.(+ IH
J
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
61
Hoc+pon+e n sanyc+n+e nporpammy.
Ecnn nr eme ne o konna nonnmae+e, kak pa6o+am+ ykasa+enn, - ne oropnan+ecr.
B .+on knnre mr nponeem ononrno mnoro npemenn sa pa6o+on c ykasa+enmn, +ak n+o
nr eme ycnee+e ocnon+rc c nnmn.
A +enepr conepmnm +nnnnnym omn6ky nporpammnponann - y6epem * ns
ne+nep+on c+pokn 6'*+7>, n+o6r c+poka npnnna nn
'$$(/22BK k WH
Xcode nrae+ npeynpeenne o neconmec+nmom npeo6pasonannn nenoro nncna n
ykasa+enr npncnannannn '*+&' nmec+o '*+& *'.Hcnpanr+e omn6ky.
Pasuep n 6anvax
H+ak, +enepr mr snaem, n+o nce annre xpan+c n nam+n, n ymeem nonyna+r apec,
nannna c ko+oporo pasmemam+c annre. Ho kak onpeenn+r, ckonrko 6an+ sannmae+
+nn annrx?
n onpeenenn pasmepa +nna annrx ncnonrsye+c qynknn 2*/#B7>.
Hpnmep:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
*+& :'$$(/22BK k *H
A(*+&B7C* 2&#(/2 *&2 ;'-./ '& AG+C9 '$$(/22BK>H :'$$(/22BK k WH
A(*+&B7CP#L * *2 $G+C9 *>H
HI4J3KLMJ 4J3 4Q ra /q31QUJM` Q41SKL4J3VVW
HI4J3KLM HS4J31I 4Q ra /q31QUJM` Q41SKL4J3 XVVW
(/&.(+ IH
J
B nrsonax A(*+&B7> nc+penae+c nonrn sanonnn+enr .. dynknn 2*/#B7>
nosnpamae+ snanenne +nna 2*/e&, n nrnoa ko+oporo cneye+ ncnonrsona+r
o+nocn+enrno peknn sanonnn+enr ..
Hoc+pon+e n sanyc+n+e nporpammy. Ecnn pasmep ykasa+en coc+anne+ 4 6an+a,
nama nporpamma nrnonne+c n 32-paspnom penme. Ecnn e ykasa+enr sannmae+ 8
6an+, nporpamma nrnonne+c n 64-paspnom penme.
B aprymen+e qynknnn 2*/#B7> +ake moe+ nepeana+rc nepemenna, +ak n+o
npnneenna nrme nporpamma moe+ 6r+r sanncana n +akom nne:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
*+& * k XYH
*+& :'$$(/22BK k *H
A(*+&B7C* 2&#(/2 *&2 ;'-./ '& AG+C9 '$$(/22BK>H
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
62
:'$$(/22BK k WH
A(*+&B7CP#L * *2 $G+C9 *>H
A(*+&B7C!+ *+& *2 . M_&/2G+C9 2*/#B74>>H
A(*+&B7C! A#*+&/( *2 . M_&/2G+C9 2*/#B7PwwI1QQ.KN>>H
(/&.(+ IH
J
NULL
Hnora n nporpamme 6rnae+ nyno cosa+r ykasa+enr na nnn+o - +o ec+r nepemennym
n xpanenn apeca, ko+opa coepn+ snanenne, onosnanno nokasrnammee, n+o .+on
nepemennon eme ne nrna npncnoeno onpeenennoe snanenne, n .+on nenn
ncnonrsye+c snanenne PQcc:
B-#'& :6_b#*+&/(H
@@ jf sj{jjuug 6_b#*+&/( s{wfqwqjnfv ujuwj PQcc9
@@ sgpujj q uj xpjn fg{uju {jtur xonjt3
6_bg*+&/( k PQccH
H+o +akoe PQcc? Bcnomnn+e, n+o apec - ncero nnmr nncno. O6osnanennm PQcc
coo+ne+c+nye+ nynr. 3+o onenr yo6no n konc+pyknnx nna:
B-#'& :6_b#*+&/(H
@@ zj{jjuug 6_b#*+&/( rtg s{wfqgjug ujuwj~
*B 76_b#*+&/(> ?
@@ ujuwj 6_b#*+&/( gntwug gn PQcc
333 gnj f puurw9 u ogng{rj ffrtjnfv 6_b#*+&/( 333
J /-2/ ?
@@ ujuwj 6_b#*+&/( {qug PQcc
J
Hosnee, kora penr none+ o6 ykasa+enx na o6rek+r, nmec+o PQcc 6ye+
ncnonrsona+rc o6osnanenne +*-. 3+n o6osnanenn .knnnanen+nr, no nporpammnc+r
Objective-C ncnonrsym+ +*- n o6osnanenn apecon, no ko+oprm ne xpan+c
o6rek+r.
Xopomnn cvnn o6+nnnennn ykasavenen
O6rnnenne ykasa+en na B-#'& nrrnn+ npnmepno +ak:
B-#'& :A#L/(b&(H
Tak kak nepemenna o6rnne+c c +nnom ykasa+en na B-#'&, nosnnkae+
nckymenne ncnonrsona+r sanncr cneymmero nna:
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
63
B-#'&: A#L/(b&(H
Becr ne+ omn6kn, n komnnn+op nosnonn+ nam ncnonrsona+r +akym sanncr.
Onako xopomnn c+nnr nporpammnponann ne pekomenye+ noc+yna+r noo6nrm
o6pasom.
Honemy? Ho+omy n+o n onon c+poke mory+ coepa+rc o6rnnenn cpasy
neckonrknx nepemennrx. Hanpnmep, ecnn saxony o6rnn+r nepemennre x, y n z, .+o
mono cena+r +ak
B-#'& 9 x9 H
Kaa nepemenna o+nocn+c k +nny B-#'&.
Kak nr ymae+e, k kakomy +nny o+noc+c .+n nepemennre?
B-#'&: M9 fH
Cmpnpns! Hepemenna b npec+anne+ co6on ykasa+enr na B-#'&, a nepemenna c
- npoc+o B-#'&. Ecnn nr xo+n+e, n+o6r o6e nepemennre 6rnn ykasa+enmn, noc+anr+e
snak * nepe nmenem kaon ns nnx:
B-#'& :M9 :fH
Pasmemenne snaka * pom c nmenem nepemennon 6onee narnno nokasrnae+, n+o
nmen n nny nporpammnc+.
npawnennn
Hannmn+e nporpammy ko+opa nokasrnae+, ckonrko nam+n sannmae+ snanenne +nna
B-#'&.
Ha Mac +nn 24#(& npec+anne+ co6on 2-6an+onoe nenoe nncno, n ko+opom onn
6n+ saenc+nonan n xpanenn snaka. Kakoe nanmenrmee nncno moe+ 6r+r
npec+anneno .+on nepemennon? A nan6onrmee? B nepemennon +nna .+2*8+/$ 24#(&
mory+ xpann+rc +onrko ne o+pnna+enrnre nncna. Kakoe nan6onrmee nncno moe+
6r+r npec+anneno .+nm +nnom?
Fnana 8. Appeca u ykasa1enu
________________________________________________________________________________________________
64
" G<9<l)\) @; BB%Z*<
B srke C ec+r c+anap+na qynknn 6#$B7>, ko+opa nonynae+ snanenne $g.M-j
n nosnpamae+ nenym n po6nym nac+n nncna. Hanpnmep, y nncna 3,14 nena nac+r panna
3, a po6na - 0,14.
Brsrna qynknnm 6#$B7>, nr xo+n+e nonynn+r o6e nac+n nncna. Onako
qynknn C moe+ nosnpama+r +onrko ono snanenne. Kak qynknn 6#$B7>
nepne+ na nncna nmec+o onoro?
Hpn nrsone 6#$B7> cneye+ ykasa+r apec, no ko+opomy 6ye+ coxpaneno ono
ns nncen. A ecnn ronopn+r konkpe+no, qynknn nosnpamae+ po6nym nac+r nncna n
konnpye+ nenym nac+r no ykasannomy namn apecy. Cosan+e nonrn npoek+ - nporpammy
komannon c+pokn C c nmenem PBR.
O+peak+npyn+e coepnmoe main.c:
)*+,-.$/ 12&$*#345
4J2iaw1 TP3R"R
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
wSa/i1 H4 j c"!kW
wSa/i1 4J31h1I{PI3W
wSa/i1 KIP234SJ{PI3W
FF l9<B 4J31h1I{PI3 @<9<l)<(BE > )9,?A<+(<
KIP234SJ{PI3 j TSwKLH4` 4J31h1I{PI3VW
FF d@9<l<Z<+&< _+)\<+&E` [9)+E<,;BE > 4J31h1I{PI3
HI4J3KLM4J31h1I{PI3 j r"bK` KIP234SJ{PI3 j r"CKUJM` 4J31h1I{PI3`
KIP234SJ{PI3VW
(/&.(+ IH
J
Takon mexannsm nepeann annrx nasrnae+c 0$&$,)=$4 0' ++<1/$. Hnane
ronop, nr nepeae+e apec (ccrnky), a qynknn sanncrnae+ no nemy cnon annre.
Pnc. 9,1. Coc+onne c+eka npn nosnpa+e ns modf()
Fnana 9. Hepepaua no ccmnke
________________________________________________________________________________________________
65
Hpec+anr+e. n+o nr nrae+e nopynenn mnnonam. Br ronopn+e: Mne nynr
qo+orpaqnn mnnnc+pa qnnancon c ero nonon nopykon. Y c+a+yn anrena n napke
naxon+c +annnk - kopo+ka enesna +py6a. Kora qo+orpaqnn 6yy+ ro+onr,
cnepnn+e n oc+anr+e n +annnke. B sa6epy nx no n+opnnk
nocne o6ea.
dynknn 6#$B7> +oe pa6o+ae+ no npnnnnny +annnka. Br ronopn+e en, n+o
nyno cena+r n re oc+ann+r pesynr+a+r, n+o6r nr mornn nosnee sa6pa+r nx, kora
nam .+o 6ye+ yo6no. Tonrko nmec+o enesnon +py6r nr nepeae+e apec nam+n, no
ko+opomy qynknn onna oc+ann+r cnon pesynr+a+.
Hporpauunponanne qynknnn c nepe(auen apryuenvon no
ccmnke
Honoenne +onkn na nnockoc+n o6rnno onpeene+c n onon ns nyx cnc+em
koopnna+: ekap+onon nnn nonpnon. B ekap+onrx koopnna+ax sanncr (x,y)
o6osnanae+ cmemenne nnpano na x n nnepx na y o+ nanana koopnna+. B nonpnrx
koopnna+ax sanncr (&4/&', ('$*.2) o6osnanae+ nonopo+ nneno na &4/&' panan co
cmemennem na pacc+onne ('$*.2 o+ nanana koopnna+.
onyc+nm, nam no+pe6onanocr nannca+r qynknnm, npeo6pasymmym +onky ns
ekap+onon cnc+emr koopnna+ n nonpnym. Taka qynknn onna nonynn+r na
nemec+nennrx nncna n nepny+r na nemec+nennrx nncna.
Pnc. 9.2, Honpnre n ekap+onr koopnna+r
O6rnnenne qynknnn nrrnn+ npnmepno +ak:
;#*$ ,'(&/2*'+Zgb#-'(7B-#'& 9 B-#'& x9 B-#'& :(b&(9 B-#'& :&4/&'b&(>
H+ak, npn nrsone qynknnn nepeam+c snanenn n x. Kpome +oro, qynknn
nonynae+ apeca, no ko+oprm onnr 6r+r coxpanenr nrnncnennre snanenn
Fnana 9. Hepepaua no ccmnke
________________________________________________________________________________________________
66
('$*.2 n &4/&'.
Bknmnn+e qynknnm npeo6pasonann n nanano qanna main.c n nrsonn+e ee ns
6'*+7>:
)*+,-.$/ 12&$*#345
)*+,-.$/ 16'&4345
;#*$ ,'(&/2*'+Z#b#-'(7B-#'& 9 B-#'& x9 $g.M-j :(b&(9 $g.M-j :&4/&'A&(>
z
FF 9)l&?B B;[9)+E<(BE @; @<9<l)++;A? )l9<B?
XI{3I jQI3L X[t?X?VW
FF >%\&BZ<+&< 3R13PW
KiSP3 3R13PW
4K L[ jjb"bV z
4K L? jjb"bV z
3R13P j b"bW FF m;9A)Z]+; +<;@9<l<Z<++;< _+)\<+&<
|1iQ1 4K L?bVz
3R13P jN CW
|1iQ1z
3R13P j 6 }{NCW
|
|1iQ1z
3R13P j P3PJLqFVW
|
FF _+)\<+&< 3R13P B;[9)+E<(BE @; @<9<l)++;A? _+)\<+&
X3R13P{3I j 3R13PW
|
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
$g.M-j A* k S3X]H
$g.M-j *+&/8/(b'(&H
$g.M-j B(',&*#+b'(&H
@@ p{jf *+&/8/(b'(& sj{jpjnfv q {|xjunj
B(',&*#+b'(& k 6#$B7A*9 *+&/8/(b'(&>H
@@ gs{jpjtjuwj ujuwv9 fg{ujuug|g *+&/8/(b'(&
A(*+&B7C*+&/8/(b'(& k 3IB9 B(',&*#+b'(& k 3VBG+C9 *+&/8/(b'(&9 B(',&*#+b'(&>H
w;a/i< [ j "dW
w;a/i< ? jk"bW
w;a/i< IPw4aQW
w;a/i< PJhi1W
2PI31Q4PJoS{SiPIL` q` IPw4aQ` PJhi1VW
HI4J3KLMLr"CK` r"CKV /12ST1Q Lr"CK IPw4PJQ" r"CKVUJM` [` ?" IPw4aQ" PJhi1VW
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 9. Hepepaua no ccmnke
________________________________________________________________________________________________
67
Pnc. 9.3. Coc+onne c+eka npn nosnpa+e ns ,'(&/2*'+Z#b#-'(7>
Hs6eranve pasmuenonannn NULL
Hnora qynknn moe+ nonyna+r no ccrnke cpasy neckonrko snanennn, +ora kak nac
nn+epecye+ +onrko nac+r ns nnx. Kak ns6ea+r o6rnnenn ncex nepemennrx n
nepeann nx apecon, ko+opre nce panno ne 6yy+ ncnonrsona+rc? O6rnno nr
nepeae+e nmec+o apeca PQcc, ronop +em camrm qynknnn: 3+o konkpe+noe snanenne
mne ne nonao6n+c.
Ho .+o osnanae+, n+o nepe pasrmenonannem kaoro ykasa+en ncera cneye+
y6ea+rc n +om, n+o on ne panen PQcc. Bknmnn+e n qynknnm ,'(&/2*'+Z#b#-'(7>
cneymmne nponepkn:
;#*$ ,'(&/2*'+Z#b#-'(7B-#'& d9 B-#'& _9 $#.M-/ :(b&(9 $#.M-/ :&4/&'b&(>
?
FF u*)_)(<Z] I{3I ;(Z&\<+ ;( ^
4K LI{3IV z
FF )l&?B B;[9;+E<(BE @; @<9<l)++;A? )l9<B?
XI{3I j QI3L X t q X qVW
|
FF u*)_)(<Z] 3R13P{3I 9)><+ ^
4K L3R13P{3IV z
FF G9;@?B*)<A ;B()(;* m?+*n&&
I13aIJW
|
@@ ^'-,.-'&/ &4/&'
B-#'& &4/&'H
*B 7d kk I3I> ?
*B 7_ kk I3I> ?
&4/&' k I3IH
J /-2/ *B 7_ 5 I> ?
&4/&' k ebKeVH
@@ g{tug ujgs{jpjtjuugj ujuwj
J /-2/ ?
&4/&' k U ebKeVH
J
J /-2/ ?
333
Fnana 9. Hepepaua no ccmnke
________________________________________________________________________________________________
68
!b" -(9?*(?9%
Hepeko nosnnkae+ neo6xonmoc+r n cosannn nepemennon n xpanenn neckonrknx
nsanmocnsannrx qparmen+on annrx. onyc+nm, nr nnme+e nporpammy n
nrnncnenn nnekca maccr +ena. (H+o +akoe nnekc maccr +ena, nnn HMT? 3+o nec n
knnorpammax, pasenennrn na knapa+ poc+a n me+pax. Bnanenne HMT menrme 20
osnanae+, n+o nec +ena nne nopmr, a snanenn nrme 30 ykasrnam+ na ns6r+onnrn nec.
3+o kpanne ne+onna me+pnka n onenkn qnsnneckoro coc+onn, no n npnmepa no
nporpammnponannm ona noxon+.) n onncann nenoneka nam nonao6n+c snanenne
B-#'&, coepamee poc+ n me+pax, n snanenne +nna *+&, n ko+opom xpann+c nec n
knnorpammax.
n xpanenn .+nx annrx mr cosanm cnon co6c+nennrn +nn b/(2#+.
Hepemenna +nna b/(2#+ npec+anne+ co6on c+pyk+ypy c nym nonmn:
4/*84&K+/&/(2 +nna B-#'& n L/*84&K+*-#2 +nna *+&.
Cosan+e nonrn npoek+: nporpammy komannon c+pokn C c nmenem aK^'-,. Bknmnn+e
n main.c komanr cosann c+pyk+ypr, coepamen ncm neo6xonmym nnqopmannm o
nenoneke:
)*+,-.$/ 12&$*#345
FFd:]EB+<+&< B(9?*(?9% {1IQSJ
Q3Ia23 {1IQSJ z
KiSP3 R14hR3NJ}131IQW
4J3 O14hR3NJ4iSQW
|W
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
Q3Ia23 {1IQSJ H1IQSJW
H1IQSJ"O14hR3NJ4iSQ j W
H1IQSJ"R14hR3NJ}131IQ j !"W
HI4J3KLMH1IQSJ O14hRQ r4 p4iShIPTQUJM` H1IQSJ"O14hR3NJ4iSQVW
HI4J3KLMH1IQSJ 4Q r"CK T131IQ 3PiiUJM` H1IQSJ"R14hR3NJ}131IQVW
I13aIJ bW
J
O6pa+n+e nnnmanne na cnn+akcnc o6pamenn k nonm c+pyk+ypr - nm
c+pyk+ypr o+ene+c o+ nmenn non +onkon.
Ha pnc. 10.1 nokasano, kak nrrnn+ kap c+eka 6'*+7> nocne npncnannann
snanennn nonm c+pyk+ypr.
Pnc. 10.1. Kap nocne npncnannann
Fnana 10. C1pyk1ypm
________________________________________________________________________________________________
69
Kak npannno, o6rnnenne c+pyk+ypr ncnonrsye+c n nporpamme mnorokpa+no,
no.+omy n +nna c+pyk+yp o6rnno cosae+c &_A/$/B - ncenonnm n o6rnnenn
+nna, nosnonmmnn ncnonrsona+r ero kak o6rnnrn +nn annrx. Bnecn+e nsmenenn n
main.+, n+o6r n nporpamme cosananocr n ncnonrsonanocr onpeenenne &_A/$/B n
c+pyk+ypr b/(2#+:
)*+,-.$/ 12&$*#345
FF ;:eE>Z<+&< (&@) {1IQSJ
3qH1w1K Q3Ia23 z
KiSP3 R14hR3NJ}131IQW
4J3 O14hR3NJ4iSQW
| {1IQSJW
*+& 6'*+7*+& '(8,9 ,#+2& ?,4'( : '(8;<=>
?
{1IQSJ H1IQSJW
A/(2#+3L/*84&K+*-#2 k RH
A/(2#+34/*84&K+/&/(2 k X3WH
A(*+&B7CA/(2#+ L/*842 * h*-#8('62G+C9 A/(2#+3L/*84&-+*-#2>H
A(*+&B7CA/(2#+ *2 3VB 6/&/(2 &'--G+C9 A/(2#+34/*84&-+/&/(2>H
(/&.(+ IH
J
Hocne +oro kak n nporpamme nonn+c onpeenenne &_A/$/B, c+pyk+ypy b/(2#+
mono 6ye+ nepea+r pyron qynknnn. Bknmnn+e n nporpammy qynknnm c nmenem
M#$_'22K+$/d7>, ko+opa nonynae+ c+pyk+ypy b/(2#+ n aprymen+e n nrnncne+
HMT. Bknmnn+e nrson .+on qynknnn n 6'*+7>:
)*+,-.$/ 12&$*#345
@@ gvqtjuwj nws b/(2#+
&_A/$/B 2&(.,& ?
B-#'& 4/*84&-+/&/(2H
*+& L/*84&-+*-#2H
J b/(2#+H
KiSP3 /Swq}PQQNJw1L{1IQSJ 9V
z
I13aIJ H"O14hR3NJ4iSQFLH"R14hR3NJ}131IQ X H"R14hR3NJ}131IQVW
|
*+& 6'*+7*+& '(8,9 ,#+2&,4'( : '(8;<=>
?
b/(2#+ A/(2#+H
A/(2#+3L/*84&-+*-#2 k RH
A/(2#+34/*84&-+/&/(2 k X3WH
KiSP3 /T4 j /Swq}PQQNJw1LH1IQSJVW
HI4J3KLMH1IQSJ RPQ ) $N SK r"CKUJM` /T4VW
(/&.(+ IH
J
Fnana 10. C1pyk1ypm
________________________________________________________________________________________________
70
npawnenne
Hepnon c+pyk+ypon, c ko+opon c+onknync n nporpammnponannn, 6rna c+pyk+ypa &6,
ncnonrsyema c+anap+non 6n6nno+ekon C n xpanenn npemenn c pas6nnkon na
komnonen+r. C+pyk+ypa onpeene+c cneymmnm o6pasom:
2&(.,& &6?
*+& &6e2/,H @@ fjoxupr <IURI=
*+& &6e6*+H @@ wuxnr <IUT=
*+& &6e4#.(H @@ fr <IUVS=
*+& &6e6$'_H @@ pju jfv <XUSX=
*+& &6e6#+H @@ jfv <IUXX=
*+& &6e_/'(H @@ |gp gn XII
*+& &6eL$'_H @@ pju ujpjtw f qgfo{jfjuv <IUR=
*+& &6e_$'_H @@ pju |gp f X vuq{v <IUSRT=
*+& &6e*2$2&H @@ t| tjnuj|g q{jjuw
-#+8 &6e86&#BBH @: fjjuwj q fjoxup gn ^QZ :@
,4'( :&6e#+/H @: fgo{juwj fgqg|g sgvf :@
JH
dynknn &*6/7> nosnpamae+ konnnec+no cekyn o+ nanana 1970 roa no
Fpnnnnny. dynknn -#,'-&*6/e(7> nn+ae+ .+y nennnnny n sanonne+ c+pyk+ypy &6
coo+ne+c+nymmnmn snanennmn (+onnee, npn nrsone en nepeae+c apec konnnec+na
cekyn c 1970 roa n apec c+pyk+ypr &6). Taknm o6pasom, nonynenne +ekymero
npemenn n nne c+pyk+ypr &6 nponcxon+ +ak:
-#+8 2/,#+$2%*+,/XYI k &*6/7PQcc>H
A(*+&B7CK& 4'2 M//+ -$ 2/,#+$2 2*+,/ XYIG+C9 2/,#+$2%*+,/XYI>H
2&(.,& &6 +#LH
-#,'-&*6/e(72/,#+$2%*+,/XYI9 +#L>H
A(*+&B7CZ4/ &*6/ *2 $\$\$G+C9 +#L3&6e4#.(9 +#L3&6e6*+9 +#L3&6e2/,>H
Hannmn+e nporpammy, ko+opa onpeene+, kaka a+a (B qopma+e UU
) coo+ne+c+nye+ 4 mnnnnonam cekyn.
(Hockaska: &6e6#+ k I coo+ne+c+nye+ nnapm, no.+omy ne sa6yr+e npn6ann+r 1.
Take nknmnn+e saronononnrn qann 1&*6/3 45 n nanano nporpammr.)

Fnana 10. C1pyk1ypm
________________________________________________________________________________________________
71
!!" 8?\)
o cnx nop n cnonx nporpammax mr ncnonrsonann +onrko nam+r, nrenemym n kapax
c+eka. 3+a nam+r an+oma+nneckn nrene+c n nanane qynknnn n an+oma+nneckn
ocno6oae+c n konne. (Hs-sa .+oro yo6noro noneenn nokanrnre nepemennre nac+o
nasrnam+ )(#'3)#!=$+/!3! 0$&$3$""<3!.)
Onako n neko+oprx cn+yannx +pe6ye+c sapesepnnpona+r nnnnym
nocneona+enrnoc+r nam+n n ncnonrsonann n neckonrknx qynknnx. Hanpnmep,
ona qynknn moe+ nponn+a+r +ekc+onrn qann n nam+r, a nocne nee 6ye+ nrsnana
pyra qynknn, ko+opa nocnn+ae+ nce rnacnre 6yknr n nam+n, Kak npannno, nocne
sanepmenn pa6o+r c +ekc+om cneye+ coo6mn+r nporpamme. n+o sannmaemym nam+r
mono ncnonrsona+r n pyrnx nenen.
nnnnym nocneona+enrnoc+r 6an+on nporpammnc+r nac+o nasrnam+ .6>$&'3.
(Kc+a+n, o+cma n nponcxon+ +epmnn 6yqepnsann, ko+oprn nr nnn+e, noka
onae+ecr sarpyskn oc+a+onnoro konnnec+na 6an+on n sanycka onepenoro
nneoponnka c ko+om c Youtube.)
n nrenenn 6yqepa n nam+n ncnonrsye+c qynknn 6'--#,7>. Byqep
nrene+c ns o6nac+n nam+n, nasrnaemon /6=$4 (heap), ko+opa cymec+nye+ o+enrno
o+ c+eka. Banepmnn pa6o+y c 6yqepom, cneye+ nrsna+r qynknnm B(//7> n
ocno6oenn nam+n n ee nosnpamenn n kyny. onyc+nm, nam nonao6nnc 6nok
nam+n, oc+a+onnrn n xpanenn 1000 nncen +nna B-#'&.
)*+,-.$/ 12&$*#345
)*+,-.$/ 12&$-*M345 @@ 6'--#, '+$ B(// '(/ *+ 2&$-*M
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
@@ gvqtjuwj xonjtv
B-#'& :2&'(&Ba.BB/(H
@@ s{gf u qrpjtjuwj ngq w oxw
2&'(&Ba.BB/( k 6'--#,7XIII : 2*/#B7B-#'&>>H
@@ 333{gn f xj{g333
@@ gfqggypjuwj svnw ptv sgqng{ug|g wfsgtgquwv
B(//72&'(&Ba.BB/(>H
@@ rqj9 f oog|g p{jf uwutfv qrpjtjuur tgo
2&'(&Ba.BB/( k PQccH
(/&.(+ IH
J
Ykasa+enr 2&'(&Ba.BB/( ccrnae+c na nepnoe nemec+nennoe nncno n 6yqepe.
Fnana 11. Kyua
________________________________________________________________________________________________
72
Pnc. 11.1. Ykasa+enr na 6yqep, nrenennrn n kyne, xpann+c n c+eke
Ha .+on c+ann knnrn no srky C o6rnno noonry o6rcnm+, kak nponcxon+
n+enne n sanncr annrx n pasnnnnrx nosnnnx .+oro 6yqepa nemec+nennrx nncen.
Onako .+a knnra c+apae+c kak mono 6rc+pee nonec+n nac k pa6o+e c o6rek+amn,
no.+omy maccnnr C n apnqme+nneckne onepannn c ykasa+enmn o+knarnam+c na
6yymee.
dynknn 6'--#,7 > +ake moe+ ncnonrsona+rc n nrenenn nam+n no
c+pyk+ypy. Hanpnmep, ecnn nr xo+n+e nrenn+r n kyne nam+r n xpanenn c+pyk+ypr
b/(2#+, nporpamma moe+ nrrne+r +ak:
)*+,-.$/ 12&$*#345
)*+,-.$/ 12&$-*M345
&_A/$/B 2&(.,& ?
B-#'& 4/*84&K+/&/(2H
*+& L/*84&K+*-#2H
J b/(2#+H
B-#'& M#$_'22K+$/d7b/(2#+ :A>
?
(/&.(+ AU5L/*84&K+*-#2 @ 7AU54/*84&K+/&/(2 : AU54/*84&K+/&/(2>H
J
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
@@ qrpjtjuwj svnw ptv gpug fn{xonx{r b/(2#+
b/(2#+ :d k 7b/(2#+ :>6'--#,72*/#B7b/(2#+>>H
@@ s{wfqwquwj ujuw sgtv fn{xonx{r
dU5L/*84&K+*-#2 k WXH
dU54/*84&K+/&/(2 k V3IH
@@ qrqgp i
B-#'& daK k M#$_'22K+$/d7d>H
A(*+&B7Cd 4'2 ' aK #B k BG+C9 daK>H
@@ gfqggypjuwj svnw
B(//7d>H
@@ rqj utur p{jf
d k PQccH
(/&.(+ IH
J
Fnana 11. Kyua
________________________________________________________________________________________________
73
O6pa+n+e nnnmanne na onepa+op U5. Banncr AU5L/*84&K+*-#2 osnanae+:
Pasrmenona+r ykasa+enr { na c+pyk+ypy n o6pa+n+rc k nonm c nmenem
L/*84&K+*-#2.
He xpanenn c+pyk+yp n kyne o+kprnae+ maccy samena+enrnrx nosmonoc+en.
B nac+noc+n, ona saknarnae+ ocnony n pa6o+r c o6rek+amn Objective-C. 3+on +eme
6ye+ nocnmena cneymma nac+r knnrn.
Fnana 11. Kyua
________________________________________________________________________________________________
74
III
Objective-C n Foundation
Tenepr, kora nr nonnmae+e ocnonnre konnennnn nporpamm. qynknnn, nepemennrx n
+nnon annrx, mono nepexon+r k nsynennm srka Objective-C. Hoka mr 6yem
orpannnnna+rc nporpammamn komannon c+pokn, n+o6r ne o+nneka+rc o+ knmnenrx
acnek+on nporpammnponann.
Hporpammnponanne na srke Objective-C nepasprnno cnsano c Foundation
- 6n6nno+ekon knaccon, ncnonrsyemon n nanncann nporpamm. H+o +akoe knacc?
C .+oro mr n nannem ...
75
12. O6+ekvm
Bo mnornx srkax nporpammnponann noepnnae+c konnennn '.;$/#'(. O6rek+r,
kak n c+pyk+ypr, coepa+ neko+opre annre. Onako n o+nnnne o+ c+pyk+ypr, o6rek+
+ake coepn+ na6op qynknnn n pa6o+r c .+nmn an- nrmn. H+o6r nrsna+r +akym
qynknnm, cneye+ o+npann+r o6rek+y +''.@$"!$. B o6rek+non +epmnnonornn
qynknn, nrsrnaema npn nomomn coo6menn, nasrnae+c 3$#','3.
B nanane 1980-x roon Bp. Kokc n Tom han pemnnn nknmnn+r o6rek+no-
opnen+nponannre nosmonoc+n n srk C. Onn ncnonrsonann nem nrenenn nam+n n
kyne n ononnnnn ee cnn+akcncom o+npankn coo6mennn. Tak nonnnc srk Objective-
C.
O6rek+r no cnoen npnpoe onenr o6mn+enrnr. B xoe cnoen pa6o+r onn
noc+onno npnnnmam+ n o+npannm+ coo6menn, kacammnec ncero, n+o onn enam+.
Cnona nporpamma na srke Objective-C moe+ ononpemenno xpann+r n nam+n co+nn
o6rek+on, ko+opre pa6o+am+ n o+npannm+ coo6menn pyr pyry.
Knacc onncrnae+ konkpe+nym pasnonnnoc+r o6rek+on. B onncanne nknmnam+c
me+or n 0$&$3$""<$ A/-$301*&'(, n ko+oprx o6rek+r neko+oporo +nna xpan+ cnon
annre. Hporpammnc+ o6pamae+c k knaccy c sanpocom o cosannn o6rek+a ero +nna n
kyne. Honynennrn o6rek+ nasrnae+c A/-$301*&'3 .+oro knacca.
Hanpnmep, na iPhone yc+anonneno mnoec+no knaccon, onnm ns ko+oprx
nne+c knacc ^cc#,'&*#+. Br moe+e o6pa+n+rc k knaccy ^cc#,'&*#+ c sanpocom
na cosanne .ksemnnpa ^cc#,'&*#+. O6rek+ ^cc#,'&*#+ coepn+ neckonrko
nepemennrx n xpanenn reorpaqnnecknx annrx: mnpo+r, onro+r n nrco+r.
O6rek+ +ake coepn+ na6op me+oon. Hanpnmep, mono cnpocn+r y onoro
.ksemnnpa ^cc#,'&*#+, na kakom pacc+onnn on naxon+c o+ pyroro o6rek+a
^cc#,'&*#+.
Cos(anne n ncnonsonanne o6+ekvon
Hpnmno npem nannca+r namy nepnym nporpammy na Objective-C. Cosan+e nonrn
npoek+ komannon c+pokn (^#66'+$ c*+/ Z##-), no nmec+o +nna ^ nr6epn+e +nn
`#.+$'&*#+ (pnc. 12.1). Hpncnon+e npoek+y nm Z*6/!B&/(Z*6/.
dannr c koom Objective-C o6rnno nmem+ cyqqnkc .m. Hann+e n o+kpon+e
qann main.m, nnen+e cneymmne ne c+pokn koa:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
xyP31 XJSO j xyP31 wP31W
Fnana 12. OLek1m
________________________________________________________________________________________________
76
xShLMoR1 J1O wP31 i451Q P3 rHM` JSOVW
J
(/&.(+ IH
J
Pnc 12.1. Cosanne nporpammr komannon c+pokn `#.+$'&*#+

Hospannm name nepnoe coo6menne ycnemno o+npanneno! Br o+npannnn
coo6menne $'&/ knaccy P%O'&/. Me+o $'&/ npnkasrnae+ knaccy P%O'&/ cosa+r
.ksemnnp P%O'&/, nnnnnannsnpona+r ero +ekymen a+on/npemenem n nepny+r apec, c
ko+oporo pasmemae+c n nam+n nonrn o6rek+. Honynennrn apec oxpane+c n
nepemennon +#L. Coo+ne+c+nenno nepemenna nne+c ykasa+enem na o6rek+ P%O'&/.
P%c#87> qynknn Objective-C, o+nac+n nanomnnamma A(*+&B7>; ona +oe
nonynae+ qopma+nym c+poky, samene+ sanonnn+enn qak+nnecknmn snanennmn n
nrnon+ pesynr+a+ na konconr. Onako ee qopma+na c+poka ncera nannnae+c c
cnmnona n ee ne nyno sanepma+r cnmnonom nonon c+pokn G+.
Hoc+pon+e n sanyc+n+e nporpammy. Pesynr+a+ onen nrrne+r npnmepno +ak:

VIXXUIWUIT XX\TS\T]3SRR Z*6/!B&/(Z*6/<]WRV\YIY= Z4/ +/L $'&/ -*;/2 '&
IdXIIXX]$,I
B o+nnnne o+ A(*+&B7> qynknn P%c#87> nrnon+ nepe cnonmn annrmn
a+y, npem, nm nporpammr n nen+nqnka+op nponecca.
B anrnenmem, npnno pesynr+a+r nrsona P%c#87>, 6yy onycka+r .+n
cnye6nre annre c+pokn nonynam+c cnnmkom nnnnrmn.
B P%c#87> sanonnn+enr A nrnon+ apec o6rek+a. H+o6r pesynr+a+ 6onrme
nanomnnan a+y, ncnonrsyn+e sanonnn+enr , ko+oprn npnkasrnae+ o6rek+y nrnec+n
cnoe onncanne n nne c+pokn:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
Fnana 12. OLek1m
________________________________________________________________________________________________
77
?
'.&#(/-/'2/A##- ?
xyP31 XJSO j xyP31 wP31W
xShLMoR1 J1O wP31 i451Q P3 rHM` JSOVW
J
(/&.(+ IH
J
Honrn pesynr+a+ 6ye+ nrrne+r npnmepno +ak:
Z4/ $'&/ *2 VIXXUIWUIT XR\I\X] lIIII
Anavounn coo6mennn
O+npanka coo6menn ncera saknmnae+c n knapa+nre cko6kn n ncera coc+on+ ns
mnnnmym nyx nac+en:

ykasa+enr na o6rek+, nonynammnn coo6menne;

nm nrsrnaemoro me+oa.
O+npanka coo6menn (kak n nrson qynknnn) moe+ nme+r aprymen+r.
Paccmo+pnm npnmep.
O6rek+r P%O'&/ npec+annm+ konkpe+nym a+y n npem. 3ksemnnp P%O'&/
moe+ coo6mn+r pasnoc+r (n cekynax) mey a+on/npemenem, ko+opre npe- c+anne+
on cam, n npemenem 0:00 (no Fpnnnnny) 1 nnap 1970 roa. H+o6r nonynn+r .+y
nnqopmannm, cneye+ o+npann+r coo6menne &*6/K+&/(;'-%*+,/XYI o6rek+y
P%O'&/, na ko+oprn ccrnae+c ykasa+enr +#L.

)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%O'&/ :+#L k <P%O'&/ $'&/=H
P%c#87CZ4/ $'&/ *2 C9 +#L>H
wSa/i1 Q12SJwQ j JSO 34T1NJ31I5Pix4J21!bW
xShLMN3 RPQ /11J rK Q12SJwQ Q4J21 3R1 Q3PI3 SK !b"M` Q12SJwQVW

J
(/&.(+ IH
J
onyc+nm, nam nonao6nnc nonrn o6rek+ $'&/ coo+ne+c+nymmnn momen+y
npemenn na 100 000 cekyn nosnee ye cymec+nymmero. O+npannn .+o coo6menne
Fnana 12. OLek1m
________________________________________________________________________________________________
78
A3%36B1CD wv qrrqjg|g jngp
ncxonomy o6rek+y $'&/, nr nonynn+e nonrn o6rek+ $'&/. Me+o nonynae+ aprymen+:
nennnnny cmemenn n cekynax. Bocnonrsyemc nm n cosann nonoro o6rek+a $'&/
n qynknnn 6'*+7>:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%O'&/ :+#L k <P%O'&/ $'&/=H
P%c#87CZ4/ $'&/ *2 C9 +#L>H
$#.M-/ 2/,#+$2 k <+#L &*6/K+&/(;'-%*+,/XYI=H
P%c#87CK& 4'2 M//+ B 2/,#+$2 2*+,/ &4/ 2&'(& #B XYI3C9 2/,#+$2>H
xyP31 XiP31I j JSO wP31~qww4Jho4T1NJ31I5Pi!bbbbbW
xShLMNJ !bb`bbb Q12SJwQ 43 O4ii /1 rM` iP31IVW
J
(/&.(+ IH
J
B o+npannemom coo6mennn <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\XIIIII=:

+#L ykasa+enr na o6rek+, nonynammnn coo6menne (nonyna+enr);

$'&/a_!$$*+8Z*6/K+&/(;'- nm me+oa (cenek+op);

100 000 ennc+nennrn aprymen+.


<+#L $'&/a_!$$*+8Z*6/K+&/(;'-\XIIIII=
Pnc. 12.2. O+npanka coo6menn
O6+ekvm n naunvn
Ha pnc. 12.3 nso6paena narpamma o6rek+on. Ha nen nso6paenr na .ksemnnpa
P%O'&/ n kyne. ne nepemennre +#L n -'&/( nnm+c nac+rm kapa qynknnn
6'*+7>. Onn ykasrnam+ na o6rek+r P%O'&/ (cnsn nso6paenr c+penkamn).
Fnana 12. OLek1m
________________________________________________________________________________________________
79
E1%/F$B3%GD p{jf gjon9
ogng{gx gns{qtvjnfv fggjuwj
{|xjun
Hoka n+o mr nnenn +onrko onn knacc: P%O'&/, no n iOS n Mac OS X nxo+
co+nn knaccon. C camrmn pacnpoc+panennrmn ns nnx mr nosnakomnmc n cneymmnx
rnanax.
Pnc 12.3. narpamma o6rek+on npnnoenn Z*6/!B&/(Z*6/
id
Hpn o6rnnennn ykasa+en na o6rek+ o6rnno ykasrnae+c knacc o6rek+a, na ko+oprn
6ye+ ccrna+rc .+o+ ykasa+enr:
P%O'&/ :/dA*('&*#+H
Ho npem o+ npemenn 6rnae+ nyno cosa+r ykasa+enr, ne sna +onno, na kakon
o6rek+ on 6ye+ ccrna+rc. B +aknx cnynax ncnonrsye+c +nn *$, ko+oprn osnanae+
ykasa+enr na kakon-nn6o o6rek+ Objective-C. Hpnmep ncnonrsonann *$:
*$ $/-/8'&/H
O6pa+n+e nnnmanne na o+cy+c+nne snesonkn (:) n .+om o6rnnennn ona
nopasymenae+c npn nannnnn *$.
Ha nepnrx nopax ononrno +pyno ycnon+r konnennnn knaccon, o6rek+on,
coo6mennn n me+oon. He oropnan+ecr, ecnn nr nemnoro neynepenno nync+nye+e ce6
npn pa6o+e c o6rek+amn ner .+o +onrko nanano. Mr 6yem ncnonrsona+r .+n
konc+pyknnn cnona n cnona, n c karm pasom onn 6yy+ nrrne+r nce 6onee
ocmrcnenno.
npawnenne
Hcnonrsyn+e na .ksemnnpa P%O'&/ n nrnncnenn npoonn+enrnoc+n namen
nsnn n cekynax. Hockaska: nonrn o6rek+ $'&/ no saannomy roy, mecny n +. .
cosae+c cneymmnm o6pasom:
Fnana 12. OLek1m
________________________________________________________________________________________________
80
P%O'&/^#6A#+/+&2 :,#6A2 k <<P%O'&/^#6A#+/+&2 '--#,= *+*&=H
<,#6A2 2/&/'(\XR=H
<,#6A2 2/&#+&4\]=H
<,#6A2 2/&O'_\SI=H
<,#6A2 2/&D#.(\XS=H
<,#6A2 2/&*+.&/\XI=H
<,#6A2 2/&%/,#+$\I=H
P%^'-/+$'( :8 k <<P%^'-/+$'( '--#,=
*+*&E*&4^'-/+$'(K$/+&*B*/(\P%"(/8#(*'+^'-/+$'(=H
P%O'&/ :$'&/Ba*(&4 k <8 $'&/`(#6^#6A#+/+&2\,#6A2=H
n nonynenn konnnec+na cekyn mey nym .ksemnnpamn P%O'&/ cneye+
ncnonrsona+r me+o &*6/K+&/(;'-%*+,/O'&/:
$#.M-/ $ k <-'&/(O'&/ &*6/K+&/(;'-%*+,/O'&/\/'(-*/(O'&/=H
Fnana 12. OLek1m
________________________________________________________________________________________________
81
13. Coo6mennn
B rnane 12 mr o+npannnn neckonrko coo6mennn. Eme pas nocmo+pnm na c+pokn, n
ko+oprx o+npannnncr .+n coo6menn c nrsonamn coo+ne+c+nymmnx me+oon:
P%O'&/ :+#L k <P%O'&/ $'&/=H
Me+o $'&/ nne+c 3$#',)3 /1)++). Hnane ronop, n nrnonnenn .+oro
me+oa coo6menne o+npanne+c camomy knaccy P%O'&/. Me+o $'&/ nosnpamae+
ykasa+enr na .ksemnnp P%O'&/.
$#.M-/ 2/,#+$2 k <+#L &*6/K+&/(;'-%*+,/XYI=H
Me+o &*6/K+&/(;'-%*+,/XYI o+nocn+c k 3$#',)3 A/-$301*&'(. n ero
nrnonnenn coo6menne o+npanne+c konkpe+nomy .ksemnnpy knacca. Me+o
&*6/K+&/(;'-%*+,/XYI nosnpamae+ snanenne +nna $#.M-/.
P%O'&/ :-'&/( k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\XIIIII=H
Me+o $'&/a_!$$*+8Z*6/K+&/(;'-: +ake nne+c me+oom .ksemnnpa. On
nonynae+ onn aprymen+, na n+o ykasrnae+ noe+onne nocne nmenn me+oa. Me+an
+ake nosnpamae+ ykasa+enr na .ksemnnp P%O'&/.
Bnowennan ovnpanka coo6mennn
Me+o knacca '--#, nosnpamae+ ykasa+enr na nonrn o6rek+, ko+oprn neo6xonmo
nnnnnannsnpona+r. 3+o+ ykasa+enr ncnonrsye+c n o+npankn nonomy o6rek+y
coo6menn *+*&. Hcnonrsonanne '--#, n *+*& - camrn pacnpoc+panennrn cnoco6
cosann o6rek+on n Objective-C. Hpannna xopomero C+nn nporpammnponann
pekomenym+ o+npann+r o6a coo6menn n onon c+poke koa nocpec+nom nnoenn:
<<P%O'&/ '--#,= *+*&=H
Cnc+ema cnanana nrnonne+ nny+pennne coo6menn, a sa+em nepexon+
k nnemnnm. Taknm o6pasom, cnanana knaccy P%O'&/ 6ye+ o+npanneno coo6menne
'--#,, a sa+em pesynr+a+ o+npankn (ykasa+enr na cosannrn .ksemnnp)
nonynn+ coo6menne *+*&.
Me+o *+*& nosnpamae+ ykasa+enr na nonrn o6rek+ (ko+oprn non+n ncera
connaae+ c ykasa+enem, nosnpamennrm me+oom '--#,), n+o nosnone+ ncnonrsona+r
Fnana 13. Coomeuun
________________________________________________________________________________________________
82
nosnpamennoe *+*& n komane npncnannann. Hponepr+e, kak pa6o+am+ nnoennre
coo6menn - samenn+e c+poky
P%O'&/ :+#L k <P%O'&/ $'&/=H
c+pokon
xyP31 XJSO j xyP31 PiiS2 4J43W
Mnowecvnennme apryuenvm
Heko+opre me+or nonynam+ neckonrko aprymen+on. Hanpnmep, o6rek+ P%O'&/ ne
snae+, kakomy nm mecna on coo+ne+c+nye+. n nonynenn .+on nnqopmannn cneye+
ncnonrsona+r o6rek+ P%^'X/+$'(. Me+o P%^'X/+$'(, ko+oprn coo6mae+ enr
mecna, nonynae+ +pn aprymen+a. Cosan+e o6rek+ P%^'X/+$'( n ncnonrsyn+e ero n
main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%O'&/ :+#L k <<P%O'&/ '--#,= *+*&=H
P%c#87CZ4/ $'&/ *2 C9 +#L>H
$#.M-/ 2/,#+$2 k <+#L &*6/K+&/(;'-%*+,/XYI=H
P%c#87CK& 4'2 M//+ B 2/,#+$2 2*+,/ &4/ 2&'(& #B XYI3C9 2/,#+$2>H
P%O'&/ :-'&/( k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\XIIIII=H
P%c#87CK+ XII9III 2/,#+$2 *& L*-- M/ C9 -'&/(>H
x7Pi1JwPI X2Pi j x7Pi1JwPI 2aII1J37Pi1JwPIW
xNJ31h1I wPq j 2Pi SIw4JPi43q.KJ43xyPq7Pi1JwPIJ43
4JJ43x}SJ3R7Pi1JwPIJ43
KSIyP31JSOW
xShLMoR4Q 4Q wPq ria SK 3R1 TSJ3RM` wPqVW
J
(/&.(+ IH
J
Coo6menne nasrnae+c #($*+'-*&_BQ+*&\*+Q+*&\B#(O'&/\ n nonynae+ +pn
aprymen+a. Ha .+o ykasrnam+ +pn noe+onn n nmenn coo6menn. O6pa+n+e nnnmanne
na pas6nnky o+npannemoro coo6menn no +pem c+pokam. Komnnn+opy .+o nnckonrko
ne memae+ - on nrnopnpye+ nnmnne npo6enr. Hporpammnc+r Objective-C o6rnno
nrpannnnam+ noe+onn +ak, n+o6r nac+n nmenn me+oa mono 6rno nerko o+nnnn+r o+
aprymen+on. (H Xcode nam n .+om nomoe+: karn pas, kora nr nannnae+e nonym
c+poky, nperyma c+poka 6ye+ cna6a+rc npannnrnrm o+c+ynom. Ecnn .+o ne
nponcxon+, nponepr+e nac+ponkn o+c+ynon n Xcode.)
Fnana 13. Coomeuun
________________________________________________________________________________________________
83
Hepnrn n n+opon aprymen+r me+oa coepa+ konc+an+r P%O'_^'-/+$'(Q+*&
n P%#+&4^'-/+$'(Q+*&. 3+n konc+an+r onpeenm+c n knacce P%^'X/+$'(. Onn
coo6mam+ me+oy, n+o nac nn+epecye+ enr mecna. Tpe+nn aprymen+ onpeene+ a+y, k
ko+opon o+nocn+c sanpamnnaema nnqopmann.
Ecnn 6r nmec+o n mecna nr saxo+enn ysna+r nac roa, +o npn nrsone
cneonano 6r ncnonrsona+r cneymmne konc+an+r:
P%QK+&/8/( 4#.( k <,'- #($*+'-*&_BQ+*&\P%D#.(^'-/+$'(Q+*&
*+Q+*&\P%/'(^'-/+$'(Q+*&
B#(O'&/\+#L=H
Ovnpanka coo6mennn nil
Hon+n no ncex o6rek+no-opnen+nponannrx srkax cymec+nye+ konnennn +*- -
ykasa+en, ne ccrnammeroc nn na kakon o6rek+. B Objective-C +*- npec+anne+
co6on nynenon ykasa+enr (.knnnanen+ PQcc ns rnanr 8).
B 6onrmnnc+ne o6rek+no-opnen+nponannrx srkon o+npanka coo6mennn +*-
neonyc+nma, no.+omy nepe o6pamennem k o6rek+y cneye+ nrnonnn+r nponepky na
+*-. Ho .+on npnnnne n nporpammax nac+o nc+penam+c konc+pyknnn cneymmero nna:
*B 7B*$# Fk +*-> ?
<B*$# 8#"/&Z4/P/L2A'A/(=H
J
B xoe paspa6o+kn Objective-C 6rno pemeno, n+o o+npanka coo6menn +*- 6ye+
onyc+nma; npoc+o npn .+om nnnero ne 6ye+ nponcxon+r. Coo+ne+c+nenno, cneymmnn
ko nnonne onyc+nm:
O#8 :B*$# k +*-H
<B*$# 8#"/&Z4/P/L2A'A/(=H
Banoe samenanne #1: ecnn npn o+npanke coo6menn nnnero ne nponcxon+,
y6en+ecr n +om, n+o coo6menn ne o+npannm+c ykasa+enm, ko+opomy 6rna
saano snanenne +*-.
Banoe samenanne #2: ecnn nr o+npanne+e coo6menne +*-, nosnpamaemoe
snanenne ne nece+ nonesnon nnqopmannn.
O#8 :B*$# k +*-H
P/L2A'A/( :$'*-_ k <B*$# 8#"/&Z4/P/L2A'A/(=H
B .+om cnynae snanenne $'*-_ 6ye+ panno nynm. (B o6mem cnynae, ecnn
pesynr+a+om nne+c nncno nnn ykasa+enr, npn o+npanke coo6menn nonyna+enm +*-
Fnana 13. Coomeuun
________________________________________________________________________________________________
84
nosnpamae+c nynr. Ho n pyrnx +nnon - nanpnmep, n c+pyk+yp - nosmono
c+pannre n neonannre nosnpamaemre snanenn.)
npawnenne
Ha namem Mac nmee+c knacc c nmenem P%Z*6/#+/. B nncno ero me+oon nxon+
me+o knacca 2_2&/6Z*6/#+/, ko+oprn nosnpamae+ naconon noc komnrm+epa (n nne
.ksemnnpa knacca P%i*6/#+/), n me+o .ksemnnpa *2O'_-*84&%';*+8Z*6/,
ko+oprn nosnpamae+ YES, ecnn n naconom noce n nac+omee npem enc+nye+ ne+nee
npem.
Hannmn+e nporpammy komannon c+pokn Foundation, ko+opa nponepe+,
enc+nye+ nn n nac+omnn momen+ ne+nee npem.
Fnana 13. Coomeuun
________________________________________________________________________________________________
85
!k" xx3I4Jh
P%%&(*+8 - eme onn ro+onrn knacc, kak n P%O'&/. B .ksemnnpax P%%&(*+8
xpan+c nocneona+enrnoc+n cnmnonon. B nporpammnom koe n cosann .ksemnnpa
P%%&(*+8 mono ncnonrsona+r sanncr nna
P%%&(*+8 :-'6/+& k CE4_ 6/F~CH
Bcnomnn+e, no n npoek+e Z*6/!B&/(Z*6/ nnonnc cneymmnn ko:
P%c#87CZ4/ $'&/ *2 C9 +#L>H
P%c#8( ) - qynknn Objective-C (ne me+o!), nnemne noxoa na A(*+&B().
Onako n P%c#8( ) qopma+na c+poka n enc+nn+enrnoc+n nne+c .ksemnnpom
P%%&(*+8.
3ksemnnpr P%%&(*+8 mono cosana+r na nporpammnom yponne npn nomomn
me+oa knacca 2&(*+8E*&4`#(6'&:
P%%&(*+8 :d k <P%%&(*+8 2&(*+8E*&4`#(6'&\CZ4/ M/2& +.6M/( *2 $C9 T=H
n nonynenn konnnec+na cnmnonon nc+poke ncnonrsye+c me+o length:
P%QK+&/8/( ,4'(^#.+& k <d -/+8&4=H
Me+o isEqual: nponepe+ na panenc+no coepnmoe nyx c+pok:.
*B 7<-'6/+& *2.'-\d=>
P%c#87C '+$ '(/ /.'-C9 -'6/+&9 d>H
B srke C +oe cymec+nye+ konc+pyknn n pa6o+r co c+pokamn cnmnonon. Bo+
kak nperymnn npnmep nrrnen 6r na srke C:
,4'( :-'6/+& k CE4_ 6/F~CH
,4'( :dH
'2A(*+&B7d9 CZ4/ M/2& +.6M/( *2 $C9 T>H
2*/e& ,4'(^#.+& k 2&(-/+7d>H
*B 72&(,6A7-'6/+&9 d> kk I>
A(*+&B7C2 '+$ 2 '(/ /.'-G+C9 -'6/+&9 d>H
B(//7d>H
C+pokn C 6yy+ paccmo+penr n rnane 34. Tam, re .+o nosmono, ncnonrsyn+e
o6rek+r P%%&(*+8 nmec+o c+pok C. Knacc P%%&(*+8 coepn+ mnoro me+oon,
ynpomammnx namy pa6o+y. Kpome +oro, knacc P%%&(*+8 (6asnpymmnnc na c+anap+e
Fnana 14. NSString
________________________________________________________________________________________________
86
Knnko) o+nnnno cnpanne+c c xpanennem +ekc+a na nm6om ns srkon namen nnane+r.
Hesnakomre cnmnonr? He+ npo6nem. Tekc+ nn+ae+c cnpana
naneno? Banpoc+o.
npawnenne
Knacc P%D#2& coepn+ nnqopmannm o namem komnrm+epe. n nonynenn
.ksemnnpa P%D#2& ncnonrsye+c me+o knacca P%D#2& c nmenem ,.((/+&D#2&. 3+o+
me+o nosnpamae+ ykasa+enr na o6rek+ P%D#2&:
+
7P%D#2& :>2aII1J3SQ3
n nonynenn nokannsonannoro nmenn komnrm+epa ncnonrsye+c me+o .ksemnnpa
NSHost c nmenem localizedName, nosnpamammnn ykasa+enr na o6rek+ P%%&(*+8:
-
7P%%&(*+8 :>iS2Pi41wPT1
Hannmn+e nporpammy komannon c+pokn Foundation, ko+opa nrnon+ nm namero
komnrm+epa. (Hporpamma nrnonne+c na ynnnenne onro.)
Fnana 14. NSString
________________________________________________________________________________________________
87
!s" xIIPq
Knacc P%!(('_, kak n P%%&(*+8, nac+o ncnonrsye+c nporpammnc+amn Objective-C. B
.ksemnnpe P%!(('_ xpann+c cnncok ykasa+enen na pyrne o6rek+r. Hpenononm,
nr xo+n+e cosa+r cnncok ns +pex o6rek+on P%O'&/, a no+om nepe6pa+r nx n nr nec+n
kaym ns xpanmnxc n nnx a+.
Cosan+e nonrn npoek+: nporpammy komannon c+pokn Foundation c nmenem
O'&/c*2&. O+kpon+e qann main.m n o+peak+npyn+e qynknnm 6'*+():
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj n{j gjongq P%O'&/
P%O'&/ :+#L k <P%O'&/ $'&/=H
P%O'&/ :&#6#((#L k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\V]3I : RI3I : RI3I=H
P%O'&/ :_/2&/($'_ k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\UV]3I : RI3I : RI3I=H
@@ fgpuwj ffwq9 fgpj{yj|g qfj n{w gjon
P%!(('_ :$'&/c*2& k <P%!(('_ '(('_E*&4M/,&2\+#L9 &#6#((#L9 _/2&/($'_9
+*-=H
@@ ogtog gjongq fgpj{ywn fswfgo~
P%c#87CZ4/(/ '(/ -. $'&/2C9 <$'&/c*2& ,#.+&=>H
@@ qrqgp fgpj{ywg|g s{r gjongq
P%c#87CZ4/ B*(2& $'&/ *2 C9 <$'&/c*2& #M/,&!&K+$/d\I=>H
P%c#87CZ4/ &4*($ $'&/ *2 C9 <$'&/c*2& #M/,&!&K+$/d\V=>H
J
(/&.(+ IH
J
Knacc P%!(('_ coepn+ na me+oa, npec+annennrx n .+om npnmepe,
ko+oprmn nr 6ye+e noc+onno nonrsona+rc:
,#.+&: nosnpamae+ konnnec+no .nemen+on n maccnne:
#M/,&!&-+$/d: nosnpamae+ ykasa+enr, xpanmnnc n maccnne no nnekcy,
saannomy n aprymen+e.
3nemen+r maccnna xpann+c n saannom nopke, k nnm mono o6pama+rc no
nnekcy. Hymepann nnekcon nannnae+c c nyn: nepnrn .nemen+ xpann+c c nnekcom
0, n+opon - c nnekcom 1 n +. . Taknm o6pasom, ecnn me+o ,#.+& coo6mae+, n+o maccnn
coepn+ 100 .nemen+on, nr moe+e ncnonrsona+r #M/,&!&-+$/d: n sanpoca
o6rek+on c nnekcamn o+ 0 o 99.
Ha pnc. 15.1 nso6paena narpamma o6rek+on nporpammr. O6pa+n+e nnnmanne:
.ksemnnp P%!(('_ coepn+ ykasa+enn na o6rek+r P%O'&/.
Fnana 15. NSArray
________________________________________________________________________________________________
88
Pnc. 15.1. narpamma o6rek+on npnnoenn DateList
n nepe6opa n noonepenon o6pa6o+kn ncex .nemen+on maccnna ncnonrsye+c
nnkn B#(. O+peak+npyn+e qann main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?

@@ fgpuwj n{j gjongq P%O'&/
P%O'&/ :+#L k <P%O'&/ $'&/=H
P%O'&/ :&#6#((#L k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\V]3I : RI3I : RI3I=H
P%O'&/ :_/2&/($'_ k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\UV]3I : RI3I : RI3I=H

@@ fgpuwj ffwq9 fgpj{yj|g qfj n{w gjon
@@ +*- U s{wuo ogu fswfo
P%!(('_ :$'&/c*2& k <P%!(('_ '(('_E*&4M/,&2\+#L9 &#6#((#L9 _/2&/($'_9
+*-=H

xNJ31h1I wP317SaJ3 j wP314Q3 2SaJ3W
KSI L4J3 4 j bW 4 wP317SaJ3W 4ttV z
xyP31 Xw j wP314Q3 S/01233NJw14W
xShLM1I1 4Q P wP31 rM` wVW
|
J
(/&.(+ IH
J
Hporpammnc+am +ak nac+o npnxon+c pema+r saany nepe6opa maccnnon, HTO
n nee 6rno cosano cnennanrnoe pacmnpenne nnkna B#( (cnn+akcnc .<+#&'%'
0$&$=!+1$"!*) Bnecn+e n ko cneymmne nsmenenn:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj n{j gjongq P%O'&/
P%O'&/ :+#L k <P%O'&/ $'&/=H
Fnana 15. NSArray
________________________________________________________________________________________________
89
P%O'&/ :&#6#((#L k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\V]3I : RI3I : RI3I=H
P%O'&/ :_/2&/($'_ k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\UV]3I : RI3I : RI3I=H
@@ fgpuwj ffwq9 fgpj{yj|g qfj n{w gjon
@@ +*- U s{wuo ogu fswfo
P%!(('_ :$'&/c*2& k <P%!(('_ '(('_E*&4M/,&2\+#L9 &#6#((#L9 _/2&/($'_9
+*-=H
KSI LxyP31 Xw 4J wP314Q3V z
xShLM1I1 4Q P wP31 rM` wVW
|
J
(/&.(+ IH
J
Hoo6nre nnknr nnm+c ncknmnn+enrno .qqek+nnnrm cnoco6om nepe6opa
.nemen+on maccnna. Cymec+nye+ ennc+nennoe orpannnenne: n nponecce nepe6opa
sanpemeno o6ann+r nnn yan+r .nemen+r ns maccnna.
NSMutableArray
Maccnnr en+c na ne pasnonnnoc+n:
3ksemnnp P%!(('_ cosac+c c konkpe+nrm cnnckom ykasa+enen. o6annenne
n yanenne ykasa+enen n +aknx maccnnax sanpemeno.
3ksemnnp P%.&'M-/!(('_ noxo na P%!(('_, no on noepnnae+ o6annenne n
yanenne ykasa+enen. (P%.&'M-/!(('_ nne+c +6./1)++'3 P%!(('_. Cy6knaccr
6onee nopo6no paccma+pnnam+c n rnane 18.)
Bnecn+e nsmenenn n nporpammy +ak, n+o6r n nen ncnonrsonanc .ksemnnp
P%.&'M-/!(('_ n me+or knacca P%.&'M-/!(('_ :
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj n{j gjongq P%O'&/
P%O'&/ :+#L k <P%O'&/ $'&/=H
P%O'&/ :&#6#((#L k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\V]3I : RI3I : RI3I=H
P%O'&/ :_/2&/($'_ k <+#L $'&/a_!$$*+8Z*6/K+&/(;'-\UV]3I : RI3I : RI3I=H
@@ fgpuwj sxfng|g ffwq
P%.&'M-/!(('_ :$'&/c*2& k <P%.&'M-/!(('_ '(('_=H
@@ qotjuwj pn q ffwq
<$'&/c*2& '$$M/,&\+#L=H
<$'&/c*2& '$$M/,&\&#6#((#L=H
Fnana 15. NSArray
________________________________________________________________________________________________
90
ptg999
Chapter 15NSArray
90
// Pu1 yes1erday a1 1he begnnng o1 1he 1s1
|da1eLs1 nser1b]ec1:yes1erday a1Index:6)
1oJ {hS0a1e *d 1h da1eL1s1) {
hSLog{_"heJe 1s a da1e. _", d),
}
// emove yes1erday
|da1eLs1 removeb]ec1A1Index:6)
N5Log{Q"NoW 1he 1rs1 da1e s Q", |da1eLs1 ob]ec1A1Index:6))
}
Je1uJh 0,
}

Create a new Foundation Command Line Tool named Groceries. Start by creating an empty
N5Nu1ab1eArray object. Then add several grocery-like items to the array. (You`ll have to create those,
too.) Finally, use fast enumeration to print out your grocery list.
This next challenge is, well, more challenging. Read through the following program, which finds
common proper names that contain two adjacent A`s.
#1mpoJ1 <Fouhda11oh/Fouhda11oh.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
_au1oJeeasepoo {
// Read 1h a 11e as a huge s1J1hg {1ghoJ1hg 1he poss1b111y o1 ah eJJoJ)
hSS1J1hg *hameS1J1hg
= |hSS1J1hg s1J1hgW11hCoh1eh1s01F1e._"/usJ/shaJe/d1c1/pJopeJhames"
ehcod1hg.hSbTF8S1J1hgEhcod1hg
eJJoJ.hbLL],
// BJeak 11 1h1o ah aJJay o1 s1J1hgs
hSAJJay *hames = |hameS1J1hg compoheh1sSepaJa1edByS1J1hg._"\h"],
// Co 1hJough 1he aJJay ohe s1J1hg a1 a 11me
1oJ {hSS1J1hg *h 1h hames) {
// Look 1oJ 1he s1J1hg "aa" 1h a case-1hsehs111ve mahheJ
hSRahge J = |h Jahge01S1J1hg._"AA" op11ohs.hSCaseThsehs111veSeaJch],
// Was 11 1ouhd?
11 {J.oca11oh != hSho1Fouhd) {
hSLog{_"_", h),
}
}
}
Je1uJh 0,
}
The file /usJ/shaJe/d1c1/pJopeJhames contains common proper names. The file/usJ/shaJe/d1c1/
woJds contains regular words proper names. Write a program based on the one above that finds
npawnennn
Cosan+e nonym nporpammy komannon c+pokn Foundation c nmenem "(#,/(*/2.
Cosan+e nyc+on o6rek+ P%.&'M-/!(('_, o6anr+e n nero neckonrko .nemen+on,
onncrnammnx nokynkn n marasnne. (3nemen+r +oe neo6xonmo cosa+r.) Hakonen,
ncnonrsyn+e cnn+akcnc 6rc+poro nepenncnenn n nrnoa cnncka nokynok.
Cneymma saana 6ye+ nocnonee. Hponn+an+e cneymmym nporpammy n
noncka pacnpoc+panennrx nmen co6c+nennrx, coepamnx ne cmenre 6yknr A:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ t wnjnfv q qwpj gpug gt}g fn{gow
@@ j g{gnow qggyur g}wgo
P%%&(*+8 :+'6/%&(*+8 k <P%%&(*+8 2&(*+8E*&4^#+&/+&2B`*-/\C@.2(@24'(/@
$*,&@A(#A/(+'6/2C /+,#$*+8\P%QZ`W%&(*+8+,#$*+8 /((#(\PQcc=H
@@ {wqj t u ffwq fn{go
P%!(('_ :+'6/2 k <+'6/%&(*+8 ,#6A#+/+&2%/A'('&/$a_%&(*+8\CG+C=H
@@ sj{jg{ fn{go9 fgpj{ywfv q ffwqj
B#( 7P%%&(*+8 :+ *+ +'6/2> ?
@@ sgwfo sgpfn{gow CC j xjn {j|wfn{ fwqgtgq
P%N'+8/ ( k <+ ('+8/B%&(*+8\ C!!C #A&*#+2\P%^'2/K+2/+2*&*;/ %/'(,4=H
@@ u}tw~
*B 7(3-#,'&*#+ Fk P%P#&`#.+$> ?
P%c#8 7CC9 s>H
JJJ
(/&.(+ IH
J
dann @.2(@24'(/@$*,&@A(#A/(+'6/2 coepn+ cnncok pacnpoc+panennrx
nmen co6c+nennrx, qann @.2(@24'(/@$*,&@L#($2 - cnncok cnon (+o ec+r nmen
Fnana 15. NSArray
________________________________________________________________________________________________
91
co6c+nennrx n napnna+enrnrx). Hannmn+e na 6ase npnneennon nporpammr pyrym -
n noncka nmen co6c+nennrx, ko+opre ononpemenno nnm+c napnna+enrnrmn
("-/+ - myckoe nm, n 8-/+ - yska nomnna). B qanne words nmena co6c+nennre
sanncanr c nponncnon 6yknr.
Hpn ynoponennn c+pok komnrm+ep o6rnno cnn+ae+, n+o cnmnonr nepxnero
pernc+pa npemec+nym+ cnmnonam nnnero pernc+pa. n cpannenn 6es yne+a pernc+pa
ncnonrsye+c me+o ,'2/-+2/+2*&*;/^#6A'(/:
P%%&(*+8 : k CCH
P%%&(*+8 : k CfCH
*B 7< ,'2/-+2/+2*&*;/^#6A'(/\M= kk P%($/(/$%'6/> ?
P%c#87C' '+$ |j /.'-C>H
J
*B 7< ,'2/-+2/+2*&*;/^#6A'(/\M= kk P%($/(/$!2,/+$*+8>?
P%c#87C' ,#6/2 M/B#(/ MC>H
J
*B 7< ,'2/-+2/+2*&*;/^#6A'(/\M= kk P%($/(/$O/2,/+$*+8>?
P%c#87CM ,#6/2 M/B#(/ C>H
J
Fnana 15. NSArray
________________________________________________________________________________________________
92
16. okyuenvannn
paspa6ovunka
B nperymen rnane npnnen neko+opre nonesnre cneenn o knacce P%!(('_. Kora
nr nanne+e nporpammnpona+r 6es moen nomomn (xo+ 6r npn nrnonnennn ynpanennn
ns .+on knnrn), nam npne+c ncka+r nnqopmannm camoc+o+enrno. B .+om nam
nomoe+ .nek+ponna okymen+ann o+ Apple.
H+o6r npocmo+pe+r okymen+annm n Xcode, nrsonn+e nanenr (8'+*/(
(menknn+e na knonke n npanon nepxnen nac+n okna Xcode) n o+kpon+e nknaky
O#f.6j+&'&*g+.
Pnc 16.1. 3nek+ponna okymen+ann
dnpma Apple npeoc+anne+ okymen+annm n+n nnon:

B0&)('="!/!: kpa+kne onncann ncex knaccon Objective-C n ncex qynknnn C.

C6/'(',+#() ! 0'+'.!* ,1* ")=!"):@!D: pykonoc+na coepa+ 6onee nopo6nym


nnqopmannm c konnen+yanrnon rpynnnponkon nen. Hanpnmep, n pykonoc+ne
nporpammnc+a no o6pa6o+ke omn6ok onncanr mnoronncnennre cnoco6r, ko+oprmn
paspa6o+nnkn Mac n iOS mory+ nonyna+r nnqopmannm o nosnnkmnx npo6nemax.
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
93

E&!3$&< /',): ne6onrmne sakonnennre npoek+r, ko+opre emonc+pnpym+, kak


qnpma Apple pekomenye+ ncnonrsona+r cnon +exnonornn. Hanpnmep, npoek+
E/'&4/('A nokasrnae+, kak cosana+r nonrsona+enrckne anno+annn npn
o+o6paennn kap+ n iOS.

F)3$=)"!* / #$/6@$4 ($&+!!: npn nrxoe kaon nonon nepcnn Mac OS nnn iOS
npnnarae+c okymen+ann, n ko+opon onncanr nsmenenn +ekymen nepcnn no
cpannennm c nperymen.

G$D"!=$+/!$ /'33$"#)&!!, +#)#9!, +'($#< 0' 0&'%&)33!&'()"!:, #$D"!=$+/!$


('0&'+< ! '#($#<: ne6onrmne okymen+r c onncannem konkpe+nrx npo6nem.
okymen+annm mono npocma+pnna+r, no ee o6rem onenr nennk. Hame
okymen+ann ncnonrsye+c n noncka o+ne+on na konkpe+nre nonpocr.
Cnpanounme cvpannnm
Eenknn+e na knonke n nepxnen nac+n nenon nanenn (8+*j|, n+o6r nrsna+r
nanenr noncka. Bnen+e ycnonne noncka P%!(('_; nne nonnm+c pesynr+a+r
noncka. B pasene N/B/(/+,/ menknn+e na ccrnke P%!(('_, n+o6r npocmo+pe+r
+0&)('="6: +#&)"!76 knacca P%!(('_.
Tpyno nepeonenn+r nanoc+r cnpanonnrx c+pannn n nac n n nporpammnc+on
ncex yponnen. dnpma Apple ocnona+enrno no+pynnacr na cosannem orpomnrx
6n6nno+ek nporpammnoro koa, ynpomammnx namy pa6o+y. Ocnonnrm nc+onnnkom
nnqopmannn o6 .+nx 6n6nno+ekax nnm+c cnpanonnre c+pannnr. He anen+e
npemenn na npocmo+p cnpanonnrx c+pannn nonrx knaccon, c ko+oprmn nr
nosnakomn+ecr n .+on knnre, n osnakomr+ecr c nx nosmonoc+mn. Take nosmoen
nonck no nmenam me+oon, qynknnn, konc+an+ n cnonc+n (o +om, n+o +akoe cnonc+na,
6ye+ pacckasano n rnane 17). Hem ynepennee nr 6ye+e pa6o+a+r co cnpanonnrmn
c+pannnamn, +em 6rc+pee none+ nama pa6o+a.
B nanane cnpanonnoro onncann knacca P%!(('_ npn no+c neko+opre
o6mne cneenn o knacce. B nac+noc+n, knacc nne+c nponsnonrm o+ P%M/,&
(nopo6noc+n +ake npn no+c n rnane 18). On noepnnae+ neko+opre npo+okonr
(.+a +ema 6ye+ paccmo+pena n rnane 25). On nne+c nac+rm 6n6nno+ekn Foundation n
nxon+ n noc+anky ncex nepcnn Mac OS X n ncex nepcnn iOS.
B konne sarononka npnnon+c cnncok pykonoc+n c onncannmn knacca P%!(('_
n npnmepon koa, emonc+pnpymmnx ero. Eenknn+e na ccrnke Collections Programming
Gopics, n+o6r o+kpr+r pykonoc+no no pa6o+e c konneknnmn.
B .+om konkpe+nom pykonoc+ne o6cyae+c pa6o+a c knaccamn konneknnn
(nknmna P%!(('_ n P%.&M-j((x). B o+nnnne o+ cnpanonnnka no knaccam,
pykonoc+no nerko nn+ae+c, a ma+epnan nsnarae+c nornnno n nocneona+enrno.
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
94
ptg999
Chapter 16Deveoper Documentaton
94
Sample Code: These are small, complete projects that demonstrate how Apple expects its technology
to be used. For example, there is a WeatherMap project that shows how to make custom annotations
on iOS map views.
Release Notes: Every time a new version of Mac OS or iOS comes out, it is accompanied by a set of
release notes. The release notes tell developers what has changed since the last version.
Technical notes, Articles, Coding How-to's, and Technical Q&As,: These are bite-sized documents
that address specific issues.
You can browse the documentation, but there is a lot of it. More commonly, you will access the
documentation via search.
Reference pages
At the top of the lefthand pane in Organizer, click the button to bring up the search panel. Type
NSArray into the search field, and the search results will appear below the search field. In the
Reference section, click on NSArray to see the reference page for .
Fgure 16.2NSArray reference page
It would be difficult to overstate how important these reference pages will be to you and how important
they are for programmers of all levels. Apple has worked really hard to create huge libraries of code
for your convenience. You will learn about these libraries primarily through reference pages. As you
go through this book, take a moment to look up the reference pages for new classes as you use them
Pnc. 16.2. Cnpanonna c+pannna NSArray
Hamn+e knonky nosnpa+a n nepxnen nac+n nanenn okymen+a, n+o6r nepny+rc k
cnpanonnon c+pannne P%!(('_. A kakne eme coo6menn mono o+npann+r P%!(('_?
Hpo kpy+n+e cnpanonnym c+pannny n nann+e cnncok me+oon, peannsyemrx P%!(('_3
onyc+nm, kak ysna+r, coepn+ nn maccnn saannrn .nemen+? Hann+e me+o
,#+&'*+2M/,&: n nponn+an+e ero onncanne.
Pnc. 16.3. okymen+ann no pa6o+e c konneknnmn
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
95
Pnc 16.4. Cnpanonna nnqopmann o containsObject:
H +enepr nr snae+e nce neo6xonmoe n o+npankn .+oro coo6menn n cnoem
nporpammnom koe.
Bmcvpan cnpanka
Onako cymec+nye+ n pyron, 6onee npoc+on cnoco6 nepexoa ns peak+opa,
n ko+opom nr nnme+e ko, k nc+onnnky snannn n okymen+annn. Bakpon+e nanenr
(8'+*j( n nepnn+ecr k npoek+y O'&/c*2&. Hann+e n qanne main.m c+poky c
qparmen+om <$'&/c*2& '$$M/,&\+#L=. Yepnna naa+on knannmy A&*#+,
menknn+e na '$$M/,&:. Ha .kpane nonne+c okno 6rc+pon cnpankn c nnqopmannen
o6 .+om me+oe.
O6pa+n+e nnnmanne na ccrnkn n okne 6rc+pon cnpankn. Ecnn menkny+r na
ccrnke, coo+ne+c+nymma okymen+ann o+kprnae+c n (8'+*/(. Yo6no, npana?
Ecnn nr xo+n+e, n+o6r okno 6rc+pon cnpankn oc+ananocr na .kpane noc+onno,
o+kpon+e ero n nne nanenn Xcode. n npanom nepxnem yrny okna Xcode nann+e
cermen+nponannrn .nemen+ ynpannenn */L, ko+oprn nrrnn+ +ak:
on npec+anne+ nenym, nnnmm n npanym nanenn Xcode. Bknmnenne/o+knmnenne
.+nx knonok npnnon+ k cokpr+nm/o+o6paennm coo+ne+c+nymmnx nanenen.
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
96
ptg999
Ouck Hep
97
Fgure 16.5Ouck Hep wndow
Notice that there are links in the Quick Help window. If you click a link, it will open the appropriate
documentation in the Organizer. Handy, right?
If you want to see that Quick Help all the time, you can open it as a pane in .
In the upper-right corner of the window, find a segmented control called that looks like
and indicates `s lefthand, bottom, and righthand panes. Selecting and de-selecting these
buttons will hide and show the respective panes.
The lefthand pane, which you`ve used plenty by now, is called the navigator. The bottom pane, which
you`ve also seen, includes the console and is called the debug area. The righthand pane is called
Utilities. Click the righthand button to reveal Utilities. At the top of Utilities, click the button
to reveal the Quick Help pane.
This pane will show help for the text selected in the editor. Try it: just select the word
'NSMutableArray in the editor:
Pnc. 16.5. Okno 6rc+pon cnpankn
hena nanenr, c ko+opon mr ye neonokpa+no pa6o+ann, nasrnae+c
nannra+opom. Hnn nanenr, ko+opa +oe nam nonaanacr, coepn+ konconr
n nasrnae+c o6nac+rm o+nakn. Hpana nanenr nasrnae+c cnye6non nanenrm
(Q&*-*&*/2). Eenknn+e na npanon knonke .nemen+a */L, n+o6r o+kpr+r nanenr
Q&*-*&*/2. B nepxnen nac+n nanenn menknn+e na knonke
ptg999
Ouck Hep
97
Fgure 16.5Ouck Hep wndow
Notice that there are links in the Quick Help window. If you click a link, it will open the appropriate
documentation in the Organizer. Handy, right?
If you want to see that Quick Help all the time, you can open it as a pane in .
In the upper-right corner of the window, find a segmented control called that looks like
and indicates `s lefthand, bottom, and righthand panes. Selecting and de-selecting these
buttons will hide and show the respective panes.
The lefthand pane, which you`ve used plenty by now, is called the navigator. The bottom pane, which
you`ve also seen, includes the console and is called the debug area. The righthand pane is called
Utilities. Click the righthand button to reveal Utilities. At the top of Utilities, click the button
to reveal the Quick Help pane.
This pane will show help for the text selected in the editor. Try it: just select the word
'NSMutableArray in the editor:
, n+o6r o+kpr+r nanenr
6rc+pon cnpankn.
Ha nanenn 6ye+ nrnon+rc cnpanka n +ekc+a, nr6pannoro n peak+ope.
Honpo6yn+e camn: npoc+o nr6epn+e n peak+ope cnono P%.&M-j(|x:
Pnc. 16.6. Hanenr 6rc+pon cnpankn
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
97
nn eusoea uoeo
nauenu
nn npocVo1pa
uc1po cnpaeku
Ecnn nrenn+r n peak+ope pyron +ekc+, nanenr 6rc+pon cnpankn nemenenno
o6nonn+c, n na nen nonn+c okymen+ann, o+nocmac k namemy nonomy nr6opy.
pyrne nosuownocvn n pecypcm
Ecnn cena+r nonnon mennok na nm6om +ekc+e c naa+on knannmen A&*#+,
(8'+*/( nponeen nonck .+on c+pokn.
Ecnn menkny+r na nm6om knacce, qynknnn nnn me+oe c naa+on knannmen
^#66'+$, Xcode o+kpoe+ qann, n ko+opom naxon+c coo+ne+c+nymmee o6rnnenne.
(Honpo6yn+e menkny+r c naa+on knannmen ^#66'+$ na cnone P%.&M-j((x).
Pnc. 16.7. O6rnnenne P%.&M-j((x
Ecnn nr6pannomy +ekc+y coo+ne+c+nye+ neckonrko o6rnnennn, npn mennke c
naa+on knannmen ^#66'+$ o+kpoe+c npemennoe okno. Hanpnmep, nonpo6yn+e
cena+r mennok c naa+on knannmen ^#66'+$ na '$$M/,&:.
Bpem o+ npemenn Xcode nponepe+, ne nonnnacr nn o6nonnenna okymen+ann.
Bce o6nonnenn an+oma+nneckn sarpyam+c n nnekcnpym+c n noncka. Take
mono nrnonnn+r nemenennym nponepky o6nonnennn nac+poek Xcode.
dnpma Apple nee+ neckonrko nckyccnonnrx paccrnok no apecy http//
lists.apple.com/; neko+opre ns nnx nocnmenr +emam paspa6o+kn. Ecnn nam ne yac+c
nan+n o+ne+ na nn+epecymmnn nac nonpoc n okymen+annn, nonpo6yn+e nponec+n nonck
n apxnnax .+nx paccrnok (Hs ynaenn k +rcnam ynac+nnkon kaoro cnncka,
noanync+a, nonmn+e o+ne+ n apxnnax nepe +em, kak ny6nnkona+r cnon nonpoc.)
Take cneye+ ynomny+r eme o6 onom pecypce: stackouerow.com. 3+o necrma
nonynpnrn can+, na ko+opom nporpammnc+r saam+ nonpocr n o+nenam+ na nnx. Ecnn
nam yac+c noo6pa+r neckonrko knmnenrx cnon, onncrnammnx nosnnkmym npo6nemy,
ckopee ncero, nr nane+e secr o+ne+.
Fnana 16. okyueu1aqun paspao1uuka
________________________________________________________________________________________________
98
!"# %&' ()*+,- ./&00
o cnx nop mr ncnonrsonann +onrko ro+onre knaccr, cosannre qnpmon Apple.
Hpnmno npem san+rc nanncannem co6c+nennrx knaccon. Bcnomnn+e, n+o knacc
onncrnae+ ne c+oponr o6rek+on:
me+or (.ksemnnpon n knacca), peannsyemre knaccom;
nepemennre .ksemnnpon, coepamnec n kaom .ksemnnpe knacca.
Cennac mr cosanm knacc b/(2#+, noxonn na c+pyk+ypy b/(2#+, ko+opym mr
cosann n rnane 10. Knacc 6ye+ onpeen+rc n nyx qannax: Person.h n Person.m.
dann Person.h, nasrnaemrn saronononnrm nnn nn+epqencnrm qannom, coepn+
o6rnnenn nepemennrx n me+oon .ksemnnpon. dann Person.m nasrnae+c >)41'3
&$)1!-)7!!. B nem nporpammnpym+c enc+nn, neo6xonmre n nrnonnenn kaoro
me+oa.
Cosan+e nonrn npoek+: nporpammy komannon c+pokn Foundation c nmenem
aKi*6/.
H+o6r cosa+r nonrn knacc, nr6epn+e komany File!New!New File.... ns
pasena Mac OS X na nenon nanenn, nr6epn+e Cocoa n ma6non Objective-C class.
Hpncnon+e knaccy nm b/(2#+ n nasnanr+e ero cy6knaccom P%M/,&. Hakonen,
y6en+ecr n +om, n+o n okne nr6pana nenr aKi*6/, n menknn+e na knonke %';/.
Tenepr n nannra+ope npoek+a o+o6paam+c qannr nonoro knacca, Person.h n
Person.m. O+kpon+e qann Person.h, nknmnn+e n nero o6rnnenn nyx nepemennrx
.ksemnnpon n +pex me+oon .ksemnnpon:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
@@ otff b/(2#+ uftjpxjn qfj sj{jjuurj ojstv{gq
@@ w jngpr9 gs{jpjtjuurj q otffj P%M/,&
*+&/(B',/ b/(2#+ \ P%M/,&
?
@@ otff fgpj{ywn pqj sj{jjuurj ojstv{gq
KiSP3 R14hR3NJ}131IQW
4J3 O14hR3NJ4iSQW
J
@@ jngpr9 ogng{rj xpxn wfsgtgqnfv ptv s{wfqwquwv
@@ ujuw sj{jjuur ojstv{gq
6 L5S4wVQ1314hR3NJ}131IQLKiSP3VRW
6 L5S4wVQ1314hR3NJ4iSQL4J3VOW
@@ jngp ptv qrwftjuwv i
6 LKiSP3V/Swq}PQQNJw1W
/+$
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
99
O6pa+n+e nnnmanne: nepemennre .ksemnnpon o6rnnm+c n qnrypnrx
cko6kax, a me+or - nocne nepemennrx n nne qnrypnrx cko6ok.
C +onkn spenn komnnn+opa .+o+ ko ronopn+: B onpeenm nonrn knacc c
nmenem b/(2#+, n ko+oprn nxo+ nce me+or n nepemennre .ksemnnpon knacca
P%M/,&. K nnm o6annm ne nepemennre .ksemnnpon: nepemennym +nna oat c
nmenem 4/*84&K+/&/(2 n nepemennym +nna *+& c nmenem L/*84&K+*-#2. Take
co6npamcr o6ann+r +pn me+oa .ksemnnpon, ko+opre 6yy+ peannsonanr n Person.m.
Me+o 2/&D/*84&K+/&/(2: nonynae+ aprymen+ +nna oat n ne nosnpamae+
snanenn, me+o 2/&E/*84&K+*-#2: nonynae+ aprymen+ +nna *+& n ne nosnpamae+
snanenn, a me+o M#$_'22K+$/d ne nonynae+ aprymen+on n nosnpamae+ snanenne
+nna oat.
O+kpon+e qann Person.m. Yann+e nce cymec+nymmne me+or n peannsyn+e
me+or, ko+opre 6rnn o6rnnenr:
)*6A#(& Cb/(2#+34C
*6A-/6/+&'&*#+ b/(2#+
6 L5S4wVQ1314hR3NJ}131IQLKiSP3VR
z
R14hR3NJ}131IQ j RW
|
6 L5S4wVQ1314hR3NJ4iSQL4J3VO
z
O14hR3NJ4iSQ j OW
|
6 LKiSP3V/Swq}PQQNJw1
z
I13aIJ O14hR3NJ4iSQ F LR14hR3NJ}131IQ X R14hR3NJ}131IQVW
|
/+$
Kak nnn+e, cpea Xcode nmnop+nponana qann Person.h sa nac. Take o6pa+n+e
nnnmanne na +o, n+o nmena peannsonannrx me+oon onnr n +onnoc+n connaa+r c
nmenamn, o6rnnenrmn n saronononnom qanne. B Xcode .+o enae+c npoc+o; kora nr
nannnae+e nnon+r me+o n qanne peannsannn, Xcode npenarae+ nr6pa+r ono ns nmen
ye o6rnnenrx me+oon.
H+ak, nce me+or, o6rnnenre n Person.h, peannsonanr; nam knacc ro+on.
O+peak+npyn+e qann main.m, n+o6r nemnoro no .kcnepnmen+npona+r c nnm:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
4THSI3 M{1IQSJ"RM
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
FF B;_l)+&< '*_<A@ZE9) {1IQSJ
{1IQSJ XH1IQSJ j {1IQSJ PiiS2 4J43W
FF @9&B>)&>)+&< _+)\<+&< @<9<A<++%A '*_<A@ZE9)
H1IQSJ Q1314hR3NJ4iSQW
H1IQSJ Q1314hR3NJ}131IQ!"W
FF >%_;> A<(;l) /Swq}PQQNJw1
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
100
KiSP3 /T4 j H1IQSJ /Swq}PQQNJw1W
xShLMH1IQSJ RPQ P ~}N SK rKM` /T4VW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. dann Person.h nmnop+npye+c n +oro,
n+o6r komnnn+op snan, kakne me+or 6rnn o6rnnenr o nx ncnonrsonann n 6'*+().
Pnc. 17.1. narpamma o6rek+on BMITime
Mevo(m (ocvyna
Brnonn .+o e ynpanenne co c+pyk+ypamn nmec+o o6rek+on, mr nanpmym
o6pamanncr k nonm annrx c+pyk+ypr ns 6'*+ ():
A/(2#+3L/*84&K+*-#2 k RH
A/(2#+34/*84&K+/&/(2 k X3WH
B o6rek+no-opnen+nponannrx nporpammax cneye+ no nosmonoc+n ckpr+r
nepemennre .ksemnnpon, n+o6r onn 6rnn nsnec+nr n oc+ynnr +onrko n camoro
o6rek+a. A n+o6r qynknnn n me+or sa npeenamn b/(2#+ mornn saa+r snanenn neca
n poc+a, mr cosann me+or 2/&E/*84&-+*-#2: n 2/&D/*84&K+/&/(2:. Takne
me+or nasrnam+c 2j&-me+oamn (nnn me+oamn sanncn).
%/&-me+o nosnone+ pyrnm me+oam saa+r snanenne nepemennon .ksemnnpa.
"j&-me+o (me+o n+enn) nosnone+ pyrnm me+oam nponn+a+r snanenne nepemennon
.ksemnnpa. %/&- n 8j&- me+or +ake nasrnam+c 3$#',)3! ,'+#60).
o6anr+e n Person.h o6rnnenn 8/&-me+oon:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ b/(2#+ \ P%M/,&
?
B-#'& 4/*84&K+/&/(2H
*+& L/*84&K+*-#2H
J
@@ qr fgyjnj pqn ujuwv ftjpxw sj{jjuur ojstv{gq
6 LKiSP3VR14hR3NJ}131IQW
U 7;#*$>2/&D/*84&K+/&/(2\7B-#'&>4H
6 L4J3VO14hR3NJ4iSQW
U 7;#*$>2/&E/*84&K+*-#2\7*+&>LH
U 7B-#'&>M#$_'22K+$/dH
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
101
/+$
Honemy nmena 8j&-me+oon ne nknmnam+ npeqnkc 8/& no ananornn c nmenamn
2j&-me+oon? Ho+omy n+o +akoe cornamenne ncnonrsye+c n Objective-C. Hm me+oa
n n+enn nepemennon .ksemnnpa npoc+o connaae+ c nmenem .+on nepemennon.
Tenepr nepnn+ecr k qanny Person.m n o6anr+e peannsannn 8j&-me+oon:
*6A-/6/+&'&*#+ b/(2#+
6 LKiSP3VR14hR3NJ}131IQ
z
I13aIJ R14hR3NJ}131IQW
|
U 7;#*$>2/&D/*84&K+/&/(2\7B-#'&>4
?
4/*84&K+/&/(2 k 4H
J
6 L4J3VO14hR3NJ4iSQ
z
I13aIJ O14hR3NJ4iSQW
|
U 7;#*$>2/&E/*84&K+*-#2\7*+&>L
?
L/*84&K+*-#2 k LH
J
U 7B-#'&>M#$_'22K+$/d
?
(/&.(+ L/*84&K+*-#2 @ 74/*84&K+/&/(2 : 4/*84&K+/&/(2>H
J
/+$
Oc+ae+c ncnonrsona+r .+n me+or n 6'*+36:
)*6A#(& Cb/(2#+34C
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ojstv{ A/(2#+
b/(2#+ :A/(2#+ k <<b/(2#+ '--#,= *+*&=H
@@ s{wfqwquwj ujuw sj{jjuur ojstv{
<A/(2#+ 2/&E/*84&K+*-#2\R=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W=H
@@ qrgq jngp M#$_'22K+$/d
B-#'& M6* k <A/(2#+ M#$_'22K+$/d=H xShLMH1IQSJ Lrw` rKV RPQ P ~}N SK rKM`
H1IQSJ O14hR3NJ4iSQ` H1IQSJ R14hR3NJ}131IQ` /T4VW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
102
Toueunan sannc
Hporpammnc+r Objective-C nac+o ncnonrsym+ me+or oc+yna. dnpma Apple pemnna
cosa+r cokpamennym sanncr n nrsona me+oon oc+yna. Cneymmne ne c+pokn koa
.knnnanen+nr:
A k <d B*$#=H
A k d3B*$#H
n .+n ne c+pokn +oe pa6o+am+ onnakono:
<d 2/&`*$#\S=H
d3B*$# k SH
dopma, n ko+opon nmec+o knapa+nrx cko6ok ncnonrsye+c cnmnon +onka,
nasrnae+c +onennon sanncrm.
B c+apamcr o6xon+rc 6es ncnonrsonann +onennon sanncn. Mne kae+c, n+o
ona macknpye+ qak+ o+npankn coo6menn n ne coo+ne+c+nye+ cornamennm o+npankn
ncex oc+anrnrx coo6mennn n cnc+eme. Hpn enannn nr moe+e ncnonrsona+r
+onennym sanncr n cnonx nporpammax, no n .+on knnre ona ne nc+penae+c.
Cnoncvna
Bonrma nac+r koa knacca b/(2#+ o+nocn+c k me+oam oc+yna. dnpma Apple cosana
yo6nrn mexannsm, ynpomammnn nanncanne me+oon oc+yna, - +ak nasrnaemre
+('4+#(). Hpn ncnonrsonannn cnonc+n 2/&- n 8/&- me+or mono o6rnn+r n onon
c+poke.
B qanne Person.h samenn+e o6rnnenne 2/&- n 8j&- me+oon konc+pyknnen
A(#A/(&_:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ b/(2#+ \ P%M/,&
?
B-#'& 4/*84&K+/&/(2H
*+& L/*84&K+*-#2H
J
HISH1I3q KiSP3 R14hR3NJ}131IQW
HISH1I3q 4J3 O14hR3NJ4iSQW
U 7B-#'&>M#$_'22K+$/dH
/+$
Knacc o+ .+on npankn ne nsmene+c - npoc+o nr ncnonrsye+e 6onee komnak+nrn
cnn+akcnc.
Tenepr sarnnn+e n qann Person.m. B nem nonno npoc+rx, npeckasyemrx
me+oon oc+yna. Ecnn namn me+or oc+yna ne enam+ nnnero oco6ennoro, mono
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
103
npoc+o npnkasa+r komnnn+opy cnn+esnpona+r me+or oc+yna no ymonnannm na
ocnonannn o6rnnenn A(#A/(&_. Yann+e me+or oc+yna n samenn+e nx nrsonom
2xs&4j2*j:
)*6A#(& Cb/(2#+34C
*6A-/6/+&'&*#+ b/(2#+
QqJ3R1Q41 R14hR3NJ}131IQ` O14hR3NJ4iSQW
U 7B-#'&>M#$_'22K+$/d
?
(/&.(+ L/*84&K+*-#2 @ 74/*84&K+/&/(2 : 4/*84&K+/&/(2>H
J
/+$
Hoc+pon+e n sanyc+n+e nporpammy. Bce pa6o+ae+ +onno +ak e, kak npee, no
ko c+an kya 6onee npoc+rm n yo6nrm n conponoennn.
self
B kaom me+oe oc+ynna nokanrna nepemenna 2/-B. Ona coepn+ ykasa+enr na
o6rek+, n ko+oporo 6rn nrsnan me+o. Hepemenna 2/-B ncnonrsye+c +ora, kora
o6rek+ xone+ o+npann+r coo6menne camomy ce6e. Hanpnmep, mnorne nporpammnc+r
Objective-C kpanne pennoc+no o+noc+c k ncnonrsonannm me+oon oc+yna; onn
nnkora ne nn+am+ n ne sanncrnam+ snanenn nepemennrx .ksemnnpon nanpmym, a
+onrko nepes me+or oc+yna. Hsmenn+e me+o M#$_'22-+$/d, n+o6r nam me+o
yc+ponn ae camrx pennoc+nrx 6nmc+n+enen nnc+o+r srka:
U 7B-#'&>M#$_'22K+$/d
?
KiSP3 R j Q1iK R14hR3NJ}131IQW
I13aIJ Q1iK O14hR3NJ4iSQ F LR X RVW
J
3ksemnnp b/(2#+ o+npanne+ camomy ce6e na coo6menn, 4/*84&-+/&/(2 n
L/*84&-+*-#2, n nonynenn snanennn nepemennrx .ksemnnpon.
Bnanenne 2/-B +ake mono nepea+r n aprymen+e, n+o6r pyrne o6rek+r snann
apec +ekymero o6rek+a. Hanpnmep, nam knacc b/(2#+ moe+ coepa+r me+o
'$$#.(2/-BZ#!(('_: cneymmero nna:
U 7;#*$>'$$#.(2/-BZ#!(('_\7P%.&'M-/!(('_ :>&4/!(('_
?
<&4/!(('_ '$$M/,&\2/-B=H
J
Becr npn nomomn 2/-B mr coo6maem maccnny, re naxon+c .ksemnnp
b/(2#+ - +o ec+r ykasrnaem ero apec.
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
104
Hpoekvm ns neckonknx qannon
Tenepr n namem npoek+e ncnonnemrn ko xpann+c n nyx qannax: main.m n Person.m.
(dann Person.h coepn+ o6rnnenne knacca, ncnonnemoro koa n nem ne+.) Hpn
noc+poennn npoek+a .+n qannr komnnnnpym+c no o+enrnoc+n, a sa+em cnsrnam+c n
ennoe nenoe. Peanrnre npoek+r nepeko coc+o+ ns co+en qannon koa C n Objective-
C:
K npoek+am +ake mono noknmna+r 6n6nno+ekn nporpammnoro koa.
onyc+nm, nr namnn o+nnnnym 6n6nno+eky n o6pa6o+kn annrx, nonynennrx c
nnqponoro +eneckona. Ecnn namen nporpamme neo6xonma +aka qynknnonanrnoc+r, nr
komnnnnpye+e 6n6nno+eky n o6anne+e ee n nam npoek+. Hpn noc+poennn nporpammr
Xcode noknmnae+ k nen nce qynknnn n knaccr, onpeenennre n 6n6nno+eke.
Pnc. 17.2. Maccnn o6rek+on StockHolding
npawnenne
Cosan+e nonym nporpammy komannon c+pokn Foundation c nmenem %&#,h2. Ba+em
cosan+e knacc %&#,hD#-$*+8 n npec+annenn kynnennrx aknnn. 3+o+ knacc
onen 6r+r cy6knaccom P%M/,&. On coepn+ ne nepemennre .ksemnnpa +nna
oat c nmenamn A.(,4'2/%4'(/b(*,/ n ,.((/+&%4'(/b(*,/ n ony nepemennym
.ksemnnpa +nna *+& c nmenamn +.6M/(B%4'(/2. Cosan+e me+or oc+yna n
nepemennrx .ksemnnpon. Onpeenn+e na me+oa .ksemnnpa:
U 7B-#'&>,#2&K+O#--'(2H @@ A.(,4'2/%4'(/b(*,/ : +.6M/(B%4'(/2
U 7B-#'&>;'-./K+O#--'(2H @@ ,.((/+&%4'(/b(*,/ : +.6M/(B%4'(/2
B qynknnn 6'*+() sanonnn+e maccnn +pem .ksemnnpamn %&#,hD#-$*+8.
Hepe6epn+e .nemen+r maccnna n nrnen+e snanenn kaoro ns nnx.
Fnana 17. Ham nepnm knacc
________________________________________________________________________________________________
105
!" )BZ<l;>)+&<
Hpn cosannn knacca b/(2#+ mr o6rnnnn ero cy6knaccom P%M/,&. 3+o osnanae+,
n+o karn .ksemnnp b/(2#+ 6ye+ coepa+r me+or n nepemennre .ksemnnpon,
onpeenennre n P%M/,&, a +ake me+or n nepemennre .ksemnnpon, onpeenennre
n b/(2#+. Fonop+, n+o b/(2#+ ")+1$,6$# me+or n nepemennre .ksemnnpon o+
P%M/,&. B .+om pasene mr nsynnm nacneonanne 6onee nopo6no.
O+kpon+e npoek+ aKi*6/ n cosan+e nonrn qann: knacc Objective-C c nmenem
6A-#_//, nnmmnnc cy6knaccom P%M/,&. Bckope mr nsmennm knacc 6A-#_// n
cenaem ero cy6knaccom b/(2#+. Bnonne nornnno, nepno? Pa6o+nnk (6A-#_//)
nne+c pasnonnnoc+rm nenoneka (b/(2#+). Y nnx ec+r poc+ n nec. Tem ne menee ne
karn nenonek nne+c pa6o+nnkom. Mr +ake nknmnnm n nam knacc nepemennym
.ksemnnpa, o+nocmymc k namemy konkpe+nomy knaccy, - +a6enrnrn nomep
pa6o+nnka.
Pnc. 18.1. narpamma nacneonann neko+oprx snakomrx knaccon
O+kpon+e qann Employee.h. Hmnop+npyn+e Person.h, samenn+e cynepknacc na
b/(2#+ n o6anr+e nepemennym .ksemnnpa n xpanenn +a6enrnoro nomepa
pa6o+nnka:
4THSI3 M{1IQSJ"RM
*+&/(B',/ 6A-#_// \ {1IQSJ
z
4J3 1THiSq11NyW
|
HISH1I3q 4J3 1THiSq11NyW
/+$
O+kpon+e Employee.m n cnn+esnpyn+e me+or oc+yna:
Fnana 18. Hacneponauue
________________________________________________________________________________________________
106
nacneye+ o+ nacneye+ o+ nacneye+ o+ nacnee+ o+
)*6A#(& C6A-#_//34C
*6A-/6/+&'&*#+ 6A-#_//
QqJ3R1Q41 1THiSq11NyW
/+$
Tenepr y nac nmee+c nonrn knacc 6A-#_//, coepamnn nce nepemennre
.ksemnnpon b/(2#+ n nonym nepemennym .ksemnnpa c nmenem /6A-#_//KO.
3ksemnnpr 6A-#_// o+nenam+ na +e e coo6menn, n+o n .ksemnnp b/(2#+. Kpome
+oro, .ksemnnpr Employee o+nenam+ na coo6menn 2/&6A-#_//KO: n /6A-#_//KO.
Tak kak knacc b/(2#+ nne+c cy6knaccom P%M/,&, 6A-#_// nacneye+ nce
me+or n nepemennre .ksemnnpon P%M/,&. Bce o6rek+r nacneym+ (npmo nnn
kocnenno) o+ P%M/,&.
Knacc P%M/,& coepn+ mnoro me+oon, no +onrko ony nepemennym
.ksemnnpa: ykasa+enr *2'. Ykasa+enr *2' kaoro o6rek+a ccrnae+c na knacc, ero
cosanmnn.
onyc+nm, nr o+npannnn o6rek+y coo6menne B*$#. H+o6r o+ne+n+r na .+o
coo6menne, o6rek+ npn nomomn ykasa+en *2' onpeene+ cnon knacc n cnpamnnae+:
"Ec+r nn y +e6 me+o .ksemnnpa c nmenem B*$#? Ecnn n knacce cymec+nye+ me+o
.ksemnnpa B*$#, +o on nrnonne+c. Ecnn y knacca +akoro me+oa ne+, on cnpamnnae+
y cnoero cynepknacca: Ec+r nn y +e6 me+o .ksemnnpa c nmenem B*$#? Tak nonck
me+oa c nmenem do npoonae+c nnepx 1\0 camoro nanana nenonkn. Honck
npekpamae+c npn o6napyennn me+oa B*$# nnn oc+nennn nocnenero snena. B
nanane nenonkn knacc P%M/,& ronopn+: He+, me+oa B*$# y men ne+, a nr
nonynae+e coo6menne o6 omn6ke - n+o-+o +nna 3ksemnnp 6A-#_// ne o+nenae+ na
cenek+op B*$#.
Pnc. 18.2. narpamma o6rek+on BMITime
Fnana 18. Hacneponauue
________________________________________________________________________________________________
107
.ksemnnp
kap
uacnepye1 o1
uacnepye1 o1
Honpo6yn+e camn - o+kpon+e qann main.m n o+npanr+e .ksemnnpy b/(2#+
coo6menne, ko+opoe on ne nonme+:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ojstv{ b/(2#+
4w A/(2#+ k <<b/(2#+ '--#,= *+*&=H
@@ s{wfqwquwj ujuw sj{jjuur ojstv{gq
<A/(2#+ 2/&E/*84&K+*-#2\R=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W=H
@@ qrgq jngp M#$_'22K+$/d
B-#'& M6* k <A/(2#+ M#$_'22K+$/d=H
P%c#87CA/(2#+ 7$9 B> 4'2 ' aK #B BC9
<A/(2#+ L/*84&K+*-#2=9 <A/(2#+ 4/*84&K+/&/(2=9 M6*>H
H1IQSJ 2SaJ3W
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. (Ha npeynpeenne komnnn+opa ne
o6paman+e nnnmann, no+omy n+o mr enaem .+o cosna+enrno. B 6onrmnnc+ne cnynaen
na npeynpeenne o6sa+enrno c+on+ o+pearnpona+r!) Ha konconr nrnon+c
coo6menne o6 ncknmnennn npemenn nrnonnenn:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Person count]: unrecognized selector sent to instance 0x100108de0'
Hpocmo+pn+e onncanne ncknmnenn n yann+e npo6nemnym c+poky, npee nem
npoona+r.
Mr cosann nonrn knacc 6A-#_//, no eme ne ncnonrsonann ero. Hsmenn+e qann
main.m, n+o6r n nem ncnonrsonanc knacc 6A-#_//:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
4THSI3 MTHiSq11"RM
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ojstv{ b/(2#+
{1IQSJ X A/(2#+ k <<THiSq11 '--#,= *+*&=H
@@ s{wfqwquwj ujuw sj{jjuur ojstv{
<A/(2#+ 2/&E/*84&K+*-#2\R=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W=H
@@ qrgq jngp M#$_'22K+$/d
B-#'& M6* k <A/(2#+ M#$_'22K+$/d=H
P%c#87CA/(2#+ 7$9 B> 4'2 ' aK #B BC9
J
(/&.(+ IH
J
Fnana 18. Hacneponauue
________________________________________________________________________________________________
108
O6pa+n+e nnnmanne: nepemenna person no-npenemy o6rnne+c kak ykasa+enr
na Person. He cosac+ nn .+o npo6nem? Hoc+pon+e n sanyc+n+e nporpammy - nr ynnn+e,
n+o ona pa6o+ae+ nopmanrno. eno n +om, n+o pa6o+nnk (6A-#_//) nne+c nac+nrm
cnynaem nenoneka (b/(2#+) - on moe+ cena+r nce, n+o moe+ nenonek Taknm o6pasom,
mr moem ncnonrsona+r .ksemnnp Employee nese, re nporpamma onae+ nonynn+r
.ksemnnp b/(2#+.
Ho cennac mr ncnonrsyem me+o, ynnkanrnrn n 6A-#_//, no.+omy +nn
nepemennon-ykasa+en npne+c nsmenn+r:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
)*6A#(& C6A-#_//34C
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?

@@ fgpuwj ojstv{ b/(2#+
THiSq11 :A/(2#+ k <<6A-#_// '--#,= *+*&=H
@@ s{wfqwquwj ujuw sj{jjuur ojstv{
<A/(2#+ 2/&E/*84&K+*-#2\R=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W=H
H1IQSJ Q13THiSq11Ny!sW
@@ qrgq jngp M#$_'22K+$/d
B-#'& M6* k <A/(2#+ M#$_'22K+$/d=H
xShLMTHiSq11 rw RPQ P ~}N SK rKM` H1IQSJ 1THiSq11Ny` /T4VW
J
(/&.(+ IH
J
Hepeonpe(enenne uevo(on
H+ak, npn o+npanke coo6menn nonck me+oa C ykasannrm nmenem nannnae+c c knacca
o6rek+a n nepexon+ nnepx no nenonke nacneonann. Brnonne+c nepna nanenna
peannsann; .+o osnanae+, n+o ynacneonannre me+or mono nepeonpeen+r
nonrsona+enrcknm peannsannmn. onyc+nm, nr pemnnn, n+o nnekc maccr +ena
pa6o+nnkon ncera onen 6r+r panen 19. n .+oro me+o M#$_'22K+$/d mono
nepeonpeenn+r n knacce 6A-#_//. O+kpon+e qann Employee.m n nnecn+e nsmenenn:
)*6A#(& C6A-#_//34C
*6A-/6/+&'&*#+ 6A-#_//
2_+&4/2*/ /6A-#_//KOH
6 LKiSP3V/Swq}PQQNJw1
z
I13aIJ !"bW
|
/+$
Fnana 18. Hacneponauue
________________________________________________________________________________________________
109
Hoc+pon+e n sanyc+n+e nporpammy. O6pa+n+e nnnmanne: nrnonne+c nama nona
peannsann M#$_'22K+$/d, a ne peannsann knacca b/(2#+.
Me+o M#$_'22K+$/d peannsonan n 6A-#_//. +, no on ne o6rnne+c n qanne
6A-#_//34. O6rnnenne me+oa n saronononnom qanne ny6nnkye+ nnqopmannm o
me+oe, n+o6r on mor nrsrna+rc .ksemnnpamn pyrnx knaccon. Ho nockonrky
Employee nacneye+ o+ Person, nce knaccr ye snam+, n+o .ksemnnpr 6A-#_//
coepa+ me+o M#$_'22K+$/d. Coo6ma+r o6 .+om sanono ne nyno.
super
A ecnn nr pemnnn, n+o y ncex pa6o+nnkon HMT, nrnncnennrn n peannsannn b/(2#+,
onen an+oma+nneckn ymenrma+rc na 10%? Konenno, mono npoy6nnpona+r ko n
peannsannn 6A-#_//, no ropaso yo6nee 6rno 6r nrsna+r nepcnm M#$_'22K+$/d
knacca b/(2#+ n ymnon+r pesynr+a+ na 0,9, npee nem nosnpama+r ero. n pemenn
.+on saann ncnonrsye+c npek+nna 2.A/(. Honpo6yem ncnonrsona+r ee n 6A-#_//36:
)*6A#(& C6A-#_//34C
*6A-/6/+&'&*#+ 6A-#_//
2_+&4/2*/ /6A-#_//KOH
U 7B-#'&>M#$_'22K+$/d
?
KiSP3 JSITPi~}N j QaH1I /Swq}PQQNJw1W
I13aIJ JSITPi~}N X b"W
J
/+$
Hoc+pon+e n sanyc+n+e nporpammy.
Tonnee ronop, npek+nna 2.A/( osnanae+: Brnonnn+r .+o+ me+o, no nana+r
nonck peannsannn c moero cynep knacca.
npawnenne
3+o ynpanenne c+pon+c na 6ase ynpanenn ns nperymen rnanr. Cosan+e knacc
`#(/*8+%&#,hD#-$*+8, nacneymmnn o+ %&#,hD#-$*+8. Bknmnn+e n
`#(/*8+%&#,hD#-$*+8 ononnn+enrnym nepemennym .ksemnnpa ,#+;/(2*#+N'&/,
o+nocmymc k +nny oat. (Ymnoa kypc nepenoa conversionRate na mec+nym neny,
mr nonynaem neny n onnapax CEA. Byem cnn+a+r, n+o A.(,4'2/b(*,/ n
,.((/+&b(*,/ saam+c n mec+non nanm+e.) Hepeonpeenn+e ,#2&-+O#--'(2 n
;'-./-+O#--'(2, n+o6r onn npannnrno pa6o+ann.
Fnana 18. Hacneponauue
________________________________________________________________________________________________
110
B qynknnn 6'*+() nknmnn+e n cnon maccnn neckonrko .ksemnnpon
`#(/*8+%&#,hD#-$*+8.
Pnc. 18.3. O6rek+r %&#,hD#-$*+8 n `#(/*8+%&#,hD#-$*+8
Fnana 18. Hacneponauue
________________________________________________________________________________________________
111
!" d:e<*(+%< @<9<A<++%<
'*_<A@ZE9;>
o nac+omero momen+a nepemennre .ksemnnpon, o6rnnnmnec n namnx knaccax,
o+nocnnncr k npoc+rm +nnam C - +aknm, kak *+& nnn B-#'&. Ha npak+nke nepemennre
.ksemnnpon ropaso name coepa+ ykasa+enn na pyrne o6rek+r. O6rek+na
nepemenna .ksemnnpa ykasrnae+ na pyron o6rek+ n onncrnae+ cnsr mey nym
o6rek+amn. Kak npannno, o6rek+nre nepemennre .ksemnnpon o+noc+c k onon ns
+pex ka+eropnn:

H#&!.6#< '.;$/#"'%' #!0): ykasa+enr na npoc+on o6rek+-snanenne - +akon, kak


P%%&(*+8 nnn P%P.6M/(. Hanpnmep, qamnnn pa6o+nnka ns knacca 6A-#_// 6ye+
xpann+rc n o6rek+e P%%&(*+8. Coo+ne+c+nenno .ksemnnp 6A-#_// coepn+
nepemennym .ksemnnpa, n ko+opon xpann+c ykasa+enr na .ksemnnp P%%&(*+8.

I#"'8$"!* #!0) ',!" / ',"'36: ykasa+enr na onn coc+annon o6rek+. Hanpnmep,


y pa6o+nnka moe+ 6r+r ena; n .ksemnnp 6A-#_// nknmnae+c nepemenna
.ksemnnpa, ko+opa coepn+ ykasa+enr na onn .ksemnnp b/(2#+.

I#"'8$"!* #!0) ',!" /' 3"'%!3: ykasa+enr na .ksemnnp knacca konneknnn -


nanpnmep, P%.&'M-/!(('_. (pyrne npnmepr konneknnn nc+pe++c nam n rnane 21.)
Hanpnmep, y pa6o+nnka mory+ 6r+r e+n; n .ksemnnp nknmnae+c nepemenna
.ksemnnpa, ko+opa coepn+ ykasa+enr na .ksemnnp P%.&'M-/!(('_ co cnnckom
ykasa+enen na onn nnn neckonrko o6rek+on b/(2#+.
B .+om cnncke no P%%&(*+8 nonnmae+c A/-$301*& knacca P%%&(*+8.
Bosmono, na nepnrn nsrn +akoe cokpamenne nrrnn+ nenpnnrnno, no ono onenr
mnpoko pacnpoc+paneno.
Pnc. 19.1. Knacc Employee c o6rek+nrmn nepemennrmn .ksemnnpon

Kak n na pyrnx narpammax, ykasa+enn nso6paenr c+penkamn (a +ake
nomenenr nmenamn nepemennrx). Taknm o6pasom, knacc 6A-#_// coepn+ +pn
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
112
nepemennre .ksemnnpon: -'2&P'6/, spouse n ,4*-$(/+. O6rnnenne nepemennrx
.ksemnnpon 6A-#_// 6ye+ nrrne+r +ak:
*+&/(B',/ 6A-#_// \ b/(2#+
?
*+& /6A-#_//KOH
P%%&(*+8 :-'2&P'6/H
b/(2#+ :2A#.2/H
P%.&'M-/!(('_ :,4*-$(/+H
J
Bce .+n nepemennre, sa ncknmnennem /6A-#_//KO, nnm+c ykasa+enmn.
O6rek+nre nepemennre .ksemnnpon ncera Bnnm+c ykasa+enmn. Hanpnmep,
nepemenna 2A#.2/ coepn+ ykasa+enr na pyron o6rek+, nnymnn n kyne.
Ykasa+enr 2A#.2/ xpann+c n o6rek+e 6A-#_//, a o6rek+ b/(2#+, na ko+oprn
on ccrnae+c - ne+. O6rek+r ne xpan+c nny+pn pyrnx o6rek+on. O6rek+ pa6o+nnka
coepn+ cnon +a6enrnrn nomep (nepemenna n camo snanenne), no on +onrko snae+
apec, no ko+opomy n nam+n xpann+c o6rek+ ero enr.
Y xpanenn n o6rek+ax ykasa+enen na o6rek+r (nmec+o camnx o6rek+on) ec+r na
nanrx no6onnrx .qqek+a:

Onn o6rek+ moe+ npnnnma+r neckonrko ponen. Hanpnmep, moe+ okasa+rc, n+o
o6rek+ enr pa6o+nnka +ake ykasan n kanec+ne kon+ak+noro nnna n o6rek+ax e+en.
Pnc. 19.2. Onn o6rek+ n neckonrknx ponx

B nam+n nporpammr xpann+c 6onrmoe konnnec+no ncenosmonrx o6rek+on.


Hcnonrsyemre o6rek+r onnr oc+ana+rc n nam+n, no nenynre o6rek+r onnr
ynnn+oa+rc (nx nam+r onna nosnpama+rc n kyny n non+opnoro
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
113
ncnonrsonann). Hon+opnoe ncnonrsonanne nam+n cnon+ k mnnnmymy sa+pa+r
nam+n na nrnonnenne nporpammr, a komnrm+ep 6rc+pee pearnpye+ na enc+nn
nonrsona+en. Ha mo6nnrnrx yc+ponc+nax (nanpnmep, iPhone) onepannonna cnc+ema
ynnn+oae+ nporpammy, sannmammym cnnmkom mnoro nam+n.
Bna(ennm o6+ekvon n ARC
n pemenn .+on npo6nemr 6rna paspa6o+ana konnennn (1),$"!* o6rek+amn. Kora
o6rek+ nne+c o6rek+non nepemennon .ksemnnpa, ronop+, n+o on nne+c
nnaenrnem o6rek+a, na ko+oprn ykasrnae+ nepemenna.
C pyron c+oponr, o6rek+ snae+, ckonrko nnaenrnen on nmee+ n nac+omnn
momen+. Hanpnmep, na npnneennon nrme narpamme .ksemnnp b/(2#+ nmee+ +pex
nnaenrnen: o6rek+ 6A-#_// n na o6rek+a ^4*-$. Kora konnnec+no nnaenrnen
o6rek+a ymenrmae+c o nyn, o6rek+ cnn+ae+, n+o on 6onrme nnkomy ne nyen, n
yane+ ce6 ns nam+n.
Hocne+om nnaenrnen kaoro o6rek+a sannmae+c mexannsm !N^ (.&#6'&/$
N/B/(/+,/ g.+&*s8) - n Objective-C .+a qynknn nonnnacr neanno.
o nrxoa Xcode 4.2 ynpann+r nocne+om nnaenrnen npnxonnocr npynnym, na
n+o yxonno mnoro npemenn n ycnnnn. (O pynnom nocne+e nnaenrnen o +om, kak on
pa6o+an, pacckasano n nocnenem pasene rnanr 20. Bnponem, no ncem koe,
npnneennom n knnre, npenonarae+c, n+o nr ncnonrsye+e !N^.)
anan+e nemnoro opa6o+aem npoek+ aKi*6/ n nocmo+pnm, kak nocne+
nnaenrnen pa6o+ae+ na npak+nke. Komnannn ononrno nac+o cne+ sa +em, kakne
ma+epnanrnre pecypcr 6rnn nrenenr +omy nnn nnomy pa6o+nnky. Mr cosanm knacc
!22/&, a n kaom o6rek+e 6A-#_// 6ye+ xpann+rc maccnn c onncannem pecypcon,
onepennrx annomy pa6o+nnky.
Pnc 19.3. Pa6o+nnkn n pecypcr

Hoo6nre nepapxnn nac+o nasrnam+c o+nomennmn pon+enr/no+omok:
pon+enr (.ksemnnp 6A-#_//) coepn+ konneknnm no+omkon (konneknn
P%.&'M-/!(('_ c o6rek+amn !22/&).
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
114
Cos(anne knacca Asset
Cosan+e nonrn qann n cy6knacca P%M/,&. Hpncnon+e emy nm !22/&. O+kpon+e
qann Asset.h, o6rnn+e ne nepemennre .ksemnnpon n na cnonc+na:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ !22/& \ P%M/,&
z
xx3I4Jh XiP/1iW
aJQ4hJ1w 4J3 I1QPi1Pia1W
|
HISH1I3q LQ3ISJhV xx3I4Jh XiP/1iW
HISH1I3q aJQ4hJ1w 4J3 I1QPi1Pia1W
/+$
O6pa+n+e nnnmanne na monqnka+op 2&(#+8 y o6rnnenn {(g{j(&x n -'M/-.
On osnanae+: 3+o ykasa+enr na o6rek+, nnaenrnem ko+oporo c+anonnmcr. (pyrne
nosmonre snanenn .+oro a+pn6y+a paccma+pnnam+c n rnane 20.)
Hanomnm, n+o o6rek+, y ko+oporo ne+ nn onoro nnaenrna, ynnn+oae+c. Hpn
ynnn+oennn o6rek+y o+npanne+c coo6mennm $/'--#,. (Bce o6rek+r nacneym+
me+o $/'--#, o+ P%M/,&.) Hepeonpeenenne me+oa $/'--#, nosnonn+ nam
nona6nma+r sa ynnn+oennem .ksemnnpon !22/&.
H+o6r 6rno non+no, kakon nmenno .ksemnnp !22/& ynnn+oae+c, +ake
neo6xonmo peannsona+r pyron me+o P%M/,&, ko+oprn nasrnae+c $/2,(*A&*#+.
Me+o nosnpamae+ c+poky c coepa+enrnrm onncannem .ksemnnpa knacca. n
.ksemnnpon !22/& me+o $/2,(*A&*#+ 6ye+ nosnpama+r c+poky, nknmnammym
snanenn -'M/- n (/2'-/'-./ +ekymero .ksemnnpa.
O+kpon+e qann Asset.m. Cnn+esnpyn+e me+or oc+yna n nepemennrx
.ksemnnpon, a sa+em nepeonpeenn+e $/2,(*A&*#+ n $/'--#,.
)*6A#(& C!22/&34C
*6A-/6/+&'&*#+ !22/&
QqJ3R1Q41 iP/1i` I1QPi1Pia1W
6 Lxx3I4Jh XVw1Q2I4H34SJ
z
I13aIJ xx3I4Jh Q3I4Jh43RSITP3Mr rw M`
Q1iK iP/1i` Q1iK I1QPi1Pia1W
|
6 L5S4wVw1PiiS2
z
xShLMw1PiiS2P34Jh rM` Q1iKVW
|
/+$
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
115
O6pa+n+e nnnmanne na sanonnn+enr n qopma+nrx c+pokax npnneennoro nrme
koa. 3+o+ sanonnn+enr samene+c pesynr+a+om o+npankn coo6menn
coo+ne+c+nymmen nepemennon (ko+opa onna coepa+r ykasa+enr na o6rek+,
nonynammnn coo6menne).
Honpo6yn+e noc+pon+r nporpammy. n+o6r nocmo+pe+r, ne 6rnn nn onymenr
omn6kn npn nnoe. Hporpammy mono noc+pon+r n 6es sanycka - n .+on nenn
ncnonrsye+c kom6nnann knannm ^#66'+$la. anna nosmonoc+r moe+ 6r+r
nonesna n nponepkn koa 6es sanycka nporpammr - nanpnmep, ecnn nr snae+e, n+o
nporpamma k sanycky eme ne ro+ona.
Take ncera pekomenye+c c+pon+r nporpammy nocne nnecenn nsmenennn,
n+o6r nm6re cnn+akcnneckne omn6kn o6napynnanncr n ncnpannnncr nemenenno.
Ho+om 6ye+ neonennno, kakne nsmenenn o+nenam+ sa nonnenne nonon omn6kn.
o6annenne ovnomennn o(nn ko unornu B Employee
A cennac mr o6annm n knacc 6A-#_// o+nomenne onn ko mnornm. Hanomnm, n+o n
.+om o+nomennn saenc+nonan o6rek+ konneknnn (nanpnmep, maccnna) n o6rek+r,
coepamnec n konneknnn.
na nanrx qak+a, ko+opre neo6xonmo sna+r o konneknnx n nnaenrnax
o6rek+on:

Hpn o6annennn o6rek+a konneknn coxpane+ ykasa+enr na o6rek+, n y o6rek+a


nonne+c nnaenen.

Hpn yanennn o6rek+a konneknn ocno6oae+ ykasa+enr na o6rek+, a y o6rek+a


+epe+c nnaenen.
H+o6r peannsona+r o+nomenne onn ko mnornm. B 6A-#_//, nam nonao6n+c
nona nepemenna .ksemnnpa, n ko+opon 6ye+ xpann+rc ykasa+enr na nsmenemrn
maccnn pecypcon. Take nonao6n+c napa ononnn+enrnrx me+oon. O+kpon+e qann
Employee.h n o6anr+e nx:
)*6A#(& Cb/(2#+34C
2iPQQ QQ13W
*+&/(B',/ 6A-#_// \ b/(2#+
?
*+& /6A-#_//KOH
x}a3P/i1IIPq XPQQ13QW
J
A(#A/(&_ *+& /6A-#_//KOH
6 L5S4wVPwwQQ13Q./0123LQQ13 XVPW 6 LaJQ4hJ1w 4J3V5Pia1.KQQ13QW
/+$
O6pa+n+e nnnmanne na c+poky ,-'22 !22/&;. B nponecce n+enn qanna
komnnn+op nc+penae+ nm knacca !22/&. Ecnn y nero ne+ nnkakon nnqopmannn o6 .+om
knacce, +o komnnn+op nrae+ omn6ky. C+poka ,-'22 !22/&; coo6mae+ komnnn+opy:
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
116
Knacc c nmenem !22/& cymec+nye+. Ecnn on nc+pe+n+c n .+om qanne - 6es nannkn. H
.+o nce, n+o +e6e noka neo6xonmo sna+r.
Hcnonrsonanne ,-'22 nmec+o )*6A#(& npeoc+anne+ komnnn+opy menrme
nnqopmannn, no yckope+ o6pa6o+ky .+oro konkpe+noro qanna. Mr moem
ncnonrsona+r npek+nny f-22 n Employee.h n pyrnx saronononnrx qannax, no+omy
n+o n o6pa6o+kn qanna c o6rnnennmn komnnn+opy ne +pe6ye+c mnoro
nnqopmannn.
Tenepr o6pa+nmc k qanny Employee.m. B o+nomennx +nna onn ko mnornm.
neo6xonmo cosa+r o6rek+ konneknnn (maccnn n namem cnynae), npee nem n+o-nn6o
coxpan+r n nem. 3+o mono cena+r npn cosannn ncxonoro o6rek+a (.ksemnnp
6A-#_//) nnn e ne +oponn+rc n nooa+r nepnon onepannn c konneknnen. B namen
nporpamme 6ye+ ncnonrsonan n+opon napnan+.
)*6A#(& C6A-#_//34C
4THSI3 MQQ13"RM
*6A-/6/+&'&*#+ 6A-#_//
2_+&4/2*/ /6A-#_//KOH
6 L5S4wVPwwQQ13Q./0123LQQ13 XVP
z
FF NQ PQQ13Q J4i^
4K LPQQ13QV z
FF 7I1P31 3R1 PIIPq
PQQ13Q j x}a3P/i1IIPq PiiS2 4J43W
|
PQQ13Q Pww./0123PW
|
6 LaJQ4hJ1w 4J3V5Pia1.KQQ13Q
z
FF xaT aH 3R1 I1QPi1 5Pia1 SK 3R1 PQQ13Q
aJQ4hJ1w 4J3 QaT j bW
KSI LQQ13 XP 4J PQQ13QV z
QaT tj P I1QPi1Pia1W
|
I13aIJ QaTW
|
U 7B-#'&>M#$_'22K+$/d
?
B-#'& +#(6'-aK k <2.A/( M#$_'22K+$/d=H
(/&.(+ +#(6'-aK : I3H
J
6 Lxx3I4Jh XVw1Q2I4H34SJ
z
I13aIJ xx3I4Jh Q3I4Jh43RSITP3MTHiSq11 rw rw 4J PQQ13QM`
Q1iK 1THiSq11Ny` Q1iK 5Pia1.KQQ13QW
|
6 L5S4wVw1PiiS2
z
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
117
xShLMw1PiiS2P34Jh rM` Q1iKVW
|
/+$
n o6pa6o+kn qanna Employee.m komnnn+opy neo6xonma nopo6na
nnqopmann o knacce 22/&, no.+omy mr nmnop+npyem Asset.h nmec+o ncnonrsonann
f-22.
Take o6pa+n+e nnnmanne na nepeonpeenenne $/2,(*A&*#+ n $/'--#, n
o+cnennann yanenn .ksemnnpon 6A-#_//.
Hoc+pon+e npoek+ n y6en+ecr, n+o on ne coepn+ omn6ok.
Tenepr neo6xonmo cosa+r o6rek+r pecypcon n sakpenn+r nx sa pa6o+nnkamn.
O+peak+npyn+e qann main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345 )*6A#(& C6A-#_//34C
4THSI3 MQQ13"RM
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
FF B;_l)+&< A)BB&>) ;:e<*(;> THiSq11
x}a3P/i1IIPq X1THiSq11Q j x}a3P/i1IIPq PiiS2 4J43W
KSI L4J3 4 j bW 4 !bW 4ttV z
FF B;_l)+&< '*_<A@ZE9) THiSq11
THiSq11 XH1IQSJ j THiSq11 PiiS2 4J43W
FF @9&B>)&>)+&< _+)\<+&f @<9<A<++%A '*_<A@ZE9)
H1IQSJ Q1314hR3NJ4iSQb t 4W
H1IQSJ Q1314hR3NJ}131IQ!" 6 4F!b"bW
H1IQSJ Q13THiSq11Ny4W
FF >*Z\<+&< '*_<A@ZE9) > A)BB&> 1THiSq11Q
1THiSq11Q Pww./0123H1IQSJW
|
FF B;_l)+&< !b '*_<A@ZE9;> PQQ13Q
KSI L4J3 4 j bW 4 !bW 4ttV z
FF B;_l)+&< '*_<A@ZE9) PQQ13
QQ13 XPQQ13 j QQ13 PiiS2 4J43W
FF @9&B>)&>)+&< A<(*&
xx3I4Jh X2aII1J3P/1i j xx3I4Jh Q3I4Jh43RSITP3MPH3SH rwM` 4W
PQQ13 Q13P/1i2aII1J3P/1iW
PQQ13 Q131QPi1Pia14 X !W
FF @;Z?\<+&< BZ?\)f+;,; \&BZ) ;( b l; >*Z\&(<Z]+;
xNJ31h1I IPJwSTNJw1 j IPJwSTLV r 1THiSq11Q 2SaJ3W
FF @;Z?\<+&< B;;(><(B(>?<,; 9):;(+&*)
THiSq11 XIPJwSTTHiSq11 j 1THiSq11Q S/01233NJw1IPJwSTNJw1W
FF +)_+)\<+&< 9<B?9B) 9):;(+&*?
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
118
IPJwSTTHiSq11 PwwQQ13Q./0123PQQ13W
|
xShLMTHiSq11Q rM` 1THiSq11QVW
xShLM454Jh aH SOJ1IQR4H SK SJ1 1THiSq11MVW
1THiSq11Q I1TS51./01233NJw1sW
xShLM454Jh aH SOJ1IQR4H SK PIIPqMVW
1THiSq11Q j J4iW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy Pesynr+a+ onen nrrne+r npnmepno +ak:
6A-#_//2\ 7
C16A-#_// I\ I *+ '22/&25C9
C16A-#_// X\ XTS *+ '22/&25C9
C16A-#_// V\ XX *+ '22/&25C9
C16A-#_// S\ RW *+ '22/&25C9
C16A-#_// ]\ I *+ '22/&25C9
C16A-#_// T\ XSR *+ '22/&25C9
C16A-#_// R\ XX *+ '22/&25C9
C16A-#_// Y\ S] *+ '22/&25C9
C16A-#_// W\ I *+ '22/&25C9
C16A-#_// \ XSR *+ '22/&25C
>
"*;*+8 .A #L+/(24*A #B #+/ /6A-#_//
$/'--#,'&*+8 16A-#_// T\ XSR *+ '22/&25
$/'--#,'&*+8 1c'A&#A S\ TX 5
$/'--#,'&*+8 1c'A&#A T\ WT 5
"*;*+8 .A #L+/(24*A #B '(('_
$/'--#,'&*+8 16A-#_// I\ I *+ '22/&25
$/'--#,'&*+8 16A-#_// X\ XTS *+ '22/&25
$/'--#,'&*+8 1c'A&#A \ XTS 5
333
$/'--#,'&*+8 16A-#_// \ XSR *+ '22/&25
$/'--#,*+8 1c'A&#A W\ XSR 5
Hpn yanennn ns maccnna .ksemnnpa 6A-#_// c nomepom 5 .ksemnnp
ynnn+oae+c, no+omy n+o y nero ne oc+ae+c nnaenrnen. Hocne .+oro ynnn+oam+c
ero pecypcr, no+omy n+o y nnx +oe ne oc+anocr nnaenrnen. (Honepr+e na cnono:
me+kn, +o ec+r .ksemnnpr P%%&(*+8, ynnn+oennrx pecypcon +oe 6yy+
ynnn+oenr, no+omy n+o y nnx +oe ne oc+ae+c nnaenrnen.)
Kora /6A-#_//2 saae+c snanenne +*-, y maccnna ne oc+ae+c nnaenrna.
Coo+ne+c+nenno, on ynnn+oae+c, n+o npnnon+ k nennon peaknnn ocno6oennn
nam+n n ynnn+oenn o6rek+on, +ak kak nce .ksemnnpr 6A-#_// nnmam+c
nocnenero nnaenrna.
Yo6no, npana? Kak +onrko o6rek+ c+anonn+c nenynrm, on ynnn+oae+c.
Ecnn nenynre o6rek+r nonemy-nn6o ne ynnn+oam+c, ronop+, n+o n nporpamme
nosnnkae+ 6#$=/) 0)3*#!. O6rek+r saepnnam+c n nam+n cnepx nonoennoro
npemenn, npnnoenne nannnae+ nornoma+r nce 6onrme n 6onrme nam+n. B iOS
onepannonna cnc+ema n konennom n+ore anapnnno sanepmae+ npnnoenne. B Mac OS X
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
119
y+enka nam+n cnnae+ o6mym nponsnon+enrnoc+r cnc+emr, +ak kak komnrm+ep
nannnae+ +pa+n+r nce 6onrme npemenn na nrrpysky annrx ns nam+n na nck.
npawnenne
Hcnonrsy knacc %&#,hD#-$*+8 ns nperymero ynpanenn, nannmn+e nporpammy,
ko+opa cosae+ .ksemnnp knacca b#(&B#-*# (nop+qenr aknnn) H sanonne+ ero
o6rek+amn %&#,hD#-$*+8 ns ynpanenn nperymen rnanr. O6rek+ b#(&B#-*#
onen yme+r nrnncn+r cnom +ekymym c+onmoc+r.
Fnana 19. OLek1ume nepeueuume akseunnnpon
________________________________________________________________________________________________
120
12# 3*)456+*&7)89)
:6);.9 (&<=69
B nporpammnponannn nac+o nc+penam+c nyc+oponnne o+nomenn. onyc+nm, o6rek+
pecypca onen nme+r nosmonoc+r ysna+r, sa kem ns pa6o+nnkon on sakpennen.
anan+e nknmnnm .+o o+nomenne n namy nporpammy. Hona narpamma o6rek+on 6ye+
nrrne+r kak na pnc. 20.1.
Pnc. 20.1. o6annenne o+nomenn
C +onkn spenn apxn+ek+ypr .+a cnsr peannsye+c o6annennem ykasa+en o+
no+omka (.ksemnnp !22/&) k pon+enm (.ksemnnp 6A-#_//).
o6anr+e n Asset.h nepemennym .ksemnnpa n xpanenn ykasa+en na o6rek+
pa6o+nnka:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
2iPQQ THiSq11W
*+&/(B',/ !22/& \ P%M/,&
?
P%%&(*+8 :-'M/-H
THiSq11 XRSiw1IW
.+2*8+/$ *+& (/2'-/'-./H
J
A(#A/(&_ 72&(#+8> P%%&(*+8 :-'M/-H
HISH1I3q LQ3ISJhV THiSq11 XRSiw1IW
A(#A/(&_ .+2*8+/$ *+& (/2'-/'-./H
/+$
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
121
B qanne Asset.m crenepnpyn+e me+or oc+yna n pacmnpr+e me+o
$/2,(*A&*#+, n+o6r on nrnonn nnqopmannm o epa+ene pecypca:
)*6A#(& C!22/&34C
4THSI3 MTHiSq11"RM
*6A-/6/+&'&*#+ !22/&
2_+&4/2*/ -'M/-9 (/2'-/'-./` RSiw1IH
U 7P%%&(*+8 :>$/2,(*A&*#+
?
FF l<9=)(<Z] 9<B?9B) ;(Z&\<+ ;( J4i^
4K LQ1iK RSiw1IV z
I13aIJ xx3I4Jh Q3I4Jh43RSITP3Mr rw` PQQ4hJ1w 3S rM`
Q1iK iP/1i` Q1iK I1QPi1Pia1` Q1iK RSiw1IW
| 1iQ1 z
I13aIJ xx3I4Jh Q3I4Jh43RSITP3Mr rw aJPQQ4hJ1wM`
Q1iK iP/1i` Q1iK I1QPi1Pia1W
|
J
U 7;#*$>$/'--#,
?
P%c#87C$/'--#,*+8 C9 2/-B>H
J
/+$
Bosnnkae+ nanrn nonpoc: kak npn conmec+nom ncnonrsonannn knaccon !22/& n
6A-#_// o6ecnenn+r cornaconannoc+r nyx o+nomennn? Hnane ronop, pecypc onen
npncy+c+nona+r n maccnne pecypcon pa6o+nnka n +om n +onrko n +om cnynae, ecnn
pa6o+nnk nne+c epa+enem pecypca. Bosmonr +pn napnan+a:

Bnno saa+r o6a o+nomenn:


<;*,/b(/2*$/+& '$$!22/&2M/,&\&#L+^'(=H
<&#L+^'( 2/&D#-$/(\;*,/b(/2*$/+&=H

B me+oe, saammem snanenne ykasa+en no+omka, o6ann+r no+omka n konneknnm


pon+en:
U 7;#*$>2/&D#-$/(\76A-#_// :>/
?
4#-$/( k /H
</ '$$!22/&2M/,&\2/-B=H
J
Takoe pemenne npnmene+c o+nocn+enrno peko.

B me+oe, o6annmmem no+omka n konneknnm pon+en, saa+r ykasa+enr no+omka.



B .+om ynpanennn mr nocnonrsyemc nocnennm napnan+om. B qanne
Employee.m pacmnpr+e me+o '$$!22/&2M/,&:, snanenne n+o6r on +ake npncnannan
4#-$/(:
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
122
U 7;#*$>'$$!22/&2M/,&\7!22/& :>'
?
@@ K2 '22/&2 +*-~
*B 7F'22/&2> ?
@@ ^(/'&/ &4/ '(('_
'22/&2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
J
<'22/&2 '$$M/,&\'=H
P Q13Siw1IQ1iKW
J
(Ona ns monx nm6nmrx omn6ok: na me+oa oc+yna an+oma+nneckn nrsrnam+
pyr pyra. B nporpamme nosnnkae+ 6eckonennrn nnkn: '$$!22/&2M/,&: nrsrnae+
2/&D#-$/(:, ko+oprn nrsrnae+ '$$!22/&2M/,&:, ko+oprn nrsrnae+ 2/&D#-$/(:,
ko+oprn...)
Hoc+pon+e n sanyc+n+e nporpammy. Pesynr+a+ onen nrrne+r npnmepno +ak:
6A-#_//2\ 7
C16A-#_// I\ I *+ '22/&25C9
C16A-#_// X\ XTS *+ '22/&25C9
C16A-#_// V\ XX *+ '22/&25C9
C16A-#_// S\ RW *+ '22/&25C9
C16A-#_// ]\ I *+ '22/&25C9
C16A-#_// T\ XSR *+ '22/&25C9
C16A-#_// R\ XX *+ '22/&25C9
C16A-#_// Y\ S] *+ '22/&25C9
C16A-#_// W\ I *+ '22/&25C9
C16A-#_// \ XSR *+ '22/&25C
>
"*;*+8 .A #L+/(24*A #B #+/ /6A-#_//
"*;*+8 .A #L+/(24*A #B '(('_
$/'--#,'&*+8 16A-#_// I\ I *+ '22/&25
$/'--#,'&*+8 16A-#_// ]\ I *+ '22/&25
$/'--#,'&*+8 16A-#_// W\ I *+ '22/&25
O6pa+n+e nnnmanne: nn onn ns o6rek+on pa6o+nnkon, ko+oprm nasnanenr
pecypcr, ne ynnn+oae+c. To e nponcxon+ n c pecypcamn. Honemy?
Hnknnueckoe nna(enne
Pecypc nne+c nnaenrnem pa6o+nnka, pa6o+nnk nne+c nnaenrnem maccnna
pecypcon, a maccnn pecypcon nne+c nnaenrnem pecypca. Hoo6nre nnknnneckne
o+nomenn npnno+ k xpanennm mycopa n nam+n. 3+n o6rek+r onnr 6r+r
ynnn+oenr n ocno6oenn nam+n, no .+oro ne nponcxon+. Bosnnkae+ cn+yann
7!/1!=$+/'%' (1),$"!* - onenr pacnpoc+panennrn nc+onnnk y+enkn nam+n.
n nrnnenn nnknnneckoro nnaenn n nporpammax mono nocnonrsona+rc
nporpammon - npoqnnnponmnkom Apple, ko+opa nasrnae+c Instruments. Hporpamma
sanyckae+c no ynpannennem npoqnnnponmnka n anannsa co6r+nn, nponcxomnx
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
123
na sanem nnane namero koa n cnc+emr. Onako nama nporpamma sanyckae+c n
sanepmae+ pa6o+y onenr, onenr 6rc+po. H+o6r nonynn+r npem n npoqnnnponann,
nknmnn+e n konen qynknnn 6'*+() nrson 2-//A() c nayson na co+nm cekyn:
Pnc 20.2. Br6op npoqnnnponmnka
J
Qi11HL!bbVW
(/&.(+ IH
J
Pnc. 20.3. Hnc+pymen+ Allocations
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
124
Brnonnn+e n menm Xcode komany b(#$.,&!b(#B*-/; sanyckae+c nporpamma
Instruments. Br6epn+e n cnncke nnc+pymen+on npoqnnnponann penm c/'h2.
B xoe nrnonnenn nporpammr nr na6nmae+e sa +ekymnm nonoennem en. Ha
nenon nanenn naxo+c na nnc+pymen+a (pnc. 20.3). Eennok na nnc+pymen+e
!--#,'&*#+2 o+o6paae+ rnc+orpammy nam+n, nrenennon n kyne.
Hanpnmep, ns pncynka nnno, n+o n kyne oc+anncr 10 .ksemnnpon !22/&.
H+o6r nan+n n nporpamme nnknnneckoe nnaenne, nepeknmnn+ecr na nnc+pymen+
c/'h2 n nr6epn+e npec+annenne ^_,-/2 ns c+pokn menm na +a6nnnen. Hpn nr6ope
konkpe+noro nnkna o+o6paae+c ero narpamma o6rek+on (pnc. 20.4).
Pnc. 20.4. Hnc+pymen+ Leaks
Cna6me ccmnkn
Kak yc+pann+r npo6nemy c nnknnnecknm nnaennem? Hcnonrsyn+e cna6re ccrnkn.
B1).)* ++<1/) npec+anne+ co6on ykasa+enr, ne nopasymenammnn nnaenn. B namen
nporpamme n pasprna nnknnneckoro nnaenn pecypc ne onen 6r+r nnaenrnem
cnoero epa+en. O+peak+npyn+e qann Asset.h, n+o6r npeo6pasona+r epa+en n
cna6ym ccrnky:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
,-'22 6A-#_//H
*+&/(B',/ !22/& \ P%M/,&
?
P%%&(*+8 :-'M/-H
O1Pp 6A-#_// :4#-$/(H .+2*8+/$ *+& (/2'-/'-./H
J
A(#A/(&_ 72&(#+8> P%%&(*+8 :-'M/-H
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
125
A(#A/(&_ 7O1Pp> 6A-#_// :4#-$/(H
A(#A/(&_ .+2*8+/$ *+& (/2'-/'-./H
/+$
Hoc+pon+e n sanyc+n+e nporpammy. Tenepr nce o6rek+r ynnn+oam+c +ak,
kak nonoeno.
B o+nomennx pon+enr/no+omok n npeo+npamenn nnknon nnaenn
o6rnno npnmene+c npoc+oe npannno: pon+enr nne+c nnaenrnem no+omka, no
no+omok ne onen 6r+r nnaenrnem pon+en.
O6nynenne cna6mx ccmnok
H+o6r nona6nma+r sa pa6o+on cna6rx ccrnok, anan+e nknmnnm n namy kap+nny eme
onn maccnn. onyc+nm, nam nonao6nnc maccnn ncex pecypcon - ae +ex, ko+opre ne
6rnn nasnanenr nn onomy konkpe+nomy pa6o+nnky.
Pecypcr o6annm+c n maccnn no mepe cosann. Bknmnn+e neckonrko c+pok koa n
main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
)*6A#(& C6A-#_//34C
)*6A#(& C!22/&34C
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ffwq gjongq 6A-#_//
P%.&'M-/!(('_ :/6A-#_//2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
B#( 7*+& * k IH * 1 XIH *ll> ?
@@ fgpuwj ojstv{ 6A-#_//
6A-#_// :A/(2#+ k <<6A-#_// '--#,= *+*&=H
@@ s{wfqwquwj ujuw sj{jjuur ojstv{gq
<A/(2#+ 2/&E/*84&K+*-#2\I l *=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W U *@XI3I=H
<A/(2#+ 2/&6A-#_//KO\*=H
@@ qotjuwj {gnuwo q ffwq /6A-#_//2
</6A-#_//2 '$$M/,&\A/(2#+=H
J
x}a3P/i1IIPq XPiiQQ13Q j x}a3P/i1IIPq PiiS2 4J43W
@@ fgpuwj XI ojstv{gq '22/&2
B#( 7*+& * k IH * 1 XIH *ll> ?

@@ fgpuwj ojstv{ '22/&
!22/& :'22/& k <<!22/& '--#,= *+*&=H
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
126
@@ s{wfqwquwj jnow
P%%&(*+8 :,.((/+&c'M/- k <P%%&(*+8 2&(*+8E*&4`#(6'&\Cc'A&#A $C9
*=H
<'22/& 2/&c'M/-\,.((/+&c'M/-=H
<'22/& 2/&N/2'-/'-./\* : XY=H
@@ sgtxjuwj ftxug|g wft gn I pg qotwnjtug
P%QK+&/8/( ('+$#6K+$/d k ('+$#67> </6A-#_//2 ,#.+&=H
@@ sgtxjuwj fggnqjnfqxj|g {gnuwo
6A-#_// :('+$#66A-#_// k </6A-#_//2 #M/,&!&K+$/d\('+$#6K+$/d=H
@@ uujuwj {jfx{f {gnuwox
<('+$#66A-#_// '$$!22/&2M/,&\'22/&=H
PiiQQ13Q Pww./0123PQQ13W
J
P%c#87C6A-#_//2\ C9 /6A-#_//2>H
P%c#87C"*;*+8 .A #L+/(24*A #B #+/ /6A-#_//C>H
</6A-#_//2 (/6#;/M/,&!&K+$/d\T=H
xShLMPiiQQ13Q rM` PiiQQ13QVW
P%c#87C"*;*+8 .A #L+/(24*A #B '(('_2C>H
PiiQQ13Q j J4iW
/6A-#_//2 k +*-H
J
2-//A7XII>H
(/&.(+ IH
J
Hpee nem c+pon+r n sanycka+r nporpammy, nonpo6yn+e npenonon+r, kak
6ye+ nrrne+r nrno. Br ynnn+e coepnmoe maccnna '--!22/&2 - nocne
ynnn+oenn .ksemnnpa 6A-#_// -T. B kakom coc+onnn 6yy+ naxon+rc ero
pecypcr na .+o+ momen+? Onn +epm+ onoro nnaenrna (.ksemnnp 6A-#_//
-T), no y nnx no-npenemy oc+ae+c nnaenen '--!22/&2, no.+omy onn ne 6yy+
ynnn+oenr.
H+o nponcxon+ co snanennem 4#-$/( n pecypcon, nnaenrnem ko+oprx panee
6rn .ksemnnp 6A-#_// -T? Hpn ynnn+oennn o6rek+a, na ko+oprn ykasrnae+ cna6a
ccrnka, nepemenna ykasa+en o6nyne+c, +o ec+r en npncnannae+c +*-.
Coo+ne+c+nenno pecypcr .ksemnnpa 6A-#_// -T ne 6yy+ ynnn+oenr, a nx
nepemennon 4#-$/( 6yy+ an+oma+nneckn o6nynenr.
Tenepr noc+pon+e n sanyc+n+e npo rpammy n nponepr+e pesynr+a+:
6A-#_//2\ 7
C16A-#_// I\ I *+ '22/&25C9
333
C16A-#_// \ XSR *+ '22/&25C
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
127
>
"*;*+8 .A #L+/(24*A #B #+/ /6A-#_//
$/'--#,'&*+8 16A-#_// T\ XSR *+ '22/&25
'--!22/&2\ 7
C1c'A&#A I\ I9 '22*8+/$ &# 16A-#_// S\ RW *+ '22/&255C9
C1c'A&#A X\ XY9 '22*8+/$ &# 16A-#_// R\ XX *+ '22/&255C9
C1c'A&#A V\ S]9 '22*8+/$ &# 16A-#_// Y\ S] *+ '22/&255C9
HIJKLM>L ND OP, QRKSS+TR-UVH9
C1c'A&#A ]\ RW9 '22*8+/$ &# 16A-#_// S\ RW *+ '22/&255C9
HIJKLM>L PD OWP QRKSS+TR-UVH9
C1c'A&#A R\ XIV9 '22*8+/$ &# 16A-#_// R\ XX *+ '22/&255C9
C1c'A&#A Y\ XX9 '22*8+/$ &# 16A-#_// V\ XX *+ '22/&255C9
C1c'A&#A W\ XSR9 '22*8+/$ &# 16A-#_// \ XSR *+ '22/&255C9
C1c'A&#A \ XTS9 '22*8+/$ &# 16A-#_// X\ XTS *+ '22/&255C
>
"*;*+8 .A #L+/(24*A #B '(('_2
$/'--#,*+8 1c'A&#A S\ TX .+'22*8+/$5
333
$/'--#,*+8 1c'A&#A W\ XSR .+'22*8+/$5
nn n6osnavennmx: pyunon no(cuev ccmnok n ncvopnn
ARC
Kak ynomnnanocr n nanane rnanr 19, o nonnenn n Objective-C mexannsma
an+oma+nneckoro nocne+a ccrnok ARC (Automatic Reference Counting) ncnonrsonanc
pynnon nocne+ ccrnok, npn ko+opom nnaenrnr nsmennncr +onrko npn nnon
o+npanke o6rek+y coo6menn, ymenrmanmero nnn ynennnnnanmero cne+nnk ccrnok.
<'+M/,& (/-/'2/=H @@ '+M/,& nj{vjn qtpjt
<'+M/,& (/&'*+=H @@ '+M/,& sgtxjn qtpjt
Hoo6nre nrsonr n ocnonnom nc+penam+c n me+oax oc+yna ((/&'*+ - npn
cosannn nonoro snanenn, (/-/'2/ - npn ocno6oennn c+aporo snanenn) n n me+oax
$/'--#, (npn ocno6oennn ncex panee saxnanennrx o6rek+on). Me+o 2/&D#-$/(:
knacca !22/& n .+om cnynae nrrnn+ npnmepno +ak:
U 7;#*$>2/&D#-$/(\76A-#_// :>+/L6A
?
<+/L6A (/&'*+=H
<4#-$/( (/-/'2/=H
4#-$/( k +/L6AH
J
A no+ kak 6ye+ nrrne+r me+o $/'--#,:
U 7;#*$>$/'--#,
?
<-'M/- (/-/'2/=H
<4#-$/( (/-/'2/=H
<2.A/( $/'--#,=H
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
128
J
Kak nacne+ me+oa $/2,(*A&*#+? On cosae+ n nosnpamae+ c+poky. onen nn
knacc !22/& sann+r na nee npana nnaenrna? He+, .+o 6eccmrcnenno - ner pecypc ne
yepnnae+ cosannym c+poky. O+npann o6rek+y coo6menne '.&#(/-/'2/, nr
nomenae+e ero n o+npankn (/-/'2/ n 6yymem. o nonnenn ARC me+o description
knacca !22/& nrrnen 6r +ak:
U 7P%%&(*+8 :>$/2,(*A&*#+
?
P%%&(*+8 :(/2.-& k <<P%%&(*+8 '--#,= *+*&E*&4`#(6'&\C1\ $ 5C9
<2/-B -'M/-=9 <2/-B (/2'-/'-./==H
<(/2.-& '.&#(/-/'2/=H
(/&.(+ (/2.-&H
J
Kora o+npanne+c coo6menne (/-/'2/? Hpn c6poce +ekymero nyna
'.&#(/-/'2/:
@@ gpuwj sxt '.&#(/-/'2/
P%!.&#(/-/'2/b##- :'(A k <<P%!.&#(/-/'2/b##- '--#,= *+*&=H
!22/& :'22/& k <<!22/& '--#,= *+*&=H
P%%&(*+8 :$ k <'22/& $/2,(*A&*#+=H
@@ fn{go9 u ogng{x xorqjn $9 ugpwnfv q sxtj '.&#(/-/'2/
<'(A $('*+=H @@ n{goj gns{qtvjnfv fggjuwj (/-/'2/
ARC !+0'19-6$# nyn '.&#(/-/'2/ an+oma+nneckn, no sa cosanne n c6poc nyna
o+nenae+e nr. C nonnennem ARC +ake nonnnc nonrn cnn+akcnc cosann nyna
'.&#(/-/'2/. Hpnneennrn nrme ko +enepr nrrnn+ cneymmnm o6pasom:
@@ gpuwj sxt '.&#(/-/'2/
'.&#(/-/'2/A##- ?
!22/& :'22/& k <<!22/& '--#,= *+*&=H
P%%&(*+8 :$ k <'22/& $/2,(*A&*#+=H
@@ fn{go9 u ogng{x xorqjn $9 ugpwnfv q sxtj '.&#(/-/'2/
J@@ {gf sxt
Hpannna no(cueva ccmnok
Cymec+nye+ c+anap+nrn na6op cornamennn no pa6o+e c nam+rm, ko+opre
co6nmam+c ncemn nporpammnc+amn Objective-C. Ecnn nr ncnonrsye+e ARC, +o .+n
cornamenn an+oma+nneckn nrnonnm+c sa nac.
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
129
B .+nx npannnax cnonom nr o6osnanam .ksemnnp knacca, c ko+oprm nr
pa6o+ae+e n nac+omnn momen+. Taka +onka spenn necrma nonesna: npec+anr+e, n+o
nr - o6rek+, na ko+oprm nr pa6o+ae+e n nac+omnn momen+. Taknm o6pasom, qpasa
Ecnn nr c+anonn+ecr nnaenrnem c+pokn, ona ne 6ye+ ynnn+oena n
enc+nn+enrnoc+n osnanae+ Ecnn .ksemnnp knacca, na ko+oprm nr pa6o+ae+e,
c+anonn+c nnaenrnem c+pokn, ona ne 6ye+ ynnn+oena.
H+ak, npannna (n kpyrnrx cko6kax onncanr nopo6noc+n peannsannn).
Ecnn nr cosae+e o6rek+ me+oom, nm ko+oporo nannnae+c c --#, nnn +/L nnn
coepn+ fg{x, +o nr c+anonn+ecr ero nnaenrnem. (To ec+r npenonarae+c, n+o y
nonoro o6rek+a cne+nnk ccrnok panen 1 n on ne naxon+c n nyne '.&#(/-/'2/.) Br
o+nenae+e sa ynnn+oenne o6rek+a, kora on c+ane+ nenynrm. Hpnmepr nac+o
ncnonrsyemrx me+oon, ko+opre enam+ nac nnaenrnem cosannoro o6rek+a: alloc (sa
nrsonom ko+oporo ncera cneye+ nrson *+*&), copy n 6.&M-jg{x.

Ecnn o6rek+ 6rn cosan nm6rmn pyrnmn cpec+namn, nr ne nnerecr ero


nnaenrnem (To ec+r cneye+ nonara+r, n+o ero cne+nnk ccrnok panen 1 n on ye
naxon+c n nyne autorelease - a cneona+enrno, o6penenna ynnn+oenne, ecnn +onrko
y nero ne nonn+c nonrn nnaenen o c6poca nyna autorelease.)

Ecnn nr ne nne+ecr nnaenrnem o6rek+a, no xo+n+e, n+o6r on npoona


cymec+nona+r, c+anr+e ero nnaenrnem, o+npannn emy coo6menne (/&'*+ (n+o
npnnon+ k ynennnennm cne+nnka ccrnok).

Ecnn nr nne+ecr nnaenrnem o6rek+a n on nam 6onrme ne nyen, o+npanr+e emy


coo6menne (/-/'2/ nnn autorelease. (Coo6menne (/-/'2/ ymenrmae+ cne+nnk ccrnok
nemenenno, autorelease npnnon+ k o+npanke coo6menn (/-/'2/ npn c6poce nyna.)

Hoka y o6rek+a oc+ae+c xo+ 6r onn nnaenen, on npoonn+ cymec+nona+r, (Kora


cne+nnk ccrnok ymenrmn+c o nyn, o6rek+y o+npanne+c coo6menne $/'--#,.)
H+o6r nynme paso6pa+rc n ynpannennn nam+rm, nonesno paccma+pnna+r
nponcxomee c nokanrnon +onkn spenn. Knaccy !22/& nnnero ne nyno sna+r o
pyrnx o6rek+ax, n ko+oprx +ake cymec+nenno coepnmoe ero me+kn. Hpn
ycnonnn, n+o .ksemnnp !22/& nasnanae+ ce6 nnaenrnem o6rek+on, ko+opre on xone+
yepa+r n nam+n, npo6nem ne 6ye+. Hannnammne nporpammnc+r nnora conepmam+
omn6ky, nr+acr camoc+o+enrno nec+n yne+ ccrnok na nce o6rek+r n npnnoennn. He
enan+e .+oro.
Ecnn nr co6nmae+e .+n npannna n ncera mrcnn+e nokanrnrmn ka+eropnmn
knacca, nam nnkora ne npne+c 6ecnokon+rc o +om, kak oc+anrnre nac+n npnnoennn
pa6o+am+ c o6rek+om.
Hocne snakomc+na c konnennnen nnaenn c+anonn+c cno, nonemy neo6xonmo
o+npann+r autorelease n me+oe description n c+pokn: o6rek+ pa6o+nnka cosae+
c+poky, no ne xone+ c+anonn+rc ee nnaenrnem.
Fnana 20. Hpepo1npameuue y1euku naun1u
________________________________________________________________________________________________
130
C!" 8Z)BB% *;ZZ<*n&f
B knnre ye ncnonrsonanncr na knacca konneknnn: P%!(('_ n ero cy6knacc
P%.&M-j((x. Kak nr ye snae+e, maccnn coepn+ na6op ykasa+enen na pyrne
o6rek+r. Ykasa+enn xpan+c n onpeenennom nopke, n n o6pamenn k o6rek+am
konneknnn mono ncnonrsona+r nnekc (nenonncnennrn nomep). B .+on rnane mr
6nne nosnakomnmc c maccnnamn, a +ake paccmo+pnm pyrne knaccr konneknnn:
NSArray /NSMutableArray
Hpn nknmnennn o6rek+a n maccnn nocnennn c+anonn+c ero nnaenrnem. Kora o6rek+
yane+c ns maccnna, maccnn ncknmnae+c ns nncna nnaenrnen. O+kpon+e npoek+
aKi*6/ n nocmo+pn+e, kak n nporpamme ncnonrsye+c maccnn employees. Ecnn ne
o6pama+r nnnmann na nce oc+anrnoe, ocnonnon ko nrrnn+ npnmepno +ak:
@@ gpuwj ffwq gjongq 6A-#_//
P%.&'M-/!(('_ :/6A-#_//2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
B#( 7*+& * k IH * 1 XIH *ll> ?
@@ gpuwj gjon {gnuwo
6A-#_// :A/(2#+ k <<6A-#_// '--#,= *+*&=H
@@ otjuwj gjon {gnuwo q ffwq /6A-#_//2
</6A-#_//2 '$$M/,&\A/(2#+=H
J
</6A-#_//2 (/6#;/M/,&!&K+$/d\T=H
/6A-#_//2 k +*-H
Kak npannno, nyc+on nsmenemrn (6.&M-j) maccnn cosae+c me+oamn --#,@
*+*& nnn me+oom knacca ((x. Hanpnmep, komana cosann nsmenemoro maccnna
moe+ nrrne+r +ak:
P%.&'M-/!(('_ :/6A-#_//2 k <P%.&'M-/!(('_ ((x=H
Me+o '$$M/,&: o6anne+ o6rek+ n konen cnncka. C o6annennem nonrx
o6rek+on maccnn an+oma+nneckn ynennnnnae+c o pasmepa, neo6xonmoro n nx
xpanenn.
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
131
Hensuenneume o6+ekvm
Hpn cosannn .ksemnnpa P%!(('_ ykasrnam+c nce o6rek+r, ko+opre n nem onnr
xpann+rc. O6rnno .+o nrrnn+ +ak:
P%!(('_ :,#-#(2 k <P%!(('_ '(('_E*&4M/,&2\C('+8/C9 C/--#LC9 C"(//+C9
+*-=H
Bnanenne +*- n konne cnncka nokasrnae+, re me+o onen npekpa+n+r
o6annenne nonrx .nemen+on. B namem npnmepe maccnn ,#-#(2 6ye+ coepa+r +pn
c+pokn. (Ecnn nr sa6ye+e +*-, ckopee ncero, .+o npnnee+ k anapnnnomy sanepmennm
nporpammr.)
Mnornx nannnammnx nporpammnc+on ynnne+ cam qak+ cymec+nonann
P%!(('_. Banem nyen cnncok, coepnmoe ko+oporo nenrs nsmenn+r? Ec+r ne
npnnnnr:

Br ne onepe+e nmm, c ko+oprmn pa6o+ae+e. Hnane ronop, nr xo+n+e paspemn+r


nm npocmo+p maccnna, no ne nsmenenne ero coepnmoro. pyroe, 6onee ennka+noe
pemenne - npeoc+ann+r nm P%.&M-j((x, no nra+r ero P%!(('_. Hanpnmep,
npec+anr+e ce6e cneymmnn me+o:
@@ gq{jn ffwq w SI ujjnur wfjt
U 7P%!(('_ :>#$$2
?
2&'&*, P%.&'M-/!(('_ :#$$2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
*+& * k XH
L4*-/ 7<#$$2 ,#.+&= 1 SI> ?
<#$$2 '$$M/,&\<P%P.6M/( +.6M/(E*&4K+&\*=H
* lk VH
J
(/&.(+ #$$2H
J
Karn, k+o nrsrnae+ .+o+ me+o, cnn+ae+, n+o on nosnpamae+ nensmenemrn
maccnn P%!(('_. Ecnn nrsrnamma c+opona nonr+ae+c o6ann+r nnn yann+r
.nemen+r ns nosnpamennoro o6rek+a, komnnn+op nrae+ cyponoe npeynpeenne -
xo+ na camom ene nosnpamae+c .ksemnnp P%.&M-j((x.

pyra npnnnna n ncnonrsonann nensmenemrx o6rek+on - nponsnon+enrnoc+r;


nensmenemre o6rek+r ne nyno konnpona+r. Hpn pa6o+e c ns- menemrm o6rek+om
nnora npnxon+c cosana+r npnna+nym konnm, n+o6r 6r+r ynepennrm n +om, n+o
pyron ko ne cmoe+ monqnnnpona+r o6rek+ nesame+no n nac. n nensmenemrx
o6rek+on .+o ne o6sa+enrno. Bonee +oro, me+o copy knacca P%.&M-j((x cosae+
nonym konnm camoro ce6 n nosnpamae+ ykasa+enr na nonrn maccnn, a me+o copy
knacca P%!(('_ ne enae+ nnnero - on npoc+o nosnpamae+ ykasa+enr na camoro ce6.
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
132
Ho .+nm npnnnnam nensmenemre o6rek+r oc+a+onno nac+o ncnonrsym+c
n nporpammnponannn Objective-C. B Foundation nensmenemre .ksemnnpr cosam+c
mnornmn knaccamn: P%!(('_, P%%&(*+8, P%!&&(*M.&/$%&(*+8, P%O'&',
P%^4'(',&/(%/&, P%O*,&*#+'(_, P%%/&, P%K+$/d%/& n P%QNcN/./2&. Bce .+n knaccr
nmem+ nsmenemre cy6knaccr: P%.&M-j((x, P%.&M-j2&(*s8,
P%.&M-j&&(*M.&j$%&(*+8 n. +. . Knaccr P%O'&/ n P%P.6M/( +oe o+noc+c k
nensmenemrm, no ne nmem+ nsmenemrx cy6knaccon.
Copvnponka
Cymec+nye+ neckonrko cnoco6on cop+nponkn maccnna, no camrn pacnpoc+panennrn
cnoco6 ocnonan na ncnonrsonannn maccnna eckpnn+opon cop+nponkn. Knacc
NSMutableArray coepn+ cneymmnn me+o:
U 7;#*$>2#(&Q2*+8O/2,(*A&#(2\7P%!(('_ :>2#(&O/2,(*A&#(2H
Aprymen+ coepn+ maccnn o6rek+on P%%#(&O/2,(*A&#(. eckpnn+op
cop+nponkn coepn+ nm cnonc+na o6rek+on, coepamnxc n maccnne, n npnsnak
nanpannenn cop+nponkn (no nospac+annm nnn y6rnannm). Banem nepeana+r maccnn
eckpnn+opon cop+nponkn? Hpec+anr+e, n+o nam no+pe6onanocr o+- cop+npona+r
cnncok knnen+on no qamnnnm n nopke nospac+ann. A ecnn y nyx knnen+on
okay+c onnakonre qamnnnn? Tora npnxon+c ncnonrsona+r 6onee cnonrn
kpn+epnn cop+nponkn, nanpnmep:. O+cop+npona+r no qamnnnm n nopke nospac+ann;
ecnn qamnnnn connaam+ - +o no nmenam n nopke nospac+ann, a ecnn connaam+ n
nmena, n qamnnnn - +o no non+onrm nnekcam.
Pnc. 21.1. Cop+nponka ocymec+nne+c cnanana no lastName, sa+em no rstName n no zipCode
Cnonc+no, no ko+opomy ocymec+nne+c cop+nponka, moe+ 6r+r nm6on
nepemennon .ksemnnpa nnn pesynr+a+om nrnonnenn nm6oro me+oa o6rek+a.
Bepnemc k npoek+y KZ*6j n nocmo+pnm, kak cop+nponka pa6o+ae+ na npak+nke. B
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
133
qynknnn 6'*+(), nenocpec+nenno nepe nrnoom maccnna /6A-#_//2, o+cop+npyem ero
no snanennm ;'-./B!22/&2. Ecnn sa nym pa6o+nnkamn sakpennenr pecypcr pannon
c+onmoc+n, n+opnnna cop+nponka ocymec+nne+c no snanennm /6A-#_//KO. Bnecn+e
nsmenenn n qann main.m:
J
xxSI3y1Q2I4H3SI X5SP j xxSI3y1Q2I4H3SI
QSI3y1Q2I4H3SI43R1qM5Pia1.KQQ13QM
PQ21Jw4JhxW
xxSI3y1Q2I4H3SI X14 j xxSI3y1Q2I4H3SI
QSI3y1Q2I4H3SI43R1qM1THiSq11NyM
PQ21Jw4JhxW
1THiSq11Q QSI3Q4Jhy1Q2I4H3SIQxIIPq PIIPq43R./0123Q5SP` 14` J4iW

P%c#87C6A-#_//2\ C9 /6A-#_//2>H
Hoc+pon+e n sanyc+n+e nporpammy Br ynnn+e, n+o cnncok pa6o+nnkon cop+npye+c n
npannnrnom nopke:
6A-#_//2\ 7
C16A-#_// I\ I *+ '22/&25C9
C16A-#_// ]\ I *+ '22/&25C9
C16A-#_// W\ I *+ '22/&25C9
C16A-#_// Y\ S] *+ '22/&25C9
C16A-#_// S\ RW *+ '22/&25C9
C16A-#_// V\ XX *+ '22/&25C9
C16A-#_// R\ XX *+ '22/&25C9
C16A-#_// T\ XSR *+ '22/&25C9
C16A-#_// \ XSR *+ '22/&25C9
C16A-#_// X\ XTS *+ '22/&25C
+nnvpannn
Knacc P%b(/$*,'&/ npenasnanen n npec+annenn 0&$,!/)#'( - ycnonnn, ko+opre
mory+ 6r+r nc+nnnrmn, nanpnmep: Bnanenne /6A-#_//KO 6onrme 75. Knacc
P%.&M-j((x coepn+ yo6nrn me+o n yanenn ncex o6rek+on, n ko+oprx
npenka+ ne nrnonne+c:
U 7;#*$>B*-&/(Q2*+8b(/$*,'&/\7P%b(/$*,'&/ :>A(/$*,'&/H
Y NSArray nmee+c me+o, ko+oprn cosae+ nonrn maccnn co ncemn o6rek+amn,
n ko+oprx npenka+ nrnonne+c:
U 7P%!(('_ :>B*-&/(/$!(('_Q2*+8b(/$*,'&/\7P%b(/$*,'&/ :>A(/$*,'&/H
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
134
onyc+nm, mr xo+nm o+o6pa+r nce pecypcr, nrenennre pa6o+nnkam, y ko+oprx
cymmapna c+onmoc+r nrenennrx pecypcon npenrmae+ $70. Bknmnn+e n konen main.m
cneymmnn ko:
</6A-#_//2 (/6#;/M/,&!&K+$/d\T=H
P%c#87C'--!22/&2\ C9 '--!22/&2>H
x{I1w42P31 XHI1w42P31 j x{I1w42P31 HI1w42P3143RSITP3
MRSiw1I"5Pia1.KQQ13Q
bMW
xIIPq X3S~112iP4T1w j PiiQQ13Q
K4i31I1wIIPqQ4Jh{I1w42P31HI1w42P31W
xShLM3S~112iP4T1w rM` 3S~112iP4T1wVW
3S~112iP4T1w j J4iW
P%c#87C"*;*+8 .A #L+/(24*A #B '(('_2C>H
'--!22/&2 k +*-H
/6A-#_//2 k +*-H
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy Hona nepcn nrnon+ cneymmnn cnncok
pecypcon:
&#a/N/,-'*6/$\ 7
C1c'A&#A X\ XY9 '22*8+/$ &# 16A-#_// R\ XX *+ '22/&255C9
C1c'A&#A S\ TX9 '22*8+/$ &# 16A-#_// T\ XSR *+ '22/&255C9
C1c'A&#A T\ WT9 '22*8+/$ &# 16A-#_// T\ XSR *+ '22/&255C9
C1c'A&#A R\ XIV9 '22*8+/$ &# 16A-#_// R\ XX *+ '22/&255C9
C1c'A&#A W\ XSR9 '22*8+/$ &# 16A-#_// \ XSR *+ '22/&255C9
C1c'A&#A \ XTS9 '22*8+/$ &# 16A-#_// X\ XTS *+ '22/&255C
>
dopma+na c+poka n onpeenennn npenka+a moe+ 6r+r onenr cnonon. Ecnn
nam nac+o npnxon+c qnnr+pona+r coepnmoe konneknnn, o6sa+enrno nponn+an+e
pykonoc+no Predicate Programming Guide ns okymen+annn Apple.
NSSet/NSMutableSet
J"'2$+#(' npec+anne+ co6on pasnonnnoc+r konneknnn, n ko+opon o+cy+c+nye+
non+ne nopka cneonann o6rek+on. hm6on konkpe+nrn o6rek+ moe+ nxon+r n
mnoec+no +onrko onn pas.
Mnoec+na, npee ncero, nonesnr n nonynenn o+ne+a na nonpoc:
Hpncy+c+nye+ nn o6rek+ n saannom na6ope? Hanpnmep, n namen nporpamme moe+
xpann+rc na6op URL-apecon, ko+opre onnr 6r+r neoc+ynnr n e+en. Hpee
nem o+o6paa+r ne6-c+pannny n pe6enka, cneye+ nponepn+r, ne npncy+c+nye+ nn ona
cpen URL-apecon mnoec+na. C mnoec+namn +aka nponepka nrnonne+c onenr
6rc+po.
Brenennre pa6o+nnky pecypcr ne nmem+ onpeenennoro nopka xpanenn n ne
mory+ non+opno nxon+r B konneknnm pecypcon onoro pa6o+nnka. Hsmenn+e
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
135
nporpammy n+o6r nmec+o P%.&M-j((x n xpanenn o+nomennn pecypcon
ncnonrsonanc knacc P%.&M-/%j&.
Pnc. 21.2. Hcnonrsonanne knacca NSMutableSet n xpanenn pecypcon
B qanne Employee.h nsmenn+e o6rnnenne nepemennon:
)*6A#(& Cb/(2#+34C
,-'22 !22/&H
*+&/(B',/ 6A-#_// \ b/(2#+
?
*+& /6A-#_//KOH
x}a3P/i1x13 X'22/&2H J
A(#A/(&_ *+& /6A-#_//KOH
U 7;#*$>'$$!22/&2M/,&\7!22/& :>'H
U 7.+2*8+/$ *+&>;'-./B!22/&2H
/+$
K+ 6A-#_//369 ,(/'&/ '+ *+2&'+,/ #B &4/ ,#((/,& ,-'22\
U 7;#*$>'$$!22/&2M/,&\7!22/& :>'
?
*B 7F'22/&2> ?
'22/&2 k <<x}a3P/i1x13 '--#,= *+*&=H
J
<'22/&2 '$$M/,&\'=H
<' 2/&D#-$/(\2/-B=H
J
Hoc+pon+e n sanyc+n+e nporpammy. Ona onna pa6o+a+r +ak e, kak npee.
K o6rek+y mnoec+na nenosmono o6pa+n+rc no nnekcy, no+omy n+o .nemen+r
mnoec+na ne ynoponenr. Bmec+o .+oro mono nnmr cnpocn+r: Bxon+ nn o6rek+ n
mnoec+no? B knacce P%%/& nmee+c coo+ne+c+nymmnn me+o:
U 7ac>,#+&'*+2M/,&\7*$>dH
Hpn o+npanke .+oro coo6menn mnoec+no nepe6npae+ cnom konneknnm
o6rek+on n nonckax o6rek+a, pannoro . Ecnn +akon o6rek+ 6ye+ nanen, me+o
nosnpamae+ %; n npo+nnnom cnynae nosnpamae+c P.
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
136
H +y+ mr c+anknnaemc c oc+a+onno rny6oknm nonpocom: n+o osnanae+
pannoro? Knacc P%M/,& onpeene+ me+o c nmenem *2.'-:, nponepmmnn
panenc+no nyx o6rek+on:
*B 7<6_O#,&#( *2.'-\_#.(Z/++*2b'(&+/(=> ?
P%c#87C6_ $#,&#( *2 /.'- &# _#.( &/++*2 A'(&+/(C>H
J
P%M/,& npeoc+anne+ npoc+ym peannsannm *2.'-:. Ona nrrnn+ +ak:
U 7ac>*2.'-\7*$>#&4/(
?
(/&.(+ 72/-B kk #&4/(>H
J
Taknm o6pasom, 6es nepeonpeenenn me+oa *2.'-: npnneennrn qparmen+
.knnnanen+en cneymmemy:
*B 76_O#,&#( kk _#.(Z/++*2b'(&+/(> ?
P%c#87C6_ $#,&#( *2 /.'- &# _#.( &/++*2 A'(&+/(C>H
J
Heko+opre knaccr nepeonpeenm+ *2.'-:. Hanpnmep, n P%%&(*+8 me+o
*2.'-: nepeonpeene+c n cpannenn cnmnonon c+pokn. n .+nx knaccon
panenc+no ne connaae+ c nen+nnnoc+rm. onyc+nm, y nac nmem+c na ykasa+en na
c+pokn:
P%%&(*+8 :d k 333
P%%&(*+8 :_ k 333
Bnanenn n x nen+nnnr, ecnn onn coepa+ onnakonre apeca. Bnanenn n
x pannr, ecnn c+pokn, na ko+opre onn ykasrnam+, coepa+ onnakonre cnmnonr,
cneymmne n onom nopke.
Taknm o6pasom, nen+nnnre o6rek+r ncera pannr, no pannre o6rek+r ne ncera
nen+nnnr.
Bana nn .+a pasnnna? a, nana. Hanpnmep, knacc P%.&M-j((x coepn+
na me+oa:
U 7P%QK+&/8/(>*+$/dBM/,&\7*$>'+M/,&H
U 7P%QK+&/8/(>*+$/dBM/,&K$/+&*,'-Z#\7*$>'+M/,&H
Hepnrn me+o nepe6npae+ konneknnm, nponep n kaoro o6rek+a ycnonne
*2.'-: '+M/,&. B+opon me+o nepe6npae+ konneknnm c nponepkon ycnonn kk
'+M/,&.
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
137
NSDictionary/NSMutableDictionary
Kak nr ye snae+e, .nemen+r maccnnon nen+nqnnnpym+c nncnonrmn nnekcamn;
nerko n yo6no o6pa+n+rc k maccnny: an mne o6rek+ c nnekcom 10. Cnonapn
(acconna+nnnre maccnnr) nnekcnpym+c c+pokamn; y nnx sanpoc na nr6opky .nemen+a
nrrnn+ nnane: an mne o6rek+ c knmnom B';#(*&/^#-#(. Fonop +onnee, cnonapr
npec+anne+ co6on konneknnm nap knmn/snanenne. Knmn o6rnno nne+c c+pokon,
a snanenne moe+ 6r+r nponsnonrnrm o6rek+om. Hapr knmn/snanenne ne xpan+c n
kakom-nn6o onpeenennom nopke.
Cosanm cnonapr pykonon+enen qnpmr. Knmnom n nem 6ye+ onnoc+r, a
snanennem - .ksemnnp 6A-#_//. Hepnrn pa6o+nnk sanocn+c n cnonapr c knmnom
@"CEO" (ncnonnn+enrnrn npek+op), a n+opon - c knmnom @"CTO" (+exnnnecknn
npek+op). Bnecn+e nsmenenn n main.m: cnanana nporpamma cosae+ n sanonne+
P%.&M-jO*f&*g+(x, nocne nero nrnon+ coepnmoe cnonap. Hakonen, ykasa+enm
na cnonapr npncnannae+c +*-, n+o6r 6rno nnno, n+o cnonapr 6rn ynnn+oen.
@@ gpuwj ffwq gjongq 6A-#_//
P%.&'M-/!(('_ :/6A-#_//2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
@@gpuwj ftgq{v /d/,.&*;/2
x}a3P/i1y4234SJPIq X112a3451Q j x}a3P/i1y4234SJPIq PiiS2 4J43W
B#( 7*+& * k IH * 1 XIH *ll> ?
@@ gpuwj ojstv{ 6A-#_//
6A-#_// :A/(2#+ k <<6A-#_// '--#,= *+*&=H
@@ z{wfqwquwj ujuw sj{jjuur ojstv{
<A/(2#+ 2/&E/*84&K+*-#2\I l *=H
<A/(2#+ 2/&D/*84&K+/&/(2\X3W U *@XI3I=H
<A/(2#+ 2/&6A-#_//KO\*=H
@@ otjuwj gjon 6A-#_// q ffwq /6A-#_//2
</6A-#_//2 '$$M/,&\A/(2#+=H
@@ zj{qr {gnuwo~
4K L4 jj bV z
112a3451Q Q13./0123H1IQSJ KSI1qM7.MW
|
@@ ng{g {gnuwo~
4K L4 jj !V z
112a3451Q Q13./0123H1IQSJ KSI1qM7o.MW
|
J
333
P%c#87C'--!22/&2\ C9 '--!22/&2>H
xShLM112a3451Q rM` 112a3451QVW
112a3451Q j J4iW
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
138
P%b(/$*,'&/ :A(/$*,'&/ k <P%b(/$*,'&/ A(/$*,'&/E*&4`#(6'&\
C4#-$/(3;'-./B!22/&2 5 YIC=H
P%!(('_ :&#a/N/,-'*6/$ k <'--!22/&2
B*-&/(/$!(('_Q2*+8b(/$*,'&/\A(/$*,'&/=H
P%c#87C&#a/N/,-'*6/$\ C9 &#a/N/,-'*6/$>H
&#a/N/,-'*6/$ k +*-H
P%c#87C"*;*+8 .A #L+/(24*A #B '(('_2C>H
'--!22/&2 k +*-H
/6A-#_//2 k +*-H
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy Coepnmoe cnonap nrnon+c na konconr:
/d/,.&*;/2 k ?
^ k C16A-#_// I\ I *+ '22/&25CH
^Z k C16A-#_// X\ XTS *+ '22/&25CH
J
ptg999
Chapter 21Coecton Casses
142
Fgure 21.3Two nstances of Empoyee n an NSMutabeDctonary
The keys in a dictionary are unique. If you try to add a second object under a string, the first key-value
pair gets replaced.
// CJea1e a d1c11ohaJy
hShu1abe01c11ohaJy *1J1ehds = |hShu1abe01c11ohaJy d1c11ohaJy],
// Pu1 ah obec1 uhdeJ key "bes1FJ1ehd"
|1J1ehds se10bec1.be11y 1oJKey._"bes1FJ1ehd"],
// Repace 1ha1 obec1 w11h a hew bes1 1J1ehd
|1J1ehds se10bec1.ahe 1oJKey._"bes1FJ1ehd"],
// 1J1ehds has ohy ohe key-vaue pa1J {bes1FJ1ehd => ahe)
C primitive types
The collections covered in this chapter only hold objects. What if you want a collection of 1oa1s or
1h1s or pointers to structures? You can wrap common C primitive types in an object. There are two
classes that are designed specifically for this purpose. N5Number holds C number types. N5va1ue can
hold a pointer and some types of s1Juc1s.
For instance, if you wanted to put the numbers 4 and 5.6 into an array, you would use N5Number:
hShu1abeAJJay *humL1s1 = ||hShu1abeAJJay aoc] 1h11],
|humL1s1 add0bec1.|hShumbeJ humbeJW11hTh1.4]],
|humL1s1 add0bec1.|hShumbeJ humbeJW11hFoa1.5.6]],
Collections and nil
You are not allowed to add h1 to any of the collection classes we have covered. What if you need to
put that idea of nothingness, a hole, into a collection? There is a class called N5Nu11. There is exactly
one instance of N5Nu11, and it is an object that represents nothingness. Here`s an example:
Pnc. 21.3. na .ksemnnpa Employee n NSMutableDictionary
Knmnn cnonap ynnkanrnr. Hpn nonr+ke o6annenn n+oporo o6rek+a c ye
cymec+nymmnm knmnom nponcxon+ samena nepnon napr knmn/snanenne.
@@ gpuwj ftgq{v
P%.&'M-jO*,&*g+(x :B(*/+$2 k <P%.&'M-jO*,&*g+(x $*,&*#+'(_=H
@@ otjuwj gjon f otg CM/2&`(*/+$C
<B(*/+$2 2/&M/,&\M/&&_ B#(/_\CM/2&`(*/+$C=H
@@ ju gjon
<B(*/+$2 2/&M/,&\'+/ B#(/_\CM/2&`(*/+$C=H
@@ B(*/+$2 fgpj{ywn ngtog gpux s{x Cot@ujuwjC 7M/2&`(*/+$ k5 '+/>
Hpnunvnnnme vnnm C
B konneknnx, paccma+pnnaemrx n .+on rnane, mory+ xpann+rc +onrko o6rek+r. A
ecnn nam nonao6n+c konneknn B-#'&, *+& nnn ykasa+enen na c+pyk+ypr?
Hpnmn+nnnre +nnr C mono ynakona+r n o6rek+nym o6ep+ky. Cymec+nye+ na
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
139
knacca, npenasnanennrx cnennanrno n .+on nenn. B P%P.6M/( xpan+c nncnonre
+nnr C, a P%'X./ moe+ coepa+r ykasa+enr n neko+opre +nnr c+pyk+yp.
Hanpnmep, n coxpanenn n maccnne nncen 4 n 5,6 cneye+ ncnonrsona+r
P%P.6M/(:
P%.&'M-/!(('_ :+.6c*2& k <<P%.&'M-/!(('_ '--#,= *+*&=H
<+.6c*2& '$$M/,&\<P%P.6M/( +.6M/(E*&4K+&\]==H
<+.6c*2& '$$M/,&\<P%P.6M/( +.6M/(E*&4`-#'&\T3R==H
Konneknnn n nil
B knaccr konneknnn, npec+annennrx n .+on rnane, ne onyckae+c xpanenne nil. A ecnn
nam nonao6n+c nomec+n+r n konneknnm nyc+on .nemen+, nnn+o? n .+oro
ncnonrsye+c cnennanrnrn knacc NSNull. On cymec+nye+ +onrko n onom .ksemnnpe, n
.+o+ o6rek+ npec+anne+ nnn+o. Hpnmep:
P%.&'M-/!(('_ :4#&/X k <<P%.&'M-/!(('_ 'XX#,= *+*&=H
@@ sj{qg nyj qjfnwt
<4#&/- '$$M/,&\-#MM_=H
@@ qng{g nyj3 ffju
<4#&/X '$$M/,&\A##-=H
@@ i{jnw ny jj uj fn{gju
<4#&/X '$$M/,&\<P%P.-- +.--==H
@@ jnqj{ng nyj fstuw
<4#&/- '$$M/,&\M/$(##62=H
npawnenne
Hpocmo+pn+e cnpanonnre c+pannnr knaccon P%!(('_, P%.&M-j((x, P%O*,&*#+'(_
n P%.&M-jO*f&*g+(x.
Br 6ye+e noc+onno ncnonrsona+r .+n knaccr n cnoen pa6o+e.
Fnana 21. Knaccm konnekqu
________________________________________________________________________________________________
140
CC" 8;+B()+(%
Mr ononrno nopo6no paccmo+penn nepemennre, ko+opre, kak cneye+ ns camoro
nasnann, nsmenm+ cnon snanenn n xoe nrnonnenn nporpammr. Onako cymec+nym+
n +akne annre, snanenn ko+oprx "$ nsmenm+c (kak, nanpnmep, ma+ema+nnecka
noc+onna A*), Takne annre nasrnam+c /'"+#)"#)3!. B Objective-C cymec+nye+
na pacnpoc+panennrx cnoco6a onpeenenn konc+an+: )$/B*+/ n rno6anrnre
nepemennre.
Cosan+e n Xcode nonym nporpammy komannon c+pokn Foundation c nmenem
^#+2&'+&2.
B c+anap+nrx 6n6nno+ekax C konc+an+r onpeenm+c npek+nnon
npenponeccopa )$/B*+/. Ma+ema+nnecka nac+r c+anap+non 6n6nno+ekn C
o6rnne+c B qanne math.h. B nac+noc+n, +am onpeene+c konc+an+a ebK.
Hcnonrsyem ee n main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
FF > Z&(<9)Z)[ xx3I4Jh` @9;&_>;Z]+%< B&A>;Z% +&*;l) ;@9<l<ZE(BE
B&A>;Z% Ua
xShLMUabc2b 4Q rKM` }{NVW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Ona nrnon+ cneymmnn pesynr+a+:
*2 S3X]XTS
H+o6r npocmo+pe+r onpeenenne konc+an+r ebK, menknn+e na nen n peak+ope,
yepnna naa+on knannmy ^#66'+$. Eennok o+kprnae+ cneymmym c+poky n
math.h:
)$/B*+/ ebK S3X]XTVRTSTWYSVSW]RVR]SSWSVYTIVWW
npekvnnm npenponeccopa
Komnnnnponanne qanna c koom C, C++ nnn Objective-C ocymec+nne+c sa na
npoxoa. Cnanana qann o6pa6a+rnae+c npenponeccopom, nocne nero pesynr+a+
o6pa6o+kn nepeae+c komnnn+opy. npek+nnr npenponeccopa nannnam+c c cnmnona
); +pn nan6onee nac+o ncnonrsyemrx npek+nnr - )*+,-.$/, )*6A#(& n )$/B*+/.
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
141
#include n #import
npek+nnr )*+,-.$/ n )*6A#(& enam+ npak+nneckn ono n +o e: onn npnkasrnam+
npenponeccopy nponn+a+r qann n nknmnn+r ero n nrxonre annre. O6rnno onn
ncnonrsym+c n nknmnenn qannon o6rnnennn (c pacmnpennem .h); o6rnnenn
nynr komnnn+opy n 6onee nonnoro nonnmann komnnnnpyemoro koa.
Hem )*+,-.$/ o+nnnae+c o+ )*6A#(&? npek+nna )*6A#(& rapan+npye+, n+o
npenponeccop nknmnn+ qann +onrko onn pas. npek+nna )*+,-.$/ nosnone+
mnorokpa+no nknmnn+r onn qann. Hporpammnc+r C name ncnonrsym+ )*+,X.$/9 a
nporpammnc+r Objective-C npenonn+am+ )*6A#(&.
Hm nmnop+npyemoro qanna moe+ 6r+r saknmneno n kanrnkn nnn yrnonre
cko6kn. Kanrnkn osnanam+, n+o saronononnrn qann naxon+c n ka+anore npoek+a.
Yrnonre cko6kn osnanam+, n+o saronononnrn qann naxon+c n onom ns c+anap+nrx
ka+anoron, nsnec+nrx npenponeccopy. (Hanpnmep, 16'&4345 naxon+c n ka+anore /
Developer/SDKs/MacOSX10.7.sdk/usr/include/math.h.) na npnmepa npek+nnr )*6A#(&:
@@ otjuwj |gtgqogq9 uswfuur ug ptv gsj{w b/& %&#(/
)*6A#(& Cb/&%&#(/34C
@@ otjuwj |gtgqogo ptv wtwgnjo A/+cO!b
)*6A#(& 1-$'A345
Kora-+o n npoek+ax karn qann C nporpammnrm koom nannnanc c
nknmnenn na6opa sarononkon. 3+o npnnonno k sarpomoennm nanana qanna n
samenennm komnnnnnn. H+o6r nporpammnc+y 6rno npome pa6o+a+r, a nporpammr
6rc+pee komnnnnponanncr, n 6onrmnnc+ne npoek+on Xcode nmee+c qann c nepennem
sarononkon, ko+opre onnr 6r+r npenapn+enrno o+komnnnnponanr n nknmnenr n
karn qann. B npoek+e ^#+2&'+&2 .+o+ qann nasrnae+c Constants-Prex.pch.
H+ak, kak e nponcxon+ nknmnenne konc+an+r ns math.h no npem
komnnnnponann main.m? Ham qann main.m coepn+ cneymmym c+poky:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
dann F'und)ti'n.h coepn+ c+poky:
)*+,-.$/ 1^#(/`#.+$'&*#+@^#(/`#.+$'&*#+345
dann B'reF'und)ti'n.h coepn+ c+poky:
)*+,-.$/ 16'&4345
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
142
#dene
npek+nna )$/B*+/ npnkasrnae+ npenponeccopy: Karn pas, kora +r nc+penaemr ,
samenn ero na eme o +oro, kak komnnn+op ynnn+ ko. Eme pas nsrnnn+e na c+poky
ns math.h:
)$/B*+/ ebK S3X]XTVRTSTWYSVSW]RVR]SSWSVYTIVWW
B npek+nne )$/B*+/ ne nac+n (cnmnonnneckoe nm n ero samena) pasenm+c
npo6enamn.
Boo6me ronop, c nomomrm npek+nnr )$/B*+/ mono cosa+r nekoe noo6ne
qynknnn. Bknmnn+e n main.m komany nrnoa 6onrmero ns nyx nncen:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%c#87CG.IS,I *2 BC9 ebK>H
xShLMrw 4Q iPIh1IM` }L!b` !CVVW
J
(/&.(+ IH
J
- ne qynknn, a )$/B*+/. Hpoc+enma nepcn na srke C nrrnn+ +ak;
)$/B*+/ !7!9a> 77!> 5 7a> ~ 7!> \ 7a>>
H+ak, k +omy momen+y, kora komnnn+op ynnn+ o6annennym c+poky, ona
npnme+ cneymmnn nn:
P%c#87C$ *2 -'(8/(C9 77XI> 5 7XV> ~ 7XI> \ 7XV>>>H
Hcnonrsy )$/B*+/ n nmn+annn qynknnn (nmec+o npoc+on samenr snanennn),
nr cosae+e 3)/&''0&$,$1$"!$, nnn 3)/&'+.
Ino6annme nepeuennme
Bmec+o )$/B*+/ nporpammnc+r Objective-C o6rnno ncnonrsym+ n xpanenn
noc+onnrx snanennn rno6anrnre nepemennre.
anan+e ononnnm namy nporpammy. Knacc c nmenem P%c#,'-/ coepn+
1'/)19"<4 /'"#$/+#, +o ec+r onncanne c+anap+on, enc+nymmnx n onpeenennom
reorpaqnneckom mec+e. Hanpnmep, nr moe+e nonynn+r .ksemnnp +ekymero
nokanrnoro kon+ekc+a n nonrsona+en, a sa+em o6pama+rc k nemy sa nnqopmannen.
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
143
Hanpnmep, cenn nr xo+n+e ysna+r, kaka enena ennnna ncnonrsye+c n nokanrnom
kon+ekc+e nonrsona+en, .+o enae+c +ak:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%c#87CG.IS,I *2 BC9 ebK>H
P%c#87C$ *2 -'(8/(C9 !7XI9 XV>>H
xS2Pi1 XR1I1 j xS2Pi1 2aII1J3S2Pi1W
xx3I4Jh X2aII1J2q j R1I1 S/0123SI1qM2aII1J2qMW
xShLM}SJ1q 4Q rM` 2aII1J2qVW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. B sanncnmoc+n o+ +oro, re nr naxon+ecr,
pesynr+a+ moe+ nrrne+r npnmepno +ak:
#+/_ *2 Q%O
Ho ecnn npn nnoe knmna 6ye+ onymena omn6ka (nanpnmep,C.(.+fxC), nr ne
nonynn+e nnkaknx annrx. n npeo+npamenn noo6nrx npo6nem n 6n6nno+eke
Foundation onpeene+c rno6anrna nepemenna c nmenem P%c#,'X/^.((/+,_^#$/.
Bp nn ee npome nnon+r, no sa+o ecnn npn nnoe 6ye+ onymena omn6ka, komnnn+op
ee same+n+. Kpome +oro, an+osanepmenne koa n Xcode nopmanrno pa6o+ae+ n
rno6anrnon nepemennon, no ne n c+pokn C,.((/+,_C. Hsmenn+e ko +ak, n+o6r n nem
ncnonrsonanacr konc+an+a:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%c#87CG.IS,I *2 BC9 ebK>H
P%c#87C$ *2 -'(8/(C9 !7XI9 XV>>H
P%c#,'-/ :4/(/ k <P%c#,'-/ ,.((/+&c#,'-/=H
P%%&(*+8 :,.((/+,_ k <4/(/ #M/,&`#(/_\xS2Pi17aII1J2q7Sw1=H
P%c#87C#+/_ *2 C9 ,.((/+,_>H
J
(/&.(+ IH
J
Hpn nanncannn macca P%c#,'X/ .+a rno6anrna nepemenna nc+penanacr n nyx
mec+ax. B qanne NSLocale.h nepemenna 6rna o6rnnena npnmepno +ak
/d&/(+ P%%&(*+8 ,#+2& :P%c#,'-/^.((/+,_^#$/H
Knmnenoe cnono fg+2& ronopn+ o +om, n+o ykasa+enr ne 6ye+ nsmen+rc n
+enenne ncero nsnennoro nnkna nporpammr. Knmnenoe cnono /d&/(+ osnanae+: B
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
144
rapan+npym, n+o .+o cymec+nye+, no ono onpeene+c n pyrom qanne. H
enc+nn+enrno, qann NSLocale.m coepn+ c+poky cneymmero nna:
P%%&(*+8 ,#+2& :P%c#,'-/^.((/+,_^#$/ k C,.((/+,_CH
enum
Hac+o n nporpamme +pe6ye+c onpeenn+r na6op konc+an+. onyc+nm, nr
nporpammnpye+e 6nenep c n+rm pa6onnmn penmamn. Bam knacc aX/+$/( coepn+
me+o 2/&%A//$:. Brno 6r xopomo, ecnn 6r +nn aprymen+a ykasrnan, n+o
onyc+nmrmn nnm+c +onrko n+r konkpe+nrx snanennn. n .+oro n nporpamme
onpeene+c 0$&$=!+1*$3<4 #!0, nnn 0$&$=!+1$"!$:
/+.6 a-/+$/(%A//$ ?
a-/+$/(%A//$%&*( k X9
a-/+$/(%A//$^4#A k V9
a-/+$/(%A//$c*.*B_ k T9
a-/+$/(%A//$b.-2/ k 9
a-/+$/(%A//$K,/^(.24 k XT
JH
*+&/(B',/ a-/+$/( \ P%M/,&
?
@@ svn pgsxfnwr ujuw fog{gfnw
/+.6 a-/+$/(%A//$ 2A//$H
J
@@ 2/&%A//$\ sgtxjn gpug w svnw pgsxfnwr ujuw
U 7;#*$>2/&%A//$\7/+.6 a-/+$/(%A//$>dH
/+$
H+o6r paspa6o+nnkam ne npnxonnocr noc+onno nnon+r /+.6 a-/+$/(%A//$,
onn nac+o onpeenm+ cokpamennym sanncr c ncnonrsonannem
&_A/$/B /+.6 ?
a-/+$/(%A//$%&*( k X9
a-/+$/(%A//$^4#A k V9
a-/+$/(%A//$c*.*B_ k T9
a-/+$/(%A//$b.-2/ k 9
a-/+$/(%A//$K,/^(.24 k XT
J a-/+$/(%A//$H
*+&/(B',/ a-/+$/( \ P%M/,&
?
@@ svn pgsxfnwr ujuw fog{gfnw
a-/+$/(%A//$ 2A//$H
J
@@ 2/&%A//$\ sgtxjn gpug w svnw pgsxfnwr ujuw
U 7;#*$>2/&%A//$\7a-/+$/(%A//$>dH
/+$
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
145
Hac+o n nporpammnc+a conepmenno nenano, kaknmn nncnamn npec+annenr
n+r ckopoc+en - nnmr 6r onn o+nnnanncr pyr o+ pyra. Bnanenn .nemen+on
nepenncnenn mono ne ykasrna+r, n .+om cnynae komnnn+op crenepnpye+ nx
an+oma+nneckn:
&_A/$/B /+.6 ?
a-/+$/(%A//$%&*(9
a-/+$/(%A//$^4#A9
a-/+$/(%A//$c*.*B_9
a-/+$/(%A//$b.-2/9
a-/+$/(%A//$K,/^(.24
J a-/+$/(%A//$H
#dene n rno6annme nepeuennme
Ecnn n onpeenenn konc+an+ mono ncnonrsona+r kak )$/B*+/, +ak n rno6anrnre
nepemennre (nknmna /+.6), nonemy nporpammnc+r Objective-C o6rnno ncnonrsym+
rno6anrnre nepemennre? Hnora rno6anrnre nepemennre nonrmam+ 6rc+poenc+nne
nporpammr. Hanpnmep, npn pa6o+e c rno6anrnon nepemennon n cpannenn c+pok
mono ncnonrsona+r == nmec+o *2.'-\ (a ma+ema+nnecka onepann nrnonne+c
6rc+pee o+npankn coo6menn). Kpome +oro, c rno6anrnrmn nepemennrmn yo6nee
pa6o+a+r n o+nannke.
Hcnonrsyn+e n konc+an+ rno6anrnre nepemennre n /+.6, a ne )$/B*+/.
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
146
Cc" )@&B] > m)fZ% B
&B@;Z]_;>)+&<A xx3I4Jh
& xyP3P
Bn6nno+eka Foundation npeoc+anne+ paspa6o+nnky neckonrko npoc+rx mexannsmon
n+enn n sanncn n qannr. Heko+opre ns nnx 6yy+ paccmo+penr n .+on rnane.
3annc NSString n qann
Hannem c coxpanenn n qanne coepnmoro P%%&(*+8. Hpn sanncn c+pokn n qann
neo6xonmo ykasa+r ncnonrsyemym konponky, +o ec+r cnoco6 npec+annenn kaoro
cnmnona n nne maccnna 6an+on. B konponke %^KK 6ykna '' xpann+c n nne IXIIIIIX,
a n konponke QZ`-XR +a e 6ykna '' npec+anne+c n nne IIIIIIIIIXIIIIIX.
Bn6nno+eka Foundation noepnnae+ okono 20 nnon konponkn c+pok.
Konponka QZ` cnoco6na cnpann+rc ae c camrmn .kso+nnecknmn anqann+amn. Ona
cymec+nye+ n nyx pasnonnnoc+x: n QZ`-XR karn cnmnon npec+anne+c nym n
6onee 6an+amn, a n QZ` UW n nepnrx XVW cnmnonon %^KK ncnonrsye+c onn 6an+, a
n ncex oc+anrnrx cnmnonon - na n 6onee 6an+a. n mnornx npak+nnecknx saan
QZ`-W okasrnae+c oc+a+onno ..
Cosan+e nonrn npoek+: nporpammy komannon c+pokn `#.+$'&*#+ c nmenem
%&(*+8. B qynknnn 6'*+() me+oamn knacca P%%&(*+8 mr cosanm c+poky n sannmem
ee n qannonym cnc+emy:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=> ?
'.&#(/-/'2/A##- ?
x}a3P/i1x3I4Jh XQ3I j x}a3P/i1x3I4Jh PiiS2 4J43W
KSI L4J3 4 j bW 4 !bW 4ttV z
Q3I PHH1Jwx3I4JhMPISJ 4Q 2SSiUJMW
|
Q3I OI431oS4i1MF3THF2SSi"33M
P3ST42Piiqx
1J2Sw4Jhxox3I4JhJ2Sw4Jh
1IISIW
xShLMwSJ1 OI434Jh F3THF2SSi"33MVW
J
(/&.(+ IH
J
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
147
Hporpamma cosae+ +ekc+onrn qann, ko+oprn mono nn+a+r n peak+npona+r n
nm6om +ekc+onom peak+ope. C+poka @&6{@fgg-3&d& coepn+ ny+r k qanny. Hy+n
mory+ 6r+r a6conm+nrmn nnn o+nocn+enrnrmn; a6conm+nrn ny+r nannnae+c c
cnmnona /, npec+annmmero kopnenon yponenr qannonon cnc+emr, a o+nocn+enrnre
ny+n nannnam+c c pa6onero ka+anora nporpammr. O+nocn+enrnrn ny+r ne moe+
nannna+rc c @. B nporpammnponannn Objective-C non+n ncera ncnonrsym+c
a6conm+nre ny+n, no+omy n+o mr ne snaem pa6onnn ka+anor nporpammr. (H+o6r nan+n
ka+anor @&6A n `*+$j(, ncnonrsyn+e komany menm "# U5 "# &# `#-$/(.)
NSError
Honr+ka sanncn c+pokn n qann moe+ okasa+rc neyannon no mnornm npnnnnam.
Hanpnmep, nonrsona+enm moe+ 6r+r sanpemena sanncr n ka+anor, n ko+opom
naxon+c qann. Hnn ka+anor moe+ noo6me ne cymec+nona+r. Hnn n qannonon
cnc+eme ne nane+c cno6onoro mec+a. n noo6nrx cn+yannn, kora sanepmenne
onepannn nenosmono, me+o onen kaknm-+o o6pasom nepny+r onncanne omn6kn
kpome nornneckoro npnsnaka ycnexa nnn neyann.
Kak ronopnnocr n rnane 9, ecnn qynknn onna nepny+r kakym-nn6o
nnqopmannm nomnmo cnoero nosnpamaemoro snanenn, cneye+ ncnonrsona+r mexannsm
nepeann no ccrnke. dynknnn (nnn me+oy) nepeae+c ccrnka na nepemennym, n
ko+opon coxpane+c nynoe snanenne nnn nrnonnm+c pyrne onepannn. Ccrnka
npec+anne+ co6on apec nepemennon n nam+n. n o6pa6o+kn omn6ok mnornm
me+oam nepeae+c no ccrnke ykasa+enr na P%((#(. Bknmnn+e o6pa6o+ky omn6ok n
npnneennrn nrme npnmep:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=> ?
'.&#(/X/'2/A##X ?
P%.&'M-/%&(*+8 :2&( k <<P%.&'M-/%&(*+8 'XX#,= *+*&=H
B#( 7*+& * kIH * 1XIH *ll> ?
<2&( 'AA/+$%&(*+8\C!'(#+ *2 ,##XFG+C=H
J
@@ Spjf xonjt u gjon P%((#( gvqtvjnfv j fgpuwv
@@ ojstv{3 Sojstv{ P%((#( xpj( fgpu ngtog s{w qguwougqjuww g}wow3
P%((#( :j((#( k +*-H
@@ zj{jp sg ffrtoj xonjtv P%((#( jngpx P%%&(*+8
aIIc 2.,,/22 k <2&( L(*&/Z#`*-/\C@&6A@,##-3&d&C
'&#6*,'--_\%
/+,#$*+8\P%QZ`W%&(*+8+,#$*+8
/((#(\/((#(=\
@@ z{gqj{o sgtxjuug|g t| w qrqgp wug{ww w P%((#( q ftxj g}wow
swfw
*B 72.,,/2> ? J /X2/ ?
P%c#87C$#+/ L(*&*+8@&6A@,##-3&d&C>H
J
P%c#87CL(*&*+8@&6A@,##-3&d& B'*-/$\ C9 <j((#( -#,'-*/$O/2,(*A&*#+=>H
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
148
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. Hsmenn+e ko +aknm o6pasom, n+o6r me+oy
sanncn nepeananc necymec+nymmnn ny+r k qanny - nanpnmep, C@&##@$'(+/$@
M'$3&d&C. Hporpamma onna nrnec+n nnqopma+nnnoe coo6menne o6 omn6ke.
O6pa+n+e nnnmanne: n .+om koe mr o6rnnem ykasa+enr na .ksemnnp P%((#(,
no ne cosaem .ksemnnp P%((#(, ko+oprn c .+nm ykasa+enem cnsrnae+c.
Honemy? He c+on+ cosana+r o6rek+ omn6kn, ecnn omn6kn ne 6rno. Ecnn
nponsone+ omn6ka, +o coo6menne L(*&/i#`*-/\'&#6*,'--_\/+,#$*+8\j((#(\
o6ecnenn+ cosanne nonoro .ksemnnpa P%((#( c nocneymmnm nsmenennem ykasa+en
error, n+o6r on ykasrnan na cosannrn o6rek+. anee nr cmoe+e no ykasa+enm error
nonynn+r pacmnpennym nnqopmannm o6 omn6ke.
Ycnonnoe cosanne P%((#( +pe6ye+ nepeann ccrnkn na error 7/((#(), no+omy
n+o o6rek+, ko+oprn mono 6rno 6r nepea+r, eme ne cymec+nye+. Onako n o+nnnne
o+ nepeann no ccrnke, ko+opa ncnonrsonanacr n rnane 9 n npnmn+nnnrx
nepemennrx C, n annom cnynae nepeae+c apec nepemennon-ykasa+en. dak+nneckn
mr nepeaem apec pyroro apeca (ko+oprn moe+ c+a+r apecom o6rek+a P%((#().
Bosnpamacr k namemy npnmepy co mnnonamn ns rnanr 9, nr moe+e npnkasa+r
cnoemy noonennomy: Ecnn n+o-+o none+ ne +ak, coc+anr nonnrn o+ne+ (ko+oprn n
+annnke ne nomec+n+c) n sanon ero n knnry n 6n6nno+eke. A n+o6r ysnan, re
cnp+an o+ne+, nonon n +py6y 6n6nno+ennrn mnqp knnrn. To ec+r nr coo6mae+e
mnnony mec+o, n ko+opom on moe+ cnp+a+r apec cosannoro nm o+ne+a o6 omn6ke.
Barnnem nonny+pr knacca P%%&(*+8, n ko+opom o6rnne+c L(*&/Z#`*-/\
'&#6*,'--_\/+,#$*+8\/((#(\\
U 7ac>L(*&/Z#`*-/\7P%%&(*+8 :>A'&4
'&#6*,'--_\7ac>.2/!.d*-*'(_`*-/
/+,#$*+8\7P%%&(*+8+,#$*+8>/+,
/((#(\7P%((#( ::>/((#(
O6pa+n+e nnnmanne na nonnym snesonky. Hpn nrsone .+oro me+oa mr
nepeaem 6/)-)#$19 ") 6/)-)#$19 na .ksemnnp P%((#(.
Me+or, nepeammne P%((#( no ccrnke, ncera nosnpamam+ snanenne, no
ko+opomy mono onpeenn+r, nponsomna omn6ka nnn ne+. Hanpnmep, npn nannnnn
omn6kn .+o+ me+o nosnpamae+ P. He nr+an+ecr o6pa+n+rc k P%((#(, ecnn
nosnpamaemoe snanenne ne ykasrnae+ na nannnne omn6kn; ecnn o6rek+ P%((#( ne
cymec+nye+, +o nonr+ka o6pamenn k nemy npnnee+ k c6om nporpammr.
Hvenne qannon c ncnonsonanneu NSString
H+enne coepnmoro qanna n c+poky nrnonne+c ananornnnrm o6pasom:
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
149
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=> ?
'.&#(/-/'2/A##- ?
xIISI X1IISI j J4iW
xx3I4Jh XQ3I j xx3I4Jh PiiS2 4J4343R7SJ31J3Q.K4i1MF132F
I1QSi5"2SJKM

1J2Sw4Jhxx7NNx3I4JhJ2Sw4Jh
1IISI1IISIW
4K LQ3IV z
xShLMI1Pw KP4i1w rM` 1IISI iS2Pi41wy1Q2I4H34SJVW
| 1iQ1 z
xShLMI1QSi5"2SJK iSSpQ i4p1 3R4Q rM` Q3IVW
|
J
(/&.(+ IH
J
3annc o6+ekva NSData n qann
O6rek+ P%O'&' npec+anne+ annre. Hanpnmep, npn sarpyske annrx no URL-apecy
nr nonynae+e .ksemnnp P%O'&', ko+opomy mono npnkasa+r sannca+r ce6 n qann.
Cosan+e nonym nporpammy komannon c+pokn `#.+$'&*#+ c nmenem K6'8/`/&,4; .+a
nporpamma 6ye+ sarpya+r nso6paenne c can+a Google n .ksemnnp P%O'&'. Ba+em
npnkan+e P%O'&' sannca+r cnon 6yqep annrx n qann:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
x XaIi j x 43Rx3I4Jh
MR33HFFOOO"hSShi1"2STF4TPh1QFiShSQFHQiShSC"HJhMW
x1a1Q3 XI1a1Q3 j x1a1Q3 I1a1Q343RaIiW
xIISI X1IISI j J4iW
xyP3P XwP3P j x7SJJ1234SJ Q1JwxqJ2RISJSaQ1a1Q3I1a1Q3
I13aIJ4Jh1QHSJQ1
1IISI1IISIW
4K LwP3PV z
xShLMK132R KP4i1w rM` 1IISI iS2Pi41wy1Q2I4H34SJVW
I13aIJ !W
|
xShLMoR1 K4i1 4Q ria /q31QM` wP3P i1Jh3RVW
~.. OI4331J j wP3P OI431oS4i1MF3THFhSShi1"HJhM
SH34SJQb
1IISI1IISIW
4K LOI4331JV z
xShLMOI431 KP4i1w rM` 1IISI iS2Pi41wy1Q2I4H34SJVW
I13aIJ !W
|
xShLMxa221QQMVW
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
150
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy o+kpon+e cosannrn rpaqnnecknn qann n
Preview.
Me+o L(*&/Z#`*-/\#A&*#+2\/((#(\ nonynae+ nncno, o6osnanammee
nac+ponkn nponecca coxpanenn. Hame ncero ncnonrsye+c snanenne
P%O'&'E(*&*+8!&#6*,. onyc+nm, nso6paenne ye 6rno sarpyeno, a cennac nr
sarpyae+e ero sanono n samene+e nonon nepcnen. Bo npem sanncn nonoro
nso6paenn nnesanno o+knmnae+c nn+anne. Hanononnny sanncannrn qann
6ecnonesen. B +ex cnynax, kora nanononnny sanncannrn qann xye, nem nnkakoro,
mono o6rnn+r onepannm a+omapnon. o6anr+e coo+ne+c+nymmnn napame+p:
P%c#87CZ4/ B*-/ *2 -. M_&/2C9 <$'&' -/+8&4=>H
ac L(*&&/+ k <$'&' L(*&/Z#`*-/\C@&6A@8##8-/3A+8C #A&*#+2\xyP3PI434Jh3ST42
/((#(\/((#(=H
*B 7FL(*&&/+> ?
P%c#87CL(*&/ B'*-/$\ C9 </((#( -#,'-*/$O/2,(*A&*#+=>H
(/&.(+ XH
J
annre sanncrnam+c no npemennrn qann, ko+opomy nocne sanepmenn sanncn
npncnannae+c npannnrnoe nm. Taknm o6pasom nr nonynae+e nn6o necr qann, nn6o
nnnero.
Hvenne NSData ns qanna
3ksemnnp P%O'&' +ake mono cosa+r na ocnone coepnmoro qanna. Bknmnn+e n
nporpammy ne c+pokn:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%QNc :.(- k <P%QNc QNcE*&4%&(*+8\ C4&&A\@@LLL38##8-/3,#6@*6'8/2@
-#8#2@A2e-#8#V3A+8C=H
P%QNcN/./2& :(/./2& k <P%QNcN/./2& (/./2&E*&4QNc\.(-=H
P%((#( :/((#(H
@@ ngn jngp tgow{xjnfv9 sgo qfj puurj uj xpxn wqtjjur
P%O'&' :$'&' k <P%QNc^#++/,&*#+ 2/+$%_+,4(#+#.2N/./2&\(/./2&
(/&.(+*+8N/2A#+2/\PQcc
/((#(\/((#(=H
*B 7F$'&'> ?
P%c#87CB/&,4 B'*-/$\ C9 </((#( -#,'-*/$O/2,(*A&*#+=>H
(/&.(+ XH
J
P%c#87CZ4/ B*-/ *2 -. M_&/2C9 <$'&' -/+8&4=>H
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
151
ac L(*&&/+ k <$'&' L(*&/Z#`*-/\C@&6A@8##8-/3A+8C
#A&*#+2\P%O'&'E(*&*+8!&#6*,
/((#(\/((#(=H
*B 7FL(*&&/+> ?
P%c#87CL(*&/ B'*-/$\ C9 </((#( -#,'-*/$O/2,(*A&*#+=>H
(/&.(+ XH
J
P%c#87C%.,,/22FC>H
xyP3P XI1PwyP3P j xyP3P wP3P43R7SJ31J3Q.K4i1MF3THFhSShi1"HJhMW
xShLMoR1 K4i1 I1Pw KIST 3R1 w4Qp RPQ ria /q31QM` I1PwyP3P i1Jh3RVW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 23. 3anucu n qanm c ucnonusonauueu NSString u NSData ______________________________________________________________________________________ __________
152
Ck" d:9)(+%f >%_;>
o nac+omero momen+a ncem pacnopanc nam ko. On o+npannn coo6menn
c+anap+nrm o6rek+am `#.+$'&*#+ (ckaem, .ksemnnpam P%%&(*+8 n P%!(('_) n
o+anan nm npnkasr. o nac+omero momen+a namn nporpammr nrnonnnncr n
sanepmanncr sa cnn+annre mnnnncekynr.
B peanrnom mnpe npnnoenn mory+ nrnonn+rc nacamn, a o6rek+r
nonnnm+c no+oky co6r+nn o+ nonrsona+en, cnc+emnrx nacon, ce+n n +. .
B peanrnom npnnoennn onen cymec+nona+r o6rek+, ko+oprn onae+ +aknx
co6r+nn, kak nepememenn mrmn, npnkocnonenn k .kpany, +anmepr n ce+enre
onepannn. B Mac OS Xn iOS +aknm o6rek+om nne+c .ksemnnp nnkna co6r+nn
P%N.+c##A. O6rnno on cnn+ n e+, a kora n cnc+eme n+o-+o nponcxon+ - o+npanne+
coo6menne pyromy o6rek+y.
Cn+yann, n ko+opon ko onae+ nnemnnx co6r+nn, nasrnae+c '.&)#"<3
(<-'('3. n nporpammnc+on Objective-C cymec+nym+ +pn ocnonnrx qopmr o6pa+noro
nrsona. (Tak kak npnneennre konnennnn nmem+ o6mnn xapak+ep, 6yy o6osnana+r
nponsomemee nen+o 6yknon , a nopo6noc+n 6yy+ npnneenr nne.)

E&!$3"!//,$4+#(!$: nepe nananom onann nr ronopn+e: Kora nponsone+ ,


o+npanr .+o konkpe+noe coo6menne .+omy konkpe+nomy o6rek+y. O6rek+,
nonynammnn coo6menne, nasrnae+c npnemnnkom. Cenek+op coo6menn n ec+r
enc+nne.

Bcnomora+enrnre o6rek+r: nepe nananom onann nr ronopn+e: Becr nmee+c


ncnomora+enrnrn o6rek+, noepnnammnn +non npo+okon. Kora n+o-nn6yr
nponsone+, o+npannn emy coo6menn. (Hpo+okonr nopo6no paccma+pnnam+c n
rnane 25.) Bcnomora+enrnre o6rek+r nac+o nasrnam+c ,$1$%)#)3! nnn
!+#'="!/)3! ,)""<D.

I0'($@$"!*: nmee+c o6rek+, nasrnaemrn 7$"#&'3 '0'($@$"!4. Hepe nananom


onann nr ronopn+e emy: 3+o+ o6rek+ onae+ +aknx-+o ononemennn. Hpn
noc+ynnennn onoro ns nnx o+npanr o6rek+y .+o coo6menne. Kora nponcxon+ ,
o6rek+ o+npanne+ ononemenne nen+py ononemennn, a nocnennn nepecrnae+ ero
namemy o6rek+y.
B .+on rnane mr peannsyem nce +pn +nna o6pa+nrx nrsonon, a +ake pas6epemc,
n kaknx o6c+o+enrc+nax npnmene+c karn ns nnx.
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
153
Mo(en npneunnk/(encvnne
B nac+noc+n, mexannsm npnemnnk/enc+nne. ncnonrsye+c +anmepamn. Hpn cosannn
+anmepa ykasrnae+c saepka, npnemnnk n enc+nne. Hocne nc+enenn saannon
saepkn +anmep o+npanne+ coo6menne enc+nn cnoemy npnemnnky.
Mr cosanm nporpammy c nnknom co6r+nn n +anmepom. Kare ne cekynr
+anmep 6ye+ o+npann+r cnoemy npnemnnky coo6menne enc+nn. Mr cosanm knacc,
.ksemnnp ko+oporo n 6ye+ ncnonrsona+rc n kanec+ne npnemnnka.
Pnc. 24.1. Logger nne+c npnemnnkom n NSTimer
Cosan+e n Xcode nonrn npoek+: nporpammy komannon c+pokn `#.+$'&*#+ c
nmenem ^'--M',h2. Hannem c npoc+oro cosann pa6o+ocnoco6noro nnkna co6r+nn.
Bnecn+e nsmenenn n main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
xaJSSH 2aII1J3aJSSH IaJW
J
(/&.(+ IH
J

Hoc+pon+e n sanyc+n+e nporpammy. O6pa+n+e nnnmanne: me+o (.+ nnkora ne
nosnpamae+ ynpannenne. On nrnonne+c n 6eckonennom nnkne, ona, noka n+o-
nn6yr nponsone+. H+o6r ero npepna+r, neo6xonmo sanepmn+r nporpammy. (Br6epn+e
komany b(#$.,& ! %&#A.)
Tenepr mr cosanm knacc, ko+oprn 6ye+ ncnonrsona+rc n kanec+ne npnemnnka
+anmepa. Cosan+e nonrn qann: knacc Objective-C c nmenem c#88/(, ko+oprn nne+c
cy6knaccom P%M/,&. (Ea6non knacca nrsrnae+c komanon `*-/!P/L!P/L`*-/...)
B qanne Logger.h o6rnn+e me+o enc+nn:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ c#88/( \ P%M/,&
6 L5S4wVQPq.a2RLxo4T1I XV3W
/+$
Me+o enc+nn nonynae+ onn aprymen+ - o6rek+, o+npannmmnn coo6menne
enc+nn. B namem cnynae .+o o6rek+ +anmepa.
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
154
Peannsyn+e n Logger.m npoc+on me+o 2'_.,4::
)*6A#(& Cc#88/(34C
*6A-/6/+&'&*#+ c#88/(
6 L5S4wVQPq.a2RLxo4T1I XV3
z
xShLM.a2RMVW
|
/+$
Pnc. 24.2. Honck me+oa c ykasannrm nmenem
Ha .+on c+ann neo6xonmo cnerka o+nnenrc n noronopn+r o cenek+opax.
Bcnomnn+e, n+o npn o+npanke coo6menn o6rek+y knacc o6rek+a nponepe+, coepn+
nn on me+o c ykasanrm nmenem. Honck nepexon+ nnepx no nenonke nacneonann, noka
kakon-nn6o knacc ne o+ne+n+: a,y men ec+r me+o c +aknm nmenem.
Ec+ec+nenno, +aka nponepka onna nrnonn+rc onenr, onenr 6rc+po. Hpn
ncnonrsonannn qak+nneckoro nmenn me+oa (ko+opoe moe+ 6r+r onenr nnnnrm)
nonck 6ye+ o+nocn+enrno menennrm. n yckopenn nponepkn komnnn+op
npncnannae+ kaomy nmenn me+oa n nporpamme ynnkanrnrn nen+nqnka+op. Ha
c+ann nrnonnenn nmec+o nmenn me+oa ncnonrsye+c ero nen+nqnka+op.
Ynnkanrnoe nncno, npec+annmmee konkpe+noe nm me+oa, nasrnae+c
+$1$/#'&'3. H+o6r cosa+r +anmep, o+npannmmnn coo6menne 2'_.,4: knaccy
c#88/(, neo6xonmo npnkasa+r komnnn+opy onpeenn+r ero cenek+op. n pemenn
.+on saann ncnonrsye+c npek+nna komnnn+opa 2j-jf&g(.
B qanne main.m cosan+e .ksemnnp c#88/( n cenan+e ero npnemnnkom
P%Z*6/(. B kanec+ne enc+nn ykan+e cenek+op 2'_.,4:.
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
155
Pnc. 24.3. H+o nponcxon+ n enc+nn+enrnoc+n
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
4THSI3 MShh1I"RM
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
Shh1I XiShh1I j Shh1I PiiS2 4J43W
xo4T1I X34T1I j xo4T1I Q2R1wai1wo4T1I43Ro4T1NJ31I5PiC"b
3PIh13iShh1I

Q1i123SIQ1i123SILQPq.a2RV
aQ1INJKSJ4i
I1H1P3QxW
<<P%N.+c##A ,.((/+&N.+c##A= (.+=H
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy. (Komnnn+op nrae+ coo6menne o ne
ncnonrsyemon nepemennon, noka ne o6paman+e na nero nnnmann.) Coo6menne .,4!
onno nrnon+rc na konconr kare ne cekynr.
O6pa+n+e nnnmanne: noe+onne nne+c nac+rm cenek+opa. Banncr
2/-/,&#(72'_.,4> ne .knnnanen+na 2j-jf&g(72x.f4\>.
A +enepr cnona nsrnnn+e na npeynpeenne komnnn+opa. dak+nneckn on
ronopn+: Br cosann nepemennym &*6/(, no ne ncnonrsye+e ee. B neko+oprx
cn+yannx (+aknx, kak .+a) n noannenn npeynpeennn neo6xonmo nome+n+r
nepemennym n ykasa+r, n+o ona ne ncnonrsye+c namepenno. n .+on nenn
ncnonrsye+c monqnka+op e.+.2/$. o6anr+e cneymmnn qparmen+.
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
156
aJaQ1w P%Z*6/( :&*6/( k <P%Z*6/( 2,4/$.-/$Z*6/(E*&4Z*6/K+&/(;'-\V3I
&'(8/&\-#88/(
2/-/,&#(\2/-/,&#(72'_.,4\>
.2/(K+B#\+*-
(/A/'&2\%=H
Hoc+pon+e nporpammy cnona. Ha .+o+ pas npeynpeenne ncnesae+.
Bcnouoravennme o6+ekvm
Tanmepr npoc+r. Onn ymem+ ena+r +onrko ono: cpa6a+rna+r n nynrn momen+.
Ho.+omy moenr npnemnnk/enc+nne xopomo noxon+ n nnx. Mnorne npoc+re
.nemen+r nonrsona+enrckoro nn+epqenca (+akne, kak knonkn n mkanr) +oe
ncnonrsym+ mexannsm npnemnnk/enc+nne. A kak nacne+ nero-+o 6onee cnonoro?
B rnane 23 mr ncnonrsonann o6rek+ P%QNc^#++/,&*#+ n sarpyskn annrx c
ne6-cepnepa. Hporpamma pa6o+ana, no y +akoro pemenn ec+r na neoc+a+ka:

Fnannrn nporpammnrn no+ok 6noknpye+c n onannn noc+ynnenn annrx. Ecnn 6r


.+o+ me+o ncnonrsonanc n nac+omem npnnoennn, nonrsona+enrcknn nn+epqenc
nepec+anan 6r pearnpona+r na enc+nn nonrsona+en o momen+a nonynenn annrx.

Me+o ne npeycma+pnnae+ nosmonoc+r o6pa+noro nrsona - nanpnmep, na cnynan,


ecnn ne6-cepnep sanpocn+ nm nonrsona+en n naponr.
Ho .+nm npnnnnam P%QNc^#++/,&*#+ o6rnno ncnonrsye+c acnnxponno. Hnane
ronop, mr nannnaem nr6opky annrx, a sa+em em o6pa+nrx nrsonon npn sanepmennn
sarpyskn, sanpoce ne6-cepnepom yoc+onepennn nonrsona+en nnn omn6ke sarpyskn.
Kak nonynn+r .+n o6pa+nre nrsonr? n .+oro P%QNc^#++/,&*#+ nepeae+c
ncnomora+enrnrn o6rek+. Kora n xoe sarpyskn n+o-+o nponcxon+, noknmnenne
nepeae+ coo6menn ncnomora+enrnomy o6rek+y. Kakne coo6menn? Cosa+enr knacca
P%QNc^#++/,&*#+ onpeennn npo+okon (cnncok o6rnnennn me+oon), ko+opre mory+
6r+r peannsonanr ncnomora+enrnrm o6rek+om. Heko+opre me+or .+oro npo+okona:
U 7P%QNcN/./2& :>,#++/,&*#+\7P%QNc^#++/,&*#+ :>,
L*--%/+$N/./2&\7P%QNcN/./2& :>(/
(/$*(/,&N/2A#+2/\7P%QNcN/2A#+2/ :>(/2H
U 7;#*$>,#++/,&*#+\7P%QNc^#++/,&*#+ :>2/+$/(
$*$N/,/*;/!.&4/+&*,'&*#+^4'--/+8/\7P%QNc!.&4/+&*,'&*#+^4'--/+8/ :>,4H
U 7;#*$>,#++/,&*#+\7P%QNc^#++/,&*#+ :>2/+$/(
$*$N/,/*;/O'&'\7P%O'&' :>$'&'H
U 7;#*$>,#++/,&*#+O*$`*+*24c#'$*+8\7P%QNc^#++/,&*#+ :>2/+$/(H
U 7;#*$>,#++/,&*#+\7P%QNc^#++/,&*#+ :>2/+$/( $*$`'*-E*&4((#(\7P%((#( :>/((#(H
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
157
U 7P%^',4/$QNcN/2A#+2/ :>,#++/,&*#+\7P%QNc^#++/,&*#+ :>2/+$/(
L*--^',4/N/2A#+2/\7P%^',4/$QNcN/2A#+2/ :>,',4/$N/2A#+2/H
Kak nnn+e, P%QNc^#++/,&*#+ nne+ kya 6onee nn+epecnon nsnrm, nem
P%i*6/(. Cennac mr cosanm o6rek+, ko+oprn peannsye+ nac+r me+oon .+oro
npo+okona, n npec+annm ero P%QNc^#++/,&*#+ kak ncnomora+enrnrn o6rek+. n .+on
nenn y P%QNc^#++/,&*#+ nmee+c enera+, na ko+oprn ccrnae+c ykasa+enr c nmenem
$/-/8'&/.
Pnc. 2.4.4. Logger - enera+ o6rek+a NSUrlConnection
n 6'*+() cosan+e o6rek+ P%QNc^#++/,&*#+ n nasnanr+e .ksemnnp c#88/( ero
enera+om:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
)*6A#(& Cc#88/(34C
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
c#88/( :-#88/( k <<c#88/( '--#,= *+*&=H x XaIi j x 43Rx3I4Jh
C4&&A\@@LLL38.&/+M/(83#(8@,',4/@/A.M@VIT@A8VIT3&d&C=H
x1a1Q3 XI1a1Q3 j x1a1Q3 I1a1Q343RaIiW
aJaQ1w x7SJJ1234SJ XK132R7SJJ
j x7SJJ1234SJ PiiS2 4J4343R1a1Q3I1a1Q3
w1i1hP31iShh1I
Q3PI3NTT1w4P31iqxW
ee.+.2/$ P%Z*6/( :&*6/(
k <P%Z*6/( 2,4/$.-/$Z*6/(E*&4Z*6/K+&/(;'-\V3I
&'(8/&\-#88/(
2/-/,&#(\2/-/,&#(72'_.,4\>
.2/(K+B#\+*-
(/A/'&2\%=H
<<P%N.+c##A ,.((/+&N.+c##A= (.+=H
J
(/&.(+ IH
J
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
158
sanpoc
3ksemnnpy c#88/( nonao6n+c .ksemnnp P%.&M-jO& n xpanenn
noc+ynammnx 6an+on. Bknmnn+e coo+ne+c+nymmym nepemennym .ksemnnpa n
J>TT-?X*\
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ c#88/( \ P%M/,& z
x}a3P/i1yP3P X4J2ST4JhyP3PW
|
U 7;#*$>2'_.,4\7P%Z*6/( :>&H
/+$
ijsj{ {jtwxnj ujogng{rj jngpr pjt|n q J>TT-?XY\
)*6A#(& Cc#88/(34C
*6A-/6/+&'&*#+ c#88/(
U 7;#*$>2'_.,4\7P%Z*6/( :>&
?
P%c#87C.,4FC>H
J
FF >%_%>)<(BE @9& *)=l;A @;Z?\<+&& :Z;*) l)++%[
6 L5S4wV2SJJ1234SJLx7SJJ1234SJ XV2SJJ1234SJ
w4w121451yP3PLxyP3P XVwP3P
z
xShLMI121451w ria /q31QM` wP3P i1Jh3RVW
FF <BZ& &_A<+E<A%f ;:e<*( l)++%[ +< B?<B(>?<(` B;_l)(] <,;
4K L4J2ST4JhyP3PV z
4J2ST4JhyP3P j x}a3P/i1yP3P PiiS2 4J43W
|
4J2ST4JhyP3P PHH1JwyP3PwP3PW
|
FF >%_%>)<(BE @9& ;:9):;(*< @;BZ<l+<,; :Z;*)
6 L5S4wV2SJJ1234SJy4w4J4QRSPw4JhLx7SJJ1234SJ XV2SJJ1234SJ
z
xShLMS3 43 PiiMVW
xx3I4Jh XQ3I4Jh j xx3I4Jh PiiS2 4J4343RyP3P4J2ST4JhyP3P
1J2Sw4Jhxox3I4JhJ2Sw4JhW
4J2ST4JhyP3P j J4iW
xShLMQ3I4Jh RPQ ria 2RPIP231IQM` Q3I4Jh i1Jh3RVW
FF 9)B*;A<+(&9?f(< BZ<l?? B(9;*?` \(;:% ?>&l<(] ><B] m)fZ
FF xShLMoR1 ORSi1 Q3I4Jh 4Q rM` Q3I4JhVW
|
FF >%_%>)<(BE > BZ?\)< ;D&:*& @9& _),9?_*< l)++%[
6 L5S4wV2SJJ1234SJLx7SJJ1234SJ XV2SJJ1234SJ
w4wP4i43RIISILxIISI XV1IISI
z
xShLM2SJJ1234SJ KP4i1w rM` 1IISI iS2Pi41wy1Q2I4H34SJVW
4J2ST4JhyP3P j J4iW
|
/+$
O6pa+n+e nnnmanne: mr peannsyem ne nce me+or npo+okona, a +onrko +e,
ko+opre nanr n namen nporpammr.
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
159
Hoc+pon+e n sanyc+n+e nporpammy. Br ynnn+e, n+o annre sarpyam+c c ne6-
cepnepa no 6nokam. B konne sarpyskn enera+ yneomne+c o ee sanepmennn.
H+ak, o nac+omero momen+a mr nnnm cneymmne npannna o6pa+noro nrsona:
npn o+npanke onoro o6pa+noro nrsona onomy o6rek+y Apple ncnonrsye+ mexannsm
npnemnnk/enc+nne. Hpn o+npanke pasnnnnrx nrsonon onomy o6rek+y Apple
ncnonrsye+ ncnomora+enrnrn o6rek+ c npo+okonom. (Hpo+okonr 6onee nopo6no
paccma+pnnam+c n cneymmen rnane.) Bcnomora+enrnre o6rek+r o6rnno nasrnam+c
enera+amn nnn nc+onnnkamn annrx.
A ecnn o6pa+nrn nrson onen nepeana+rc neckonrknm o6rek+am?
Ononemennn
Kora nonrsona+enr nsmene+ na Mac naconon noc, nnqopmann o6 .+om nsmenennn
moe+ npec+ann+r nn+epec n mnornx o6rek+on namen nporpammr. Karn +akon
o6rek+ pernc+pnpye+c n kanec+ne na6nma+en n nen+pe ononemennn. Hpn cmene
naconoro noca nen+py o+npanne+c ononemenne
P%%_2&j6i*6jg+jO*$4+8jPg&*B*f&*g+, a on nepeae+ ero ncem na6nma+enm,
sann+epeconannrm n .+om co6r+nn.
Bnecn+e nsmenenn n main.m, n+o6r sapernc+pnpona+r .ksemnnp c#88/( n
nonynennn ononemennn o6 nsmenennx naconoro noca:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
)*6A#(& Cc#88/(34C
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
c#88/( :-#88/( k <<c#88/( '--#,= *+*&=H xS34K42P34SJ71J31I w1KPai371J31I
Pww./Q1I51IiShh1I
Q1i123SIQ1i123SILSJ17RPJh1V

JPT1xxqQ31To4T1SJ1y4w7RPJh1S34K42P34SJ
S/0123J4iW
P%QNc :.(- k <P%QNc QNcE*&4%&(*+8\
C4&&A\@@LLL38.&/+M/(83#(8@,',4/@/A.M@VIT@A8VIT3&d&C=H
P%QNcN/./2& :(/./2& k <P%QNcN/./2& (/./2&E*&4QNc\.(-=H
ee.+.2/$ P%QNc^#++/,&*#+ :B/&,4^#++
k <<P%QNc^#++/,&*#+ '--#,= *+*&E*&4N/./2&\(/./2&
$/-/8'&/\-#88/(
2&'(&K66/$*'&/-_\%=H
ee.+.2/$ P%Z*6/( :&*6/(
k <P%Z*6/( 2,4/$.-/$Z*6/(E*&4Z*6/K+&/(;'-\V3I
<<P%N.+c##A ,.((/+&N.+c##A= (.+=H
J
(/&.(+ IH
J
Brsrnaemrn me+o peannsye+c Logger.m:
6 L5S4wVSJ17RPJh1LxS34K42P34SJ XVJS31
z
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
160
xShLMoR1 QqQ31T 34T1 SJ1 RPQ 2RPJh1wMVW
|
Hoc+pon+e n sanyc+n+e nporpammy. Bo npem nrnonnenn o+kpon+e pasen
cnc+emnrx nac+poek n nsmenn+e naconon noc namen cnc+emr. Br ynnn+e, n+o n namen
nporpamme 6ye+ nrsnan me+o #+/^4'+8/:. (B neko+oprx cnc+emax on nrsrnae+c
nar; nnkaknx npo6nem .+o ne cosae+.)
Hvo ncnonsonav?
B .+on rnane 6rnn npec+annenr +pn pasnonnnoc+n o6pa+nrx nrsonon. Kak qnpma
Apple npenarae+ nr6pa+r nan6onee noxomym pasnonnnoc+r n kaon konkpe+non
cn+yannn?

O6rek+r, ko+opre enam+ n+o-+o ono (kak P%i*6/(), ncnonrsym+ mexannsm


npnemnnk/enc+nne.

O6rek+r c 6onee cnonon nny+pennen nsnrm (kak P%QNc^#++/,&*#+) ncnonrsym+


ncnomora+enrnre o6rek+r. Camrm pacnpoc+panennrm +nnom ncnomora+enrnoro
o6rek+a nne+c enera+.

O6rek+r, ko+opre onnr nepeana+r o6pa+nre nrsonr neckonrknm pyrnm


o6rek+am (+aknm, kak P%i*6/#+/), ncnonrsym+ ononemenn.
O6pavnme nmsonm n nna(enne o6+ekvaun
Hpn ncnonrsonannn o6pa+nrx nrsonon nosnnkae+ onacnoc+r +oro, n+o o6rek+r,
onammnn o6pa+nrx nrsonon, ne 6yy+ ynnn+oenr nonoennrm o6pasom. Hs-sa
.+oro 6rno pemeno, n+o:

K$"#&< '0'($@$"!4 "$ (1),$:# +('!3! ").1:,)#$1*3!. Ecnn o6rek+ nne+c


na6nma+enem, on o6rnno yane+c ns nen+pa ononemennn n ero me+oe dealloc:
U 7;#*$>$/'--#,
?
<<P%P#&*B*,'&*#+^/+&/( $/B'.-&^/+&/(= (/6#;/M2/(;/(\2/-B=H
J

I.;$/#< "$ (1),$:# +('!3! ,$1$%)#)3! ! !+#'="!/)3! ,)""<D. Ecnn nr cosae+e


o6rek+, ko+oprn nne+c enera+om nnn nc+onnnkom annrx, on onen
nonpoma+rc n cnoem me+oe dealloc:
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
161
U 7;#*$>$/'--#,
?
<L*+$#LZ4'&a#22/2/!(#.+$ 2/&O/-/8'&/\+*-=H
<&'M-/*/LZ4'&a/82`#(O'&' 2/&O'&'%#.(,/\+*-=H
J

I.;$/#"<$ (1),$:# +('!3! 0&!$3"!/)3!. Ecnn nr cosae+e o6rek+, ko+oprn


nne+c npnemnnkom. on onen o6nynn+r ykasa+enr na npnemnnk n cnoem me+oe
dealloc:
U 7;#*$>$/'--#,
?
<M.&&#+Z4'&//A2%/+$*+8//22'8/2 2/&Z'(8/&\+*-=H
J
B namen nporpamme nce .+n npo6nemr neak+yanrnr, no+omy n+o o6rek+ c#88/(
ne 6ye+ ynnn+oen o sanepmenn nporpammr. (Kpome +oro, n .+om ynpanennn
ncnonrsonan na okymen+nponannrx ncknmnenn ns npannn: P%QNc^#++/,&*#+
nnaee+ cnonm enera+om, a P%i*6/( nnaee+ cnonm npnemnnkom.)
Fnana 24. Opa1um nmson ______________________________________________________________________________________ __________
162
Cs" G9;(;*;Z%
Hpnmno npem o6cyn+r ony, o+nac+n a6c+pak+nym konnennnm. Onar k+o-+o
ckasan: To, n+o +r ec+r, n +o, n+o +r enaemr - ne ono n +o e. 3+o y+nepenne
cnpanennno n n o6rek+on: knacc o6rek+a ne ncera connaae+ c ero ponrm n
pa6o+ammen cnc+eme. Hanpnmep, o6rek+ moe+ 6r+r .ksemnnpom P%.&M-j((x,
+ora kak n npnnoennn on moe+ o6ecnennna+r ynpannenne oneperm saannn nena+n.
enc+nn+enrno xopomo nanncannrn knacc nmee+ 6onee o6mym npnpoy, ko+opa
ne orpannnnnae+c ero ponrm n onom konkpe+nom npnnoennn. 3+o nosnone+ no
pasnomy ncnonrsona+r .ksemnnpr annoro knacca.
Mr ye ronopnnn o +om, kak onpeenn+r knacc. Bosmono nn onpeenn+r ponr?
B onpeenennon c+enenn n onpeenenn ponn mono ncnonrsona+r konc+pyknnm
@protocol.
Pnc. 25.1. Hc+onnnk annrx UITableView
Hanpnmep, n npnnoennx iOS annre nac+o o+o6paam+c n .ksemnnpe knacca
QKiM-j*jL. Onako o6rek+ QKiM-j*jL ne coepn+ annre, ko+opre n nem
o+o6paam+c; on onen nonynn+r annre ns pyroro nc+onnnka. Emy neo6xonmo
coo6mn+r: Bo+ o6rek+, ko+oprn 6ye+ nrnonn+r qynknnn +noero nc+onnnka
annrx.
Kak paspa6o+nnk, cosanmnn knacc QKiM-j*jL, onpeennn ponr nc+onnnka
annrx QKiM-j*jL? On cosan npo+okon - cnncok o6rnnennn me+oon. Onn me+or
nnm+c o6sa+enrnrmn, pyrne o+noc+c k ononnn+enrnrm. Ecnn nam o6rek+
co6npae+c nrnonn+r qynknnn neko+opon ponn, on onen peannsona+r o6sa+enrnre
me+or n no enannm peannsye+ ononnn+enrnre.
Hpo+okon nc+onnnka annrx n QKiM-j*jL nasrnae+c
QKiM-j*jLO'&'%#.(,/ n nrrnn+ cneymmnm o6pasom (c monmn kommen+apnmn):
Fnana 25. Hpo1okonm ______________________________________________________________________________________
163
@@ z{gngogtr9 oo w otffr9 g|xn uftjpgqn gn p{x|w s{gngogtgq3
@@ mngn s{gngogt uftjpxjn gn s{gngogt P%M/,&
A(#&#,#- QKZ'M-/*/LO'&'%#.(,/ 1P%M/,&5
@@ ftjpxwj jngpr gvnjtug pgtyur rn {jtwgqur oypr
@@ wfnguwog puur ntwug|g s{jpfnqtjuwv
(/.*(/$
@@ ntwugj s{jpfnqtjuwj pjtwnfv u fjoww9
@@ oypv fjowv gyjn fgfngvn w ujfogtow fn{go
U 7P%K+&/8/(>&'M-/*/L\7QKZ'M-/*/L :>&; +.6M/(BN#L2K+%/,&*#+\
7P%K+&/8/(>2/,&*#+H
@@ wupjofur sxn fgfngwn w pqx jtr wfjt 7fjowv w fn{go>
U 7QKZ'M-/*/L^/-- :>&'M-/*/L\7QKZ'M-/*/L :>&;
,/--`#(N#L!&K+$/db'&4\7P%K+$/db'&4 :>*AH
@@ nw jngpr g|xn rn 7 g|xn w uj rn> {jtwgqur
@@ wfnguwog puur ntwug|g s{jpfnqtjuwv
#A&*#+'-
@@ jftw wfnguwo puur uj {jtwxjn ngn jngp9
@@ ntwugj s{jpfnqtjuwj fgfngwn ngtog w gpug fjoww
U 7P%K+&/8/(>+.6M/(B%/,&*#+2K+Z'M-/*/L\7QKZ'M-/*/L :>&;H
@@ n{gow g|xn xptvnfv w sj{jjnfv
U 7ac>&'M-/*/L\7QKZ'M-/*/L :>&; ,'+$*&N#L!&K+$/db'&4\7P%K+$/db'&4 :>*AH
U 7ac>&'M-/*/L\7QKZ'M-/*/L :>&; ,'+#;/N#L!&K+$/db'&4\7P%K+$/db'&4 :>*AH
U 7;#*$>&'M-/*/L\7QKZ'M-/*/L :>&;
,#66*&$*&*+8%&_-/\7QKZ'M-/*/L^/--$*&*+8%&_-/>/$*&*+8%&_-/
B#(N#L!&K+$/db'&4\7P%K+$/db'&4 :>*AH
U 7;#*$>&'M-/*/L\7QKZ'M-/*/L :>&;
6#;/N#L!&K+$/db'&4\7P%K+$/db'&4 :>2#.(,/K+$/db'&4
&#K+$/db'&4\7P%K+$/db'&4 :>$/2&*+'&*#+K+$/db'&4H
@@ tv ogugww jfn v gsxfo gvqtjuwv
@@ ujfogtow pgsgtuwnjtur jngpgq3
/+$
Hpo+okonr, kak n knaccr, +oe nmem+ cnon cnpanonnre c+pannnr n
okymen+annn paspa6o+nnka Apple, c noepkon noncka n nrnoa cnncka me+oon
npo+okona.
Kora nr cosae+e knacc, ko+oprn onen ncnonrsona+rc n kanec+ne nc+onnnka
annrx QKZ'M-/*/L, nr onnr nno ykasa+r n saronononnom qanne: 3+o+ knacc
noepnnae+ npo+okon QKZ'M-/*/LO&%#.(fj. 3+o nrrnn+ npnmepno +ak:
*+&/(B',/ Z/((*B*,*/L^#+&(#--/( \ QK*/L^#+&(#--/( 1QKZ'M-/*/LO'&'%#.(,/5
333
/+$
Hnane ronop, Z/((*B*,*/L^#+&(#--/( - cy6knacc QK*/L^#+&(#--/(,
ko+oprn noepnnae+ npo+okon QKiM-j*jLO&%g.(fj.
Fnana 25. Hpo1okonm ______________________________________________________________________________________
164
Ecnn knacc noepnnae+ neckonrko npo+okonon, onn nepenncnm+c n yrnonrx
cko6kax:
*+&/(B',/ Z/((*B*,*/L^#+&(#--/( \ QK*/L^#+&(#--/(
1QKZ'M-/*/LO'&'%#.(,/9 QKZ'M-/*/LO/-/8'&/9 QKZ/d&`*/-$O/-/8'&/5
dann TerricController.m onen coepa+r peannsannn o6sa+enrnrx me+oon n
kaom npo+okone. Ecnn nr sa6ye+e peannsona+r onn ns o6sa+enrnrx me+oon,
komnnn+op nrac+ npeynpeenne.
Take npocmo+pn+e cnncok ononnn+enrnrx me+oon n nr6epn+e +e ns nnx,
ko+opre nam xo+enocr 6r peannsona+r. Peannsonannre me+or 6yy+ an+oma+nneckn
nrsnanr n nynrn momen+ npemenn.
H nocnenee samenanne: n nporpamme ^'--M',h2 ns rnanr 24 .ksemnnp c#88/(
c+anonn+c enera+om o6rek+a P%QNc^#++/,&*#+. Onako n qanne Logger.h ne ykasano,
n+o c#88/( noepnnae+ coo+ne+c+nymmnn npo+okon.
Ha momen+ nanncann knnrn qopmanrnoro npo+okona n enera+on
P%QNc^#++/,&*#+ eme ne cymec+nye+. B ne ynnnmcr, ecnn .+o nponsone+. (A ecnn
npn noc+poennn nporpammr ^'--M',h2 nr nonynnnn npeynpeenne nna annrn
o6rek+ ne noepnnae+ npo+okon P%QNc^#++/,&*#+O/-/8'&/, snann+, ye
nponsomno.)
Fnana 25. Hpo1okonm ______________________________________________________________________________________
165
C" -@&B*& B>;fB(>
Hnora +pe6ye+c cosa+r qann n qopma+e, yo6nom kak n komnrm+epon, +ak n n
nmen. onyc+nm, nr xo+n+e coxpann+r n qanne onncanne cnoero nop+qen aknnn.
o6annenne nonrx aknnn 6rno 6r yo6no ocymec+nn+r npynnym, no +ake 6rno 6r
xopomo, n+o6r coepnmoe qanna nopmanrno nn+anocr namnmn nporpammamn. n
pemenn noo6nrx npo6nem mnorne nporpammnc+r Objective-C ncnonrsym+ cnnckn
cnonc+n.
Cnncok cnonc+n npec+anne+ co6on kom6nnannm cneymmnx .nemen+on:
P%!(('_
P%*,&*#+'(_
P%%&(*+8
P%'&'
P%'&/
P%P.6M/( (nenoe, nemec+nennoe nnn nornneckoe snanenne)
Hanpnmep, maccnn cnonapen co c+pokonrmn knmnamn n o6rek+amn a+r
npec+anne+ co6on cnncok cnonc+n.
Onepannn n+enn n sanncn cnncka cnonc+n n qann onenr npoc+r. B Xcode
cosan+e nonrn npoek+: nporpammy komannon c+pokn `#.+$'&*#+ c nmenem 2&#,h.
Bknmnn+e n nee cneymmnn ko:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%.&'M-/!(('_ :2&#,h2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
P%.&'M-/O*,&*#+'(_ :2&#,hH
2&#,h k <P%.&'M-/O*,&*#+'(_ $*,&*#+'(_=H
<2&#,h 2/&M/,&\C!!bcC
B#(/_\C2_6M#-C=H
<2&#,h 2/&M/,&\<P%P.6M/( +.6M/(E*&4K+&\VII=
B#(/_\C24'(/2C=H
<2&#,h2 '$$M/,&\2&#,h=H
2&#,h k <P%.&'M-/O*,&*#+'(_ $*,&*#+'(_=H
<2&#,h 2/&M/,&\C""C
B#(/_\C2_6M#-C=H
<2&#,h 2/&M/,&\<P%P.6M/( +.6M/(E*&4K+&\XRI=
B#(/_\C24'(/2C=H
<2&#,h2 '$$M/,&\2&#,h=H
Fnana 26. Cnucku conc1n ______________________________________________________________________________________
166
<2&#,h2 L(*&/Z#`*-/\C@&6A@2&#,h23A-*2&C
'&#6*,'--_\%=H
J
(/&.(+ IH
J
(O6pa+n+e nnnmanne na non+opnoe ncnonrsonanne ykasa+en 2&#,h. Cnanana on
ykasrnae+ na nepnrn cnonapr, a no+om na n+opon.)

Pnc. 26.1. Maccnn cnonapen
Banymenna nporpamma cosae+ qann stocks.plist. O+kprn ero n +ekc+onom
peak+ope, nr ynnn+e npnmepno cneymmee:
1~d6- ;/(2*#+kCX3IC /+,#$*+8kCQZ`UWC~5
1FO^Zb A-*2& bQacK^
CU@@!AA-/@@OZO bcK%Z X3I@@PC C4&&A\@@LLL3'AA-/3,#6@OZO2@
b(#A/(&_c*2&UX3I3$&$C5
1A-*2& ;/(2*#+kCX3IC5
1'(('_5
1$*,&5
1h/_524'(/21@h/_5
1*+&/8/(5VII1@*+&/8/(5
1h/_52_6M#-1@h/_5
12&(*+85!!bc1@2&(*+85
1@$*,&5
1$*,&5
1h/_524'(/21@h/_5
1*+&/8/(5XRI1@*+&/8/(5
1h/_52_6M#-1@h/_5
12&(*+85""1@2&(*+85
1@$*,&5
1@'(('_5
1@A-*2&5
Hennoxo, nepno? XML. Xopomo nn+ae+c. H ncero ona c+poka koa.
Fnana 26. Cnucku conc1n ______________________________________________________________________________________
167
Ecnn nam npne+c cosana+r cnnckn cnonc+n npynnym, yn+n+e, n+o n Xcode n
pa6o+r co cnnckamn cnonc+n nmee+c cnennanrnrn nc+poennrn peak+op.
Tenepr o6anr+e c+poky n n+enn qanna:
*+& 6'*+7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
P%.&'M-/!(('_ :2&#,h2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
P%.&'M-/O*,&*#+'(_ :2&#,hH
2&#,h k <P%.&'M-/O*,&*#+'(_ $*,&*#+'(_=H
<2&#,h 2/&M/,&\C!!bcC
B#(/_\C2_6M#-C=H
<2&#,h 2/&M/,&\<P%P.6M/( +.6M/(E*&4K+&\VII=
B#(/_\C24'(/2C=H
<2&#,h2 '$$M/,&\2&#,h=H
2&#,h k <P%.&'M-/O*,&*#+'(_ $*,&*#+'(_=H
<2&#,h 2/&M/,&\C""C
B#(/_\C2_6M#-C=H
<2&#,h 2/&M/,&\<P%P.6M/( +.6M/(E*&4K+&\XRI=
B#(/_\C24'(/2C=H
<2&#,h2 '$$M/,&\2&#,h=H
<2&#,h2 L(*&/Z#`*-/\C@&6A@2&#,h23A-*2&C
'&#6*,'--_\%=H
xIIPq XQ3S2p4Q3 j xIIPq PIIPq43R7SJ31J3Q.K4i1MF3THF
Q3S2pQ"Hi4Q3MW
KSI Lxy4234SJPIq Xw 4J Q3S2p4Q3V z
xShLMN RP51 r QRPI1Q SK rM`
w S/0123SI1qMQRPI1QM` w S/0123SI1qMQqT/SiMVW
|
|
I13aIJ bW
|
Hoc+pon+e n sanyc+n+e nporpammy
npawnenne
Hannmn+e nporpammy n cosann cnncka cnonc+n, coepamero nce 8 +nnon: maccnn,
cnonapr, c+poka, annre, a+a, nenoe nncno, nemec+nennoe nncno, nornneckoe snanenne.
Fnana 26. Cnucku conc1n ______________________________________________________________________________________
168
IV
Co6mvnnnoe
nporpauunponanne
Mr noomnn k +omy, pan nero nr nanann nn+a+r .+y knnry - k nanncannm npnnoennn
iOS n Cocoa. B cneymmnx nyx rnanax nr nonynn+e npec+annenne o paspa6o+ke
npnnoennn. Bamn npnnoenn 6yy+ nme+r rpaqnnecknn nn+epqenc (GUI, Grapllical
User Interface), n onn 6yy+ ynpann+rc co6r+nmn.
Kak pa6o+am+ npnnoenn komannon c+pokn? Br sanyckae+e nporpammy, a ona
nannnae+ ena+r cnoe eno, noka ne sakonnn+. C npnnoennmn, ynpannemrmn
co6r+nmn, eno o6c+on+ nnane. Hpnnoenne sanyckae+ nnkn co6r+nn, ko+oprn
onae+ noc+ynnenn co6r+nn. Kora co6r+ne nponcxon+, npnnoenne o6pa6a+rnae+
ero: nrnonne+ me+or, o+npanne+ coo6menn n +. .
Cnanana mr nannmem npnnoenne iOS, a sa+em cosanm noxoee npnnoenne
Cocoa. Cocoa - na6op 6n6nno+ek, paspa6o+annrx Apple n nporpammnponann n Mac.
Br ye snakomr c onon ns .+nx 6n6nno+ek - Foundation. Cocoa n Cocoa Touch
nknmnam+ neko+opre o6mne 6n6nno+ekn - +akne, kak Foundation. pyrne 6n6nno+ekn
npenasnanenr n onon konkpe+non nna+qopmr.
169
C" G<9>;< @9&Z;=<+&<
4.x
B .+on rnane mr cosanm cnoe nepnoe npnnoenne iOS: npoc+on cnncok +ekymnx saan
*Z'4O##$-/, ko+oprn 6ye+ xpann+r cnon annre n cnncke cnonc+n. Bo+ kak 6ye+
nrrne+r *Z'4O##$-/ nocne sanepmenn pa6o+r.
Pnc. 27.1. HpnnoenneiTahDoodle
Bce npnnoenn iOS o+noc+c k ka+eropnn npnnoennn, ynpannemrx
co6r+nmn. B nnx nnkn co6r+nn onae+, noka n+o-nn6yr nponsone+. anee
onammee npnnoenne pearnpye+ na co6r+n, crenepnponannre nonrsona+enem
(+akne, kak naa+ne knonkn) nnn cnc+emon (nanpnmep, npeynpeenne o nexna+ke
nam+n).
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
170
Haunnaeu pa6ovy na( iTahDoodle
B Xcode nrnonnn+e komany `*-/P/LP/L b(#/,&333. B pasene iOS (ne n
pasene Mac OS X!) nr6epn+e c+poky Application. B o+kprnmemc na6ope ma6nonon
nr6epn+e ma6non nyc+oro npnnoenn Empty Application.
Pnc. 27.2. Cosanne nonoro npnnoenn iOS
Ea6nonr npoek+on Xcode npnsnanr ynpoc+n+r namy pa6o+y. Onn coepa+
saro+onkn koa, ko+opre mory+ yckopn+r paspa6o+ky. Onako mr namepenno nr6pann
ma6non nyc+oro npnnoenn. Ecnn Xcode crenepnpye+ cnnmkom mnoro ro+onoro koa,
.+o +onrko nomemae+ nam non+r, kak pa6o+ae+ nporpamma.
Hmena ma6nonon nac+o nsmenm+c n nonrx nepcnx Xcode; ne ynnnn+ecr,
ecnn n namen nepcnn ma6nonr ne 6yy+ nonnoc+rm connaa+r c +em, n+o nr nnn+e na
pnc. 27.2. Hann+e ma6non c camrm npoc+rm nasnannem n o+peak+npyn+e ko, npnne
ero n coo+ne+c+nne c koom knnrn. Ecnn y nac nosnnkny+ +pynoc+n c cornaconannem
koa nnn ma6nonon npoek+on, o6paman+ecr sa nomomrm na qopym Big Nerd Ranch no
apecy forums.bignerdranch.com.
Hocne nr6opa ma6nona nyc+oro npnnoenn menknn+e na knonke Next n
npncnon+e npoek+y nm *i'4O##$-/. Hon Company Identier n Bundle Identier
o6ecnennnam+ ynnkanrnoc+r npnnoennn n App Store. O6a snanenn npec+annm+
co6on c+pokn n o6pa+non omennon sanncn. Tak, Big Nerd Ranch nnon+ n none Company
Identier c+poky com.bignerdranch.
Coepnmoe non Class Prex 6ye+ nc+anneno nepe nmenem ncxonoro knacca,
crenepnponannoro ma6nonom sa nac. 3+o+ npeqnkc (coc+omnn ns nyx-+pex 6ykn)
nyen n +oro, n+o6r nmena namnx knaccon o+nnnanncr o+ nmen knaccon Apple nnn
pyrnx paspa6o+nnkon. (Bnen+e npeqnkc BNR, n+o6r nam ko ne o+nnnanc o+
npnneennoro n knnre.)
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
171
Pnc. 27.3. Hac+ponka napame+pon npoek+a iTahDoodle
O6pa+n+e nnnmanne na ncnonrsonanne npeqnkcon n koe Apple. Bce knaccr
Apple, nc+penanmnec nam o cnx nop, nannnanncr c npeqnkca NS-cokpamenn o+
NeXTSTEP (nna+qopma, n ko+opon nsnananrno npoek+nponanacr 6n6nno+eka
Foundation). B .+on rnane mr +ake 6yem ncnonrsona+r knaccr ns 6n6nno+ekn QK*&,
nmena ko+oprx 6yy+ nannna+rc c npeqnkca UI.
Hanocneok ykan+e, n+o npnnoenne *Z'4O##$-/ nne+c npnnoennem n
iPhone (a ne npnnoennem n iPad nnn ynnnepcanrnrm npnnoennem). B *Z'4O##$-/
6ye+ ncnonrsona+rc an+oma+nnecknn nocne+ ccrnok, no noepka Core Data n
moynrnre +ec+r ne nonao6+c.
BNRAppDelegate
Cosana nam npoek+ no ma6nony Empty Application, Xcode cosae+ sa nac ncero onn
knacc: aPN!AAO/-/8'&/. Knacc enera+a npnnoenn nne+c o+npannon +onkon n
cosann npnnoenn; +akon knacc npncy+c+nye+ n kaom npnnoennn n iOS.
aPN!AAO/-/8'&/ cymec+nye+ n onom .ksemnnpe, ko+oprn o+nenae+ sa o6pa6o+ky
co6r+nn n koopnnannm pa6o+r pyrnx o6rek+on n npnnoennn.
O+kpon+e qann BNRAppDelegate.h. o6anr+e ne+rpe nepemennrx n me+o
.ksemnnpa. Hepnre +pn nepemennre .ksemnnpa npec+annm+ co6on ykasa+enn na
o6rek+r, ko+opre nonrsona+enr nnn+ n c ko+oprmn moe+ nsanmoenc+nona+r, -
+a6nnnnoe npec+annenne n o+o6paenn cnncka saan, +ekc+onoe none n nnoa
nonon saann n knonka n nknmnenn nneennon saann n +a6nnny. He+nep+rn o6rek+ -
nsmenemrn maccnn. B nem saann 6yy+ xpann+rc n nne c+pok.
)*6A#(& 1QK*&@QK*&345
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
172
*+&/(B',/ aPN!AAO/-/8'&/ \ QKN/2A#+$/( 1QK!AA-*,'&*#+O/-/8'&/5
z
NoP/i141O X3PQpoP/i1W
No1341iw X3PQp41iwW
N~a33SJ X4JQ1I3~a33SJW
x}a3P/i1IIPq X3PQpQW
|
6 L5S4wVPwwoPQpL4wVQ1Jw1IW
A(#A/(&_ 72&(#+89 +#+'&#6*,> QKE*+$#L :L*+$#LH
/+$
O6pa+n+e nnnmanne: ma6non nmnop+npye+ qann UILit.h. Bn6nno+eka UIKit
coepn+ 6onrmnnc+no knaccon n iOS, nknmna QKiM-j*jL, QKi/d&`*/-$ n
QKa.&&#+. Kpome +oro, aPN!AAO/-/8'&/ noepnnae+ npo+okon
QK!AA-*,'&*#+O/-/8'&/.
Bame+r+e, n+o mr ne nmnop+npyem Foundation.h. Kak +ora ncnonrsona+r
P%.&M-j||x? B .+om ma6none sarononok 6n6nno+ekn Foundation nxon+ n
npenapn+enrno o+komnnnnponannrn qann sarononka npoek+a, +ak n+o knaccr
Foundation an+oma+nneckn oc+ynnr n ncnonrsonann. (He nepn+e? Eenknn+e na
qanne iTahDoodle-Pr$jix.&+h n pasene Supporting Files nannra+opa npoek+a n y6en+ecr
camn.)
o6annenne ncnouoravennon qynknnn C
Ho o6rnnennm nepemennrx .ksemnnpon mono non+r, n+o npnnoenne *i'4Og#$-/
6ye+ coepa+r mnnnmym ne+rpe ononnn+enrnrx o6rek+a. Ha npee nem 6pa+rc sa
.+n o6rek+r, mr nannmem qynknnm C. B Objective-C n nrnonnenn ocnonnon pa6o+r
o6rnno ncnonrsym+c me+or nmec+o qynknnn, no.+omy qynknnn C n npnnoennx
Objective-C nac+a nasrnam+c ncnomora+enrnrmn qynknnmn.
Hpnnoenne *Z'4O##$-/ xpann+ saann nonrsona+en n cnncke cnonc+n - +o ec+r
n qanne XML. 3+o osnanae+, n+o nam nonao6n+c kak-+a onpeenn+r mec+onaxoenne
qanna no npem nrnonnenn npnnoenn. Mr nannmem qynknnm C, ko+opa
nosnpamae+ ny+r k qanny n qopma+e P%%&(*+8.
H+o6r o6ann+r ncnomora+enrnym qynknnm n npnnoenne, neo6xonmo cnanana
o6rnn+r ee n qanne BNRAppDelegate.h.
)*6A#(& 1QK*&@QK*&345
@@ gvqtjuwj qfsgg|njtug xuoww ptv sgtxjuwv sxnw
@@ o ontg|x u pwfoj
9
ogng{r xpjn wfsgtgqnfv
@@ ptv fg{ujuwv fswfo p
xx3I4Jh XwS2{P3RL5S4wVW
*+&/(B',/ aPN!AAO/-/8'&/ \ QKN/2A#+$/(
1QK!AA-*,'&*#+O/-/8'&/5
?
QKZ'M-/*/L :&'2hZ'M-/H
QKZ/d&`*/-$ :&'2h`*/-$H
QKa.&&#+ :*+2/(&a.&&#+H
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
173
P%.&'M-/!(('_ :&'2h2H
J
U 7;#*$>'$$Z'2h\7*$>2/+$/(H
A(#A/(&_ 72&(#+89 +#+'&#6*,> QKE*+$#L :L*+$#LH
/+$
O6pa+n+e nnnmanne: o6rnnenne $#,b'&47> naxon+c a o6rnnenn knacca.
3+a cnsana c +em, n+o xo+ qynknn $#,b'&47> o6rnne+c n qanne
BNRAppDelegate.h, ona ne nne+c ero nac+rm. Boo6me ronop, n nee mono 6rno
6r cosa+r napy o+enrnrx qannon n npoek+e *Z'4O##$-/. Ha nockonrky *Z'4O##$-/
coepn+ ncero ony ncnomora+enrnym qynknnm, mr n npoc+a+r pasmemaem ee n
qannax knacca enera+a npnnoenn.
Tenepr o+kpon+e BNRAppDelegate.m n peannsyn+e ncnomora+enrnym qynknnm.
H cnona, +ak kak $#,b'&47> ne nne+c nac+rm knacca, peannsann onna
pacnonara+rc nocne )*6A#(&, no o c+pokn *6{-j6j+&&*g+ (n ko+opon nannnae+c
peannsann knacca).
)*6A#(& CaPN!AAO/-/8'&/34C
@@ fsgg|njtuv xuowv ptv sgtxjuwv sxnw o fswfox p
@@ {uvjxfv u pwfoj
P%%&(*+8 :$#,b'&47>
z
xIIPq XHP3R4Q3 j
xx1PI2R{P3RSIy4I123SI41QNJySTP4JQLxyS2aT1J3y4I123SIq`
xQ1IySTP4J}PQp`
xVW
I13aIJ HP3R4Q3 S/01233NJw1b
Q3I4Jh~qHH1Jw4Jh{P3R7STHSJ1J3MwP3P"3wMW
|
*6A-/6/+&'&*#+
dynknn $#,b'&47> nrsrnae+ pyrym qynknnm C, P%%/'(,4b'&4`#(
O*(/,&#(*/2K+O#6'*+27>. 3+a qynknn nme+ ka+anorn, yonne+nopmmne saannomy
kpn+epnm, n nosnpamae+ nx n maccnne. Hoka ne o6paman+e nnnmann na aprymen+r:
non+n no ncex npnnoennx iOS, ko+opre nr kora-nn6o nannme+e, nr 6ye+e
nepeana+r .+on qynknnn .+n +pn aprymen+a n nonyna+r maccnn c ponno onnm
.nemen+om . ( Ecnn nac nn+epecye+ , kak pa6o+ae+ qynknn
P%%/'(,4b'&4`#(O*(/,&#(*/2K+O#6'*+27>, nann+e ee onncanne n cnpanonnnke
Foundation Functions Reference n okymen+annn paspa6o+nnka.)
O6+ekvm n npnnowennn iTahDoodle
Bepnemc k namnm o6rek+am. Bam ye nsnec+nr n+r o6rek+on, o6pasymmnx
npnnoenne *Z'4O##$-/: npee ncero .ksemnnp aPN!AAO/-/8'&/, a n nem xpan+c
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
174
ykasa+enn na ne+rpe pyrnx o6rek+a: .ksemnnpr QKiM-j*jL, QKi/d&`*/-$,
QKa.&&#+ n P%.&M-j((x.
Ho npee nem mr nepenem k nac+ponke n cnsrnannm .+nx o6rek+on - nemnoro
+eopnn o o6rek+ax n o+nomennx mey nnmn.
Pnc.27.4, narpamma o6rek+on iTahDoodle
Mo(en-Hpe(cvannenne-Konvponnep
B na++epne npoek+nponann Moenr-Hpec+annenne-Kon+ponnep, nnn cokpamenno
MVC (Model-View-Controller), karn cosannrn namn knacc onen o+nocn+rc k
onon ns +pex qynknnonanrnrx ka+eropnn: moenr, npec+annenne nnn kon+ponnep.
Kpa+ka cnoka pasenenn o6sannoc+en mey ka+eropnmn:

J',$1! o+nenam+ sa xpanenne annrx n npeoc+annenne nx pyrnm o6rek+am. Moenn


nnnero ne snam+ o nonrsona+enrckom nn+epqence n ne ymem+ o+o6paa+r ce6 na
.kpane; nx ennc+nennoe npenasnanenne - xpanenne n ynpannenne annrmn. P%%&(*+8,
P%O'&/ n P%!(('_ - +pannnonnre o6rek+r moenen. B *Z'4O##$-/ noka
ncnonrsye+c +onrko onn o6rek+ moenn: .ksemnnp P%.&M-j((x, n ko+opom
xpan+c saann. Onako kaa o+- enrna saana 6ye+ onncrna+rc .ksemnnpom
P%%&(*+8, n on +oe 6ye+ o6rek+om moenn.

E&$,+#)(1$"!* nnm+c nnsyanrnrmn .nemen+amn npnnoennn. Onn snam+, kak


o+o6pasn+r ce6 na .kpane n kak pearnpona+r na nno annrx nonrsona+enem.
Hpec+annenn ne pacnonaram+ nnqopmannen o annrx, ko+opre nmn o+o6paam+c,
o6 nx c+pyk+ype n cnoco6e xpanenn. Tnnnnnre npnmepr o6rek+on npec+annennn -
QK*/L n ero pasnnnnre cy6knaccr, nknmna QKE*+$gL. B npnnoennn *Z'4O##$-/
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
175
o6rek+r npec+annennn nnm+c .ksemnnpamn QKiM-j*jL, QKi/d&*/L n
QK.&&g+. Hpoc+oe n npak+nnnoe npannno: ecnn nr nnn+e kakon-+o .nemen+
npnnoenn na .kpane, snann+, .+o npec+annenne.

L'"#&'11$&< nrnonnm+ nornky, neo6xonmym n cnsrnann n ynpannenn


pasnnnnrmn nac+mn namero npnnoenn. Onn o6pa6a+rnam+ co6r+n n
koopnnnpym+ pa6o+y pyrnx o6rek+on namero npnnoenn. Kon+ponnepr -
nac+oma pa6ona cnna nm6oro npnnoenn. Xo+ n *Z'4O##$-/ ncnonrsye+c
+onrko onn kon+ponnep aPN!AAO/-/8'&/, n cnonom npnnoennn nnonne moe+
6r+r mnoro pasnrx kon+ponnepon, koopnnnpymmnx pa6o+y o6rek+on moenen n
npec+annennn, a +ake pyrnx kon+ponnepon.
Ha pnc.27.5 npec+annena cxema nepeann ynpannenn mey o6rek+amn npn
noc+ynnennn nonrsona+enrckoro co6r+n (nanpnmep, naa+n knonkn). O6pa+n+e
nnnmanne: moenn n npec+annenn ne nsanmoenc+nym+ pyr c pyrom nanpmym; n
cepenne nm6oro nsanmoenc+nn naxon+c kon+ponnep, nonynammnn coo6menn o+
onnx o6rek+on n nepeammnn nnc+pyknnn pyrnm.
Pnc. 27.5. O6pa6o+ka nonrsona+enrckoro
Onenr nano ne+ko nonnma+r o6sannoc+n kaoro ns ynac+nnkon na++epna ^.
Bonrma nac+r nporpammnrx nn+epqencon Cocoa n Cocoa Touch opnen+nponana na ^
- n nam ko +oe onen opnen+npona+rc na .+o+ na++epn.
A +enepr nepnemc k kon+ponnepy, ko+oprn nne+c .ksemnnpom
aPN!AAO/-/8'&/.
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
176
Honusoea1enucku
eeop
Hpepc1aeneuun
Kou1ponnep Mopenu
Honusoea1enu esauVopec1eye1
c oek1aVu npepc1aeneuun
Hpepc1aeneuue
o1npaenne1
cooueuue
kou1ponnepy
Kou1ponnep
ouoenne1 oek1u
Vopene
Kou1ponnep
ouoenne1
npepc1aeneuue
usVeueuunVu
e oek1ax Vopenu
Kou1ponnep
nonyuae1 pauuue
o1 oek1oe
Vopene, uyxuue
ero npepc1aeneuunV
enerav npnnowennn
Hpn sanycke npnnoenn iOS nponsnon+c nnnnnannsann, nesame+na n
nonrsona+en. B .+on qase cosae+c .ksemnnp QK!AA-*,'&*#+, ko+oprn ynpanne+
coc+onnem npnnoenn n o6ecnennnae+ cnsr c onepannonnon cnc+emon. Take
cosae+c .ksemnnp aPN!AAO/-/8'&/, nasnanaemrn enera+om .ksemnnpa
QK!AA-*,'&*#+ (o+cma n +epmnn enera+ npnnoenn ).
B nponecce sanycka npnnoenne eme ne ro+ono k nrnonnennm pa6o+r nnn nnoy
annrx. Kora .+a cn+yann nsmene+c, .ksemnnp QK!AA-*,'&*#+ o+npanne+ cnoemy
enera+y coo6menne 'AA-*,'&*#+\$*$`*+*24c'.+,4*+8E*&4A&*#+2:. 3+o+ me+o
onenr naen. Hmenno n nem nrnonnm+c nce enc+nn, ko+opre onnr 6r+r
nrnonnenr o +oro, kak nonrsona+enr nanne+ nsanmoenc+nne c npnnoennem.
B npnnoennn *i'4O##$-/ onon ns onepannn, nrnonnemrx n .+om me+oe,
onen 6r+r nonck cnncka cnonc+n n sarpyska ero n maccnn. B qanne
BNRAppDelegate.m ye nmee+c ro+ona sarnymka n me+oa 'AA-*,'&*#+:
$*$`*+*24c'.+,4*+8E*&4A&*#+2:. Hann+e ee n samenn+e ko n qnrypnrx cko6kax
cneymmnm:
U 7ac>'AA-*,'&*#+\7QK!AA-*,'&*#+ :>'AA-*,'&*#+
$*$`*+*24c'.+,4*+8E*&4A&*#+2\7P%O*,&*#+'(_ :>-'.+,4A&*#+2
?
@@ sgsrno |{xow fxjfnqxj|g fswfo p
@@ w ffwq9 {uvj|gfv u pwfoj3
xIIPq XHi4Q3 j xIIPq PIIPq43R7SJ31J3Q.K4i1wS2{P3RLVW
4K LHi4Q3V z
@@ jftw ug{ puur fxjfnqxjn9 gu ogsw{xjnfv q sj{jjuux ojstv{3
3PQpQ j Hi4Q3 Ta3P/i17SHqW
| 1iQ1 z
@@ q s{gnwqug ftxj s{gfng fgpj sxfng wfgpur ug{3
3PQpQ j x}a3P/i1IIPq PiiS2 4J43W
|
J
Bame+nnn )A('86' 6'(h n nanane koa? Hporpammnc+r Objective-C nac+o
ncnonrsym+ .+y konc+pyknnm n rpynnnponkn me+oon n knaccax. Xcode +oe snae+ o
ee cymec+nonannn. Ha nanenn nannrannn n nepxnen nac+n peak+opa nann+e .nemen+
cnpana o+ BNRAppDelegate.m. (Bosmono, on coepn+ +ekc+ *6A-/6/+&'&*#+
!AAO/-/8'&/, no coepnmoe sanncn+ o+ +ekymero mec+onaxoenn kypcopa n koe.)
Eenknn+e na .nemen+e; Xcode nrnee+ cnncok nosnnnn n .+om qanne. Ecnn menkny+r
na nm6on ns npenoennrx nosnnnn, nr nepene+e npmo k coo+ne+c+nymmemy mec+y
koa. O6pa+n+e nnnmanne na npncy+c+nne n cnncke npek+nnr A('86' 6'(h. 3+o+
cnoco6 nepememenn npesnrnanno yo6en, kora knacc coepn+ mnoro me+oon.
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
177
Ho(rovonka npe(cvannennn
H+o6r npnnoenne 6rno ro+ono k pa6o+e, mr onnr noro+onn+r o6rek+r
npec+annennn: cosa+r nx, nac+pon+r n o+o6pasn+r na .kpane. hornnno, ne npana nn?
Honrsona+enr ne cmoe+ naa+r knonky, ko+opa ne cymec+nye+ nnn ne o+o6paae+c
na .kpane.
B *Z'4O##$-/ mr cosanm npec+annenn na nporpammnom yponne n
'AA-*,'&*#+: $*$`*+*24c'.+,4*+8E*&4A&*#+2:. Take cymec+nym+ nnsyanrnre
cpec+na cosann npec+annennn, ko+oprmn mr nocnonrsyemc n cneymmen rnane.
B onen npeynpen+r, n+o anrme ko c+anonn+c oc+a+onno cepresnrm.
Hopo6nrn cnn+akcnc cosann n o+o6paenn npec+annennn na .kpane - +ema n
o+enrnon knnrn, nocnmennon nporpammnponannm npnnoennn iOS. Honpo6yn+e
ynonn+r cy+r nponcxomero. Mr cosaem karn o6rek+, a sa+em nac+pannaem ero,
saana snanenn neko+oprx cnonc+n. Ba+em nac+poennre o6rek+r nknmnam+c kak
+6.0&$,c#)(1$"!* n o6rek+ okna, n nakonen, okno pasmemae+c na .kpane.
HIPhTP TPIp 6 HHi42P34SJ w1i1hP31 2Pii/P2pQ
U 7ac>'AA-*,'&*#+\7QK!AA-*,'&*#+ :>'AA-*,'&*#+
$*$`*+*24c'.+,4*+8E*&4A&*#+2\7P%O*,&*#+'(_ :>-'.+,4A&*#+2
?
FF @;@%(*) _),9?_*& B?<B(>?<,; B@&B*) _)l)\
FF &_ A)BB&>)` [9)+E<,;BE +) l&B*<"
P%!(('_ :A-*2& k <P%!(('_ '(('_E*&4^#+&/+&2B`*-/\$#,b'&47>=H
*B 7A-*2&> ?
FF <BZ& +):;9 l)++%[ B?<B(>?<(` ;+ *;@&9?<(BE > @<9<A<++? '*_<A@ZE9)"
&'2h2 k <A-*2& 6.&'M-/^#A_=H
J /-2/ ?
FF > @9;(&>+;A BZ?\)< @9;B(; B;_l)<A @?B(;f &B[;l+%f +):;9"
&'2h2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
J
FF B;_l)+&< & +)B(9;f*) '*_<A@ZE9) N4JwSO"
FF B(9?*(?9) 7123 @9<lB()>ZE<( @9EA;?,;Z]+&* B :)_;>;f (;\*;f
FF L[`?V & 9)_A<9)A& LO4w3R`R14hR3V
7123 O4JwSOIPT1 j Nx2I11J TP4Jx2I11J /SaJwQW
N4JwSO X3R14JwSO j N4JwSO PiiS2 4J4343RIPT1O4JwSOIPT1W
Q1iK Q134JwSO3R14JwSOW
FF ;@9<l<Z<+&< ,9)+&\+%[ @9EA;?,;Z]+&*;> lZE (9<[ 'Z<A<+(;>
FF @;Z]_;>)(<Z]B*;,; &+(<9m<fB)"
FF 7123}Pp1LV B;_l)<( '*_<A@ZE9 7123 @; l)++%A L[` ?` O4w3R` R14hR3V
7123 3P/i1IPT1 j 7123}Pp1Lb` b` cCb` cbVW
7123 K41iwIPT1 j 7123}Pp1LCb` kb` Cbb` c!VW
7123 /a33SJIPT1 j 7123}Pp1LCC` kb` C` c!VW
FF B;_l)+&< & +)B(9;f*) ():Z&\+;,; @9<lB()>Z<+&E
3PQpoP/i1 j NoP/i141O PiiS2 4J4343RIPT13P/i1IPT1
Q3qi1NoP/i141Ox3qi1{iP4JW
3PQpoP/i1 Q13x1HPIP3SIx3qi1NoP/i141O71iix1HPIP3SIx3qi1SJ1W
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
178
FF B;_l)+&< & +)B(9;f*) (<*B(;>;,; @;ZE lZE >>;l) +;>%[ _)l)\
3PQp41iw j No1341iw PiiS2 4J4343RIPT1K41iwIPT1W
3PQp41iw Q13~SIw1Ix3qi1No13~SIw1Ix3qi1SaJw1w123W
3PQp41iw Q13{iP21RSiw1IMoqH1 P 3PQp` 3PH NJQ1I3MW
FF B;_l)+&< & +)B(9;f*) *+;@*& NJQ1I3 > >&l< @9EA;?,;Z]+&*)
FF B _)*9?,Z<++%A& ?,Z)A&"
4JQ1I3~a33SJ j N~a33SJ /a33SJ43RoqH1N~a33SJoqH1SaJw1w123W
4JQ1I3~a33SJ Q13IPT1/a33SJIPT1W
FF 9):;() *+;@;* ;B+;>)+) +) A<[)+&_A< ;:9)(+;,; >%_;>) (&@)
FF M@9&<A+&*Fl<fB(>&<M" <fB(>&< *+;@*& NJQ1I3 +)B(9)&>)<(BE
FF +) >%_;> A<(;l) 6)wwY)Qp (<*?<,; ;:e<*()
4JQ1I3~a33SJ PwwoPIh13Q1iK
P234SJQ1i123SILPwwoPQpV
KSI7SJ3ISi51J3QN7SJ3ISi51J3oSa2RHNJQ4w1W
@@ s{jpjtjuwj upswfw u ougsoj
4JQ1I3~a33SJ Q13o43i1MNJQ1I3M
KSIx3P31N7SJ3ISix3P31SITPiW
FF >*Z\<+&< (9<[ 'Z<A<+(;> @;Z]_;>)(<Z]B*;,; &+(<9m<fB) > ;*+;
Q1iK O4JwSO Pwwxa/541O3PQpoP/i1W
Q1iK O4JwSO Pwwxa/541O3PQp41iwW
Q1iK O4JwSO Pwwxa/541O4JQ1I3~a33SJW
FF )><9D<+&< +)B(9;f*& ;*+) & ;(;:9)=<+&< <,; +) '*9)+<
Q1iK O4JwSO Q13~P2phISaJw7SiSIN7SiSI OR4317SiSIW
Q1iK O4JwSO TPp11qJw4Q4/i1W
I13aIJ xW
J
Bmnonnenne n iOS Simulator
H+ak, npec+annenn cosanr; +enepr mono noc+pon+r npnnoenne n nocmo+pe+r, kak
onn nrrn+. B Xcode nann+e packprnammnnc cnncok %,4/6/ pom c knonkon N.+.
Br6epn+e n nem c+poky iPhone 5.X Simulator n sanycka nocnenen nepcnn .myn+opa
iOS:
Pnc. 27.6. Cnncok Scheme
Hoc+pon+e n sanyc+n+e npnnoenne. Komnnn+op nrae+ npeynpeenne o +om,
n+o me+o '$$Z'2h: eme ne peannsonan. Hoka ne o6paman+e na nero nnnmann; nckope
mr peannsyem '$$Z'2h:.
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
179
3myn+op nosnone+ sanycka+r npnnoenn Cocoa Touch na nac+onrnom
komnrm+epe. 3+o npoc+on n 6rc+prn cnoco6 ynne+r, kak nama nporpamma 6ye+
nrrne+r n pa6o+a+r na yc+ponc+ne iOS.
Br ynnn+e nce npec+annenn, cosannre n nac+poennre n 'AA-*,'&*#+:
$*$`*+*24c'.+,4*+8E*&4A&*#+2:, no cena+r onn nnnero ne mory+. Bonee +oro,
nonr+ka naa+r knonky Insert npnnee+ k c6om npnnoenn, no+omy n+o me+o
enc+nn knonkn '$$Z'2h: eme ne peannsonan. (3+o ona ns npnnnn, no ko+opon
komnnn+op nran npeynpeenne.):
Pnc. 27.7, narpamma o6rek+on iTahDoodle
Cnnsmnanne va6nnunoro npe(cvannennn
Ta6nnnnoe npec+annenne o+o6paae+c na .kpane, no ono non+n ne nmee+, kakne
annre n nem onnr nrnon+rc. Ta6nnnnoe npec+annenne, 6yynn o6rek+om
npec+annenn, ne pacnonarae+ nnkakon nnqopmannen o cnonx annrx. Emy nyen
o6rek+, ko+oprn 6ye+ ncnonrsona+rc n kanec+ne nc+onnnka annrx. B *i'4O##$-/
nc+onnnkom annrx +a6nnnnoro npec+annenn 6ye+ .ksemnnp aPN!AAO/-/8'&/.
B qanne BNRAppDelegate.m nsmenn+e me+o 'AA-*,'&*#+:
$*$`*+*24c'.+,4*+8E*&4A&*#+2: +ak, n+o6r on o+npannn +a6nnnnomy
npec+annennm coo6menne, nasnanammee .ksemnnp aPN!AAO/-/8'&/ ero nc+onnnkom
annrx:
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
180
@@ gpuwj w ufn{go ntwug|g s{jpfnqtjuwv
&'2hZ'M-/ k <<QKZ'M-/*/L '--#,= *+*&E*&4`('6/\&'M-/`('6/
2&_-/\QKZ'M-/*/L%&_-/b-'*+=H
<&'2hZ'M-/ 2/&%/A'('&#(%&_-/\QKZ'M-/*/L^/--%/A'('&#(%&_-/P#+/=H
FF )_+)\<+&< (<*?<,; ;:e<*() &B(;\+&*;A l)++%[ ():Z&\+;,; @9<lB()>Z<+&E
3PQpoP/i1 Q13yP3PxSaI21Q1iKW
@@ gpuwj w ufn{go njofngqg|g sgtv ptv qqgp ugqr p
&'2h`*/-$ k <<QKZ/d&`*/-$ '--#,= *+*&E*&4`('6/\B*/-$`('6/=H
H+o6r nc+onnnk annrx +a6nnnnoro npec+annenn nrnonnn cnom pa6o+y, on
onen peannsona+r me+or npo+okona QKiM-j*jLO&%g.(fj. Cnanana cneye+
o6rnn+r o noepke .+oro npo+okona aPN!AAO/-/8'&/ n qanne BNRAppDelegate.h:
*+&/(B',/ aPN!AAO/-/8'&/ \ QKN/2A#+$/(
1QK!AA-*,'&*#+O/-/8'&/` NoP/i141OyP3PxSaI215
?
QKZ'M-/*/L :&'2hZ'M-/H
QKZ/d&`*/-$ :&'2h`*/-$H
QKa.&&#+ :*+2/(&a.&&#+H
P%.&'M-/!(('_ :&'2h2H
J
U 7;#*$>'$$Z'2h\7*$>2/+$/(H
Hpo+okon QKZ'M-/*/LO'&'%#.(,/ coepn+ na o6sa+enrnrx me+oa,
ko+opre onnr 6r+r peannsonanr knaccom aPN!AAO/-/8'&/. Kak mnnnmym nc+onnnk
annrx +a6nnnnoro npec+annenn onen 6r+r cnoco6en coo6mn+r +a6nnnnomy
npec+annennm, ckonrko c+pok coepn+ saanna ceknn +a6nnnr n n+o onno
o+o6paa+rc n nenke saannon c+pokn. Peannsyn+e me+or coo+ne+c+nymmnm
o6pasom:
HIPhTP TPIp 6 oP/i1 41O TPJPh1T1J3
6 LxNJ31h1IV3P/i141OLNoP/i141O XV3P/i141O
JaT/1I.KSOQNJx1234SJLxNJ31h1IVQ1234SJ
z
@@ no oo puugj ntwugj s{jpfnqtjuwj fgpj{ywn ngtog gpux
@@ fjow9 ogtwjfnqg fn{go q uj {qug ogtwjfnqx tjjungq
@@ ffwq &'2h2
I13aIJ 3PQpQ 2SaJ3W
|
6 LNoP/i141O71ii XV3P/i141OLNoP/i141O XV3P/i141O
21iiSISO3NJw1{P3RLxNJw1{P3R XV4Jw1{P3R
z
@@ ptv xtx}juwv rfn{gpjfnqwv r ugqg wfsgtxj
@@ vjow9 qr}jp}wj s{jpjtr o{u9 w qgq{j w
@@ f ugqr fgpj{ywr qjfng ng|g9 ngr qfj|p fgpqn
@@ ugqrj vjow3 ut r s{gqj{vj9 wjjnfv tw vjo9
@@ pgfnxsuv ptv sgqng{ug|g wfsgtgquwv3
NoP/i141O71ii X2 j 3PQpoP/i1 w1a1a11aQP/i171ii43RNw1J34K41IM71iiMW
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
181
4K L2V z
@@ 33 w fgpj ugqx vjox ngtog q ng ftxj9
@@ jftw pgfnxsur vjjo ujn3
2 j NoP/i141O71ii PiiS2 4J4343Rx3qi1NoP/i141O71iix3qi1y1KPai3
I1aQ1Nw1J34K41IM71iiMW
J
@@ nj vjo ufn{wqjnfv q fggnqjnfnqww f wug{wj
@@ gjon gpjtw 7q u}j ftxj ng ffwq &#$#K&/62>
xx3I4Jh X431T j 3PQpQ S/01233NJw14Jw1{P3R ISOW
2 313P/1i Q13o13431TW
FF PJw RPJw /P2p 3S 3R1 3P/i1 541O 3R1 HISH1Iiq 2SJK4haI1w 21ii
I13aIJ 2W |
Pnc. 27.8, Honna narpamma o6rek+on iTahDoodle
H+o6r npo+ec+npona+r npnnoenne, o6anr+e annre npmo n maccnn n nanane
PHHi42P34SJw4w4J4QRPaJ2R4Jh43R.H34SJQ.
U 7ac>'AA-*,'&*#+\7QK!AA-*,'&*#+ :>'AA-*,'&*#+
$*$`*+*24c'.+,4*+8E*&4A&*#+2\7P%O*,&*#+'(_ :>-'.+,4A&*#+2
?
@@ zgsrno |{xow fxjfnqxj|g fswfo p
@@ w ffwq 9 {uvj|gfv u pwfoj3
P%!(('_ :A-*2& k <P%!(('_ '(('_E*&4^#+&/+&2B`*-/\$#,b'&47>=H
*B 7A-*2&> ?
@@ ftw ug{ puur fxjfnqxjn9 gu ogsw{xjnfv
@@ q sj{jjuux ojstv{3
&'2h2 k <A-*2& 6.&'M-/^#A_=H
J /-2/ ?
@@ q s{gnwqug ftxj s{gfng fgpj sxfng wfgpur ug{3
&'2h2 k <<P%.&'M-/!(('_ '--#,= *+*&=H
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
182
J
FF A)BB&> 3PQpQ @?B(^
4K L3PQpQ 2SaJ3 jj bV z
FF {a3 QST1 Q3I4JhQ 4J 43
3PQpQ Pww./0123MPip 3R1 wShQMW
3PQpQ Pww./0123M11w 3R1 RShQMW
3PQpQ Pww./0123M7RSH 3R1 iShQMW
|
@@ fgpuwj w ufn{go ojstv{ QKE*+$#L
^"N/,& L*+$#L`('6/ k <<QK%,(//+ 6'*+%,(//+= M#.+$2=H
QKE*+$#L :&4/E*+$#L k <<QKE*+$#L '--#,= *+*&E*&4`('6/\L*+$#L`('6/=H
<2/-B 2/&E*+$#L\&4/E*+$#L=H
333
J
Hoc+pon+e n sanyc+n+e npnnoenne. B +a6nnnnom npec+annennn onnr
o+o6paa+rc +ec+onre annre, no nonre saann no-npenemy ne o6annm+c. Ba
eno!
o6annenne nonmx sa(au
Cosana .ksemnnp QKa.&&#+ n 'AA-*,'&*#+\$*$`*+*24c'.+,4*+8E*&4A&*#+2, mr
saann n nero napy npnemnnk/enc+nne:
<*+2/(&a.&&#+ '$$Z'(8/&\2/-B
',&*#+\2/-/,&#(7'$$Z'2h\>
B#(^#+&(#-;/+&2\QK^#+&(#-;/+&Z#.,4QAK+2*$/=H
Hpnemnnkom nne+c self, a enc+nnem - '$$Z'2h:. Taknm o6pasom, knonka
K+2/(& o+npanne+ aPN!AAO/-/8'&/ coo6menne '$$Z'2h:. Cneona+enrno, mr onnr
peannsona+r me+o '$$Z'2h: n BNRAppDelegate.m.
6 L5S4wVPwwoPQpL4wVQ1Jw1I
z
FF @;Z?\<+&< _)l)\&
xx3I4Jh X3 j 3PQp41iw 313W
FF >%[;l` <BZ& @;Z< 3PQp41iw @?B(;
4K L3 4QaPioSx3I4JhMMV z
I13aIJW |
FF >*Z\<+&< _)l)\& > 9):;\&f A)BB&>
3PQpQ Pww./01233W
FF ;:+;>Z<+&< ():Z&n%` \(;:% > +<f ;(;:9;=)ZBE +;>%f 'Z<A<+(
3PQpoP/i1 I1iSPwyP3PW
FF ;\&B(*) (<*B(;>;,; @;ZE
3PQp41iw Q13o13MMW
FF *Z)>&)(?9) ?:&9)<(BE B '*9)+)
3PQp41iw I1Q4hJ4IQ31QHSJw1IW
|
A n+o .+o sa nrson (/2*8+`*(2&N/2A#+$/(? B nyx cnonax eno o6c+on+ +ak.
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
183
Heko+opre o6rek+r npec+annennn +ake nnm+c .nemen+amn ynpannenn -
npec+annennmn, c ko+oprmn nonrsona+enr moe+ nsanmoenc+nona+r. Knonkn,
+ekc+onre non, nonsynkn - nce .+o npn mepr .nemen+on ynpannenn. Kora na .kpane
naxo+c .nemen+r, onn ns nnx moe+ o6naa+r oco6rm c+a+ycom 0$&('%'
'.&).'#=!/), ko+oprn osnanae+, n+o .nemen+ pearnpye+ na nno +ekc+a c knanna+ypr
nnn pyrne enc+nn (ckaem, nc+pxnnanne yc+ponc+na n o+menr onepannn).
Kora nonrsona+enr nanmae+ .nemen+ ynpannenn, ko+oprn moe+ nonyna+r
c+a+yc nepnoro o6pa6o+nnka, .+omy .nemen+y o+npanne+c coo6menne
(j2*8+`*(2&N/2A#+$/(. o +oro, kak pyron .nemen+ c+ane+ nepnrm o6pa6o+nnkom
nnn +ekymemy .nemen+y 6ye+ o+npanneno coo6menne (/2*8+`*(2&N/2A#+$/(, .+o+
.nemen+ 6ye+ coxpan+r ykasannrn c+a+yc n nonyna+r nno c knanna+ypr n
nc+pxnnann yc+ponc+na.
Kora .nemen+ nnoa +ekc+a (nanpnmep, +ekc+onoe none) c+anonn+c nepnrm
o6pa6o+nnkom, na .kpane nonne+c knanna+ypa. Hoka +ekymnm nepnrm o+ne+nnkom
oc+ae+c .nemen+, nonynammnn +ekc+onrn nno, knanna+ypa oc+ae+c na .kpane. B
konne '$$Z'2h: mr npnkasrnaem +ekc+onomy nonm o+kasa+rc o+ c+a+yca nepnoro
o6pa6o+nnka, n pesynr+a+e nero knanna+ypa ncnesae+ c .kpana.
Hoc+pon+e n sanyc+n+e npnnoenne. Tenepr nr moe+e nnon+r nonre saann!
Coxpanenne sa(au
B *Z'4O##$-/ oc+anocr peannsona+r nocnenmm qynknnm. Ec+ec+nenno, kora
nonrsona+enr sakprnae+ npnnoenne, on paccnn+rnae+ na +o, n+o cnncok saan 6ye+
coxpanen n ncnonrsonann n 6yymem.
Kora npnnoenne Cocoa Touch sanepmae+c nnn nepexon+ n qononrn penm,
ono o+npanne+ cnoemy enera+y coo6menne ns npo+okona QK{{-*f&*g+Oj-j8&j,
n+o6r enera+ mor san+rc enom n koppek+no o+- pearnpona+r na .+n co6r+n. B
qanne BNRAppDelegate.m sanonnn+e sarnymkn nyx me+oon enera+a,
o6ecnennnammnx coxpanenne cnncka:
U 7;#*$>'AA-*,'&*#+O*$+&/(a',h8(#.+$\7QK!AA-*,'&*#+ :>'AA-*,'&*#+
?
FF '(;( A<(;l >%_%>)<(BE (;Z]*; > 4.x k"bt
FF B;[9)+<+&< A)BB&>) 3PQpQ +) l&B*<
3PQpQ OI431oS4i1wS2{P3RLV P3ST42PiiqxW
J
U 7;#*$>'AA-*,'&*#+E*--Z/(6*+'&/\7QK!AA-*,'&*#+ :>'AA-*,'&*#+
?
FF (;( A<(;l >%_%>)<(BE (;Z]*; > ><9B&E 4.x l; k"b
FF -;[9)+<+&< A)BB&>) 3PQpQ +) l&B*<
3PQpQ OI431oS4i1wS2{P3RLV P3ST42PiiqxW
J
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
184
Hoc+pon+e n sanyc+n+e ro+onoe npnnoenne. 3+o ynpanenne ncero nnmr ae+
nepnoe npec+annenne o nporpammnponannn n iOS. Bam npec+on+ ysna+r namnoro,
namnoro 6onrme.
nn caumx n6onmvnmx: kak nacuev main()?
B camom nanane cnoero snakomc+na c C n Objective-C nr ysnann, n+o +onkon
nxoa n ko nporpammr nne+c qynknn 6'*+7>. 3+o y+nepenne a6conm+no
cnpanennno n n nporpammnponann Cocoa/Cocoa Touch, xo+ n npnnoennx Cocoa n
Cocoa Touch .+a qynknn peak+npye+c kpanne peko. H+o6r non+r, nonemy,
o+kpon+e qann main.m:
(/&.(+ QK!AA-*,'&*#+'*+7'(8,9 '(8;9 +*-9 P%%&(*+8`(#6^-'227<aPN!AAO/-/8'&/
,-'22=>>H
Hpmo ckaem, ne nnena+ne+. Bcero ona c+poka peanrnoro koa?
dynknn QK!AA-*,'&*#+'*+7> cosae+ o6rek+r, neo6xonmre n pa6o+r
namero npnnoenn. Cnanana ona cosae+ ennc+nennrn .ksemnnp knacca
QK!AA-*,'&*#+. anee cosae+ .ksemnnp knacca, onpeenemoro ne+nep+rm
(nocnennm) aprymen+om, n nasnanae+ ero enera+om npnnoenn. enera+y 6yy+
o+npann+rc pasnnnnre coo6menn: o nexna+ke nam+n, o sanepmennn nnn nepexoe n
qononrn penm npnnoenn, o sanepmennn nponecca sanycka.
Tak qynknn 6 ' * + 7 > cnsrnae+c c
'AA-*,'&*#+\$*$`*+*24c'.+,4*+8E*&4A&*#+2: n koom namero npnnoenn.
Fnana 27. Hepnoe npunoxeuue iOS ______________________________________________________________________________________
185
C" G<9>;< @9&Z;=<+&<
-;B;)
B .+on rnane mr cosanm Z'4O##$-/ - nac+onrnoe npnnoenne Cocoa. Kak n
Z'4O##$-/, ono npec+anne+ co6on npoc+on cnncok saan n xpann+ annre n cnncke
cnonc+n; onako mey nym npnnoennmn cymec+nym+ neko+opre pasnnnn. B
npnnoennn iOS mr ncnonrsonann .ksemnnpr QKiM-j*jL, QKZ/d&`*/-$ n
QKa.&&#+. B nac+onrnon nepcnn cnncok saan 6ye+ pasmema+rc n .nemen+e
P%iM-j*jL, re n 6ye+ peak+npona+rc nanpmym. Take n npnnoennn
ncnonrsye+c .nemen+ P%a.&&#+, ko+oprn nc+anne+ n +a6nnny c+poky n nnoa nonon
saann.
Pnc. 28.1, Hpnnoenne TahDoodle
Kpome +oro, n nocnenen rnane nonrsona+enrcknn nn+epqenc npnnoenn
c+ponnc na nporpammnom yponne. B .+on rnane mr nocnonrsyemc nnsyanrnrm
nnc+pymen+om Interface Builder, nknmnennrm n Xcode, n cosann, nac+ponkn n
cnsrnann .nemen+on nonrsona+enrckoro nn+epqenca.
B Xcode nrnonnn+e komany `*-/P/LP/Lb(#/,&333. B ceknnn Mac OS X
menknn+e na c+poke !AA-*,'&*#+. Br6epn+e n o+kprnmemc cnncke ma6non Cocoa
!AA-*,'&*#+ n npncnon+e npoek+y nm Z'4O##$-/. Hpnnoenne Z'4O##$-/ nne+c
okymen+nrm, +o ec+r nonrsona+enr moe+ epa+r o+kpr+rmn cpasy neckonrko
cnnckon saan. Hone Document Extension coepn+ pacmnpenne qanna, ncnonrsyemoe
npn coxpanennn okymen+on (cnnckon saan) na ncke. Ykan+e n .+om none, n+o namn
qannr annrx 6yy+ ncnonrsona+r pacmnpenne &$-. Hpnnoenne Z'4O##$-/ ne
ncnonrsye+ Core Data n moynrnre +ec+r emy +oe ne nynr.
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
186
peouo uenuok
eknkuae1 pexuV
pepak1upoeauun
c1poku
c1poka saronoeka
NSWindow
uenuok ua
NSButton
ec1aenne1 uoeyk
c1poky
Pnc. 28.2. Cosanne nonoro npnnoenn Cocoa
Pe(akvnponanne qanna BNRDocument.h
O+kpon+e qann MNRD'+!#$0t.h, nknmnn+e n nero me+o n ne nepemennre .ksemnnpa:
&#$#K&/62 - nsmenemrn maccnn c+pok, a *&j6i'M-j*jL - ykasa+enr na o6rek+
P%iM-j*jL, n ko+opom o+o6paam+c c+pokn ns &#$#K&/62. Take o6rnn+e, n+o
aPNO#,.6/+& noepnnae+ npo+okon P%iM-j*jLO&%g.|fj.
)*6A#(& 1^#,#'@^#,#'345
*+&/(B',/ aPNO#,.6/+& \ P%O#,.6/+& xoP/i141OyP3PxSaI21 z
x}a3P/i1IIPq X3SwSN31TQW
N~.a3i13 xoP/i141O X431ToP/i141OW
|
6 LN~234SJV2I1P311ON31TL4wVQ1Jw1IW
/+$
O6pa+n+e nnnmanne: y knonkn K+2/(& ne+ nepemennon .ksemnnpa. (Bckope nr
nonme+e, nonemy.) Tem ne menee .+on knonke nasnaneno enc+nne - me+o
,(/'&/P/LK&/6:.
B nperymen rnane npnemnnkom enc+nn knonkn 6rn .ksemnnp knacca
enera+a npnnoenn, aPN!AAO/-/8'&/. okymen+no-6asnponannoe npnnoenne ne
nmee+ o6rek+a enera+a npnnoenn, a n ocnony ero apxn+ek+ypr sanoen cy6knacc
P%O#,.6/+&. n npnnoenn Z'4O##$-/ .+o knacc aPNO#,.6/+&.
B okymen+no-6asnponannom npnnoennn mory+ ononpemenno cymec+nona+r
neckonrko .ksemnnpon o6rek+on okymen+on. Cneona+enrno, no npem nrnonnenn
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
187
Z'4O##$-/ mory+ cymec+nona+r cpasy neckonrko .ksemnnpon aPNO#,.6/+& (neckonrko
cnnckon saan). Karn .ksemnnp nmee+ co6c+nennoe +a6nnnnoe npec+annenne,
knonky, maccnn tasks n okno. Karn .ksemnnp pearnpye+ na coo6menn nesanncnmo
o+ pyrnx, n karn .ksemnnp nne+c npnemnnkom cnoen knonkn.
Bo nneennrx namn o6rnnennx +ake nonnnncr na nonrx o6osnanenn:
Ka.&-/& n Ka!,&*#+. Onn coo6mam+ Xcode:. 3+o ykasa+enr (Ka.&-/&) nnn me+o
enc+nn (Ka!,&*#+), n cnsrnann ko+oprx paspa6o+nnk 6ye+ ncnonrsona+r
Interface Builder - nmec+o cnsrnann na nporpammnom yponne.
3nakoucvno c Interface Builder
B nannra+ope npoek+a nann+e n nr6epn+e qann c nmenem BNRDocu#ent.xib. Kora n
nannra+ope npoek+a nr6npae+c qann c pacmnpennem.xib (XML-okymen+ Interface
Builder), n nanenn peak+opa o+kprnae+c Interface Builder make+non ce+kon.
B annrn momen+ na ce+ke naxon+c ncero onn o6rek+ npec+annenn - o6rek+
okna. 3+o .ksemnnp NSWindow, k ko+opomy mr nckope o6annm pyrne o6rek+r
npec+annennn.
B npanom nepxnem yrny okna Xcode menknn+e na npanon knonke */L, n+o-6r
o+kpr+r nanenr Q&-*&*j2. Hann+e n nepxnen nac+n nanenn Q&*-*&*/2 knonky
ptg999
A ook at Interface Buder
193
to-do lists). Each instance will have its own table view, button, tasks array, and window. Each instance
will respond to messages independently of the others, and each instance will be its own button`s target.
In the declarations you entered, there are also two new terms: TB0u1e1 and TBAc11oh. These tell
Xcode 'This is a pointer (TB0u1e1) or an action method (TBAc11oh) that the developer will use
nterface Builder to connect rather than doing so programmatically.
A look at lnterface Builder
In the project navigator, find and select a file named BhR0ocumeh1.x1b. When you select a file in the
project navigator that ends in .x1b (XML Interface Builder document), nterface Builder appears in the
editor pane, displaying a layout grid.
Right now, there is only one view object on the layout grid - a window object. That`s an instance of
, to which you`ll add other view objects shortly.
Now, in the upper-right corner of the Xcode window, click on the righthand View button to reveal the
Utilities. At the top of Utilities, click the button to reveal the inspector.
The inspector is subdivided into separate inspectors. In this chapter, you`ll use the attributes, size, and
connections inspectors.
Fgure 28.3 Attrbutes, sze, and connectons nspectors
At the bottom of the Utilities pane, below the inspector, is the library, which is also divided into tabs.
Select the button to reveal the object library. This library presents a list of all of the different object
n
menknn+e na nen, n+o6r o+kpr+r nanenr !"+0$/#'&).
Ocnonnon nncnek+op ononnn+enrno enn+c na neckonrko nncnek+opon. B .+on
rnane mr ncnonrsyem nncnek+opon a+pn6y+on, pasmepon n cnsen.
wf3 VW3S3 ufsjong{r n{wxngq9 {j{gq w fqvj
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
188
B nnnen nac+n nanenn Q&*-*&*/2, no nncnek+opom, naxon+c .!.1!'#$/),
+ake pasenenna na nknakn. Br6epn+e knonky
ptg999
A ook at Interface Buder
193
to-do lists). Each instance will have its own table view, button, tasks array, and window. Each instance
will respond to messages independently of the others, and each instance will be its own button`s target.
In the declarations you entered, there are also two new terms: TB0u1e1 and TBAc11oh. These tell
Xcode 'This is a pointer (TB0u1e1) or an action method (TBAc11oh) that the developer will use
nterface Builder to connect rather than doing so programmatically.
A look at lnterface Builder
In the project navigator, find and select a file named BhR0ocumeh1.x1b. When you select a file in the
project navigator that ends in .x1b (XML Interface Builder document), nterface Builder appears in the
editor pane, displaying a layout grid.
Right now, there is only one view object on the layout grid - a window object. That`s an instance of
, to which you`ll add other view objects shortly.
Now, in the upper-right corner of the Xcode window, click on the righthand View button to reveal the
Utilities. At the top of Utilities, click the button to reveal the inspector.
The inspector is subdivided into separate inspectors. In this chapter, you`ll use the attributes, size, and
connections inspectors.
Fgure 28.3 Attrbutes, sze, and connectons nspectors
At the bottom of the Utilities pane, below the inspector, is the library, which is also divided into tabs.
Select the button to reveal the object library. This library presents a list of all of the different object , n+o6r o+kpr+r 6n6nno+eky
o6rek+on. B nen coepn+c cnncok ncex +nnon o6rek+on, nsnec+nrx Interface Builder, n
secr e pasmemam+c nn+epqencnre .nemen+r, ko+opre onnr nepe+acknna+rc
paspa6o+nnkom n o6rek+ okna.
B nnnen nac+n 6n6nno+ekn pacnonoeno none noncka. Bnen+e n nem c+poky
&M-j, n+o6r o+qnnr+pona+r cnncok o6rek+on. Hepnrn .nemen+, Table View,
npec+anne+ .ksemnnp knacca P%ZM-j*jL. Br moe+e menkny+r na nem, n+o6r
npocmo+pe+r nopo6nym nnqopmannm.
Pe(akvnponanne qanna BNRDocument.xib
Hepe+amn+e .ksemnnp P%iM-j*jL ns 6n6nno+ekn o6rek+on n o6rek+ okna. Hsmenn+e
pasmepr +a6nnnr +ak, n+o6r ona sakprnana 6onrmym nac+r okna, no oc+anr+e mec+o
nnnsy n knonkn.
Pnc. 28.4. o6annenne NSTableView
O6pa+n+e nnnmanne: kpa o6rek+a an+oma+nneckn nrpannnnam+c no
nanpannmmnm npn npn6nnennn k kpam okna nnn pyrnx o6rek+on. 3+n
nanpannmmne o6ecnennnam+ nrpannnnanne o6rek+on npec+annennn n coo+ne+c+nnn c
pekomenannmn Human Interface Guidelines (HIG) qnpmr Apple - npannnamn,
ko+opre onen co6nma+r karn paspa6o+nnk npn npoek+nponannn
nonrsona+enrcknx nn+epqencon n Mac. Take cymec+nym+ okymen+r HIG n
iPhone n iPad. Bce okymen+r HIG npnneenr n okymen+annn paspa6o+nnka.
Tenepr mr saanm neko+opre a+pn6y+r +a6nnnnoro npec+annenn n
nncnek+ope a+pn6y+on. O6rnno n .+oro paspa6o+nnk menkae+ na o6rek+e na
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
189
make+non ce+ke, a kon+ekc+ nncnek+opa nsmene+c n o+o6paenn a+pn6y+on
nr6pannoro o6rek+a. o6pa+rc o a+pn6y+on .ksemnnpa P%Z'M-/*/L cnonee.
O6rek+ +a6nnnnoro npec+annenn, nepe+amennrn n okno, n enc+nn+enrnoc+n
npec+anne+ co6on na6op nnoennrx o6rek+on: P%%,(#--*/L, P%iM-j*jL n onn
nnn neckonrko .ksemnnpon P%Z'4-/^#-.6+. H+o6r o6pa+rc o nynoro o6rek+a
.+oro na6opa, yepnnan+e naa+rmn knannmn ^#+&(#- n %4*B& npn mennke na
+a6nnnnom npec+annennn. Ho kypcopom nonne+c cnncok o6rek+on, ns ko+oporo
mono nr6pa+r nynrn nam. Br6epn+e o6rek+ P%Z'M-/*/L.
B nncnek+ope a+pn6y+on nac+pon+e +a6nnnnoe npec+annenne +ak, n+o6r ono
coc+ono ns onoro c+on6na. Ba+em nepnn+ecr n peak+op, nr6epn+e sarononok
+a6nnnnoro npec+annenn n pac+nn+e ennc+nennrn c+on6en o nonnon mnpnnr
+a6nnnnoro npec+annenn.
Pnc. 28.5. Br6op npec+annenn ns c+onkn
Mr akkypa+no pacc+annnn npec+annenn no mec+am, no n+o nponsone+, kora
nonrsona+enr nsmenn+ pasmep okna npnnoenn? Heo6xonmo nosa6o+n+rc o +om,
n+o6r +a6nnnnoe npec+annenne nsmenno pasmepr n okne. Boo6me ronop, nsmen+rc
nmec+e c oknom 6ye+ npokpynnnaemoe npec+annenne, ko+opoe coepn+ +a6nnnnoe
npec+annenne. Eenknn+e na +a6nnnnom npec+annennn c naa+rmn knannmamn
^#+&(#- n %4*B&, nr6epn+e P%%,(#--*/L ns cnncka. B nepxnen nac+n nanenn
nncnek+opa menknn+e na knonke n+o6r o+kpr+r nncnek+op pasmepon. Hann+e n
nem pasen !.&#2**+8.
Pnc 28.6 Hsmenenne mnpnnr c+on6na
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
190
3+o+ pasen coepn+ macky an+oma+nneckoro nsmenenn pasmepon - .nemen+
ynpannenn, nosnonmmnn nac+pon+r peaknnm nr6pannoro o6rek+a npec+annenn na
nsmepenne pasmepon cynepnpec+annenn. Hpec+annenn, kak n knaccr, o6pasym+
nepapxnm, no.+omy kaoe npec+annenne moe+ nme+r cynepnpec+annenn n
cy6npec+annenn. Cynepnpec+annennem P%%,(g--*/L nne+c .ksemnnp P%E*+$#L.
Macka an+oma+nneckoro nsmenenn pasmepon coc+on+ ns ne+rpex kopen n nne
6yknr I n nyx 0&62!" n nne nyc+oponnnx c+penok. Br6op onon ns npynn
nosnone+ nr6pannomy o6rek+y npec+annenn pacmnp+rc n saannom nanpannennn
npn pacmnpennn cynepnpec+annenn. Br6op kop npnnsrnae+ nr6pannrn o6rek+
npec+annenn k saannomy kpam ero cynepnpec+annenn. K cnac+rm, cnpana o+ mackn
an+oma+nneckoro nsmenenn pasmepon o+o6paae+c yo6na annmann, no ko+opon
mono coc+ann+r npec+annenne o +om, kak 6ye+ pa6o+a+r nr6pannrn o6rek+
npec+annenn c saannon kom6nnannen kopen n npynn.
Br6epn+e n macke an+oma+nneckoro nsmenenn pasmepon nce ne+rpe kop n o6e
npynnr. Tem camrm nr npnnsrnae+e nce ne+rpe c+oponr npokpynnnaemoro
npec+annenn k coo+ne+c+nymmnm kpam okna n nosnone+e npokpynnnaemomy
npec+annennm (n coepamemyc n nem +a6nnnnomy npec+annennm) pacmnp+rc no
ropnson+ann n nep+nkann npn nsmenennn pasmepon okna.
Pnc. 28.7. Hac+ponka mackn an+oma+nneckoro nsmenenn pasmepon
Tenepr nepenem o+ +a6nnnnoro npec+annenn k knonke. Bepnn+ecr n
6n6nno+eky o6rek+on. Hann+e .ksemnnp P%a.&&#+ n nepe+amn+e ero n o6rek+ okna.
Br moe+e nr6pa+r nm6on ns c+nnen knonok, npec+annennrx n 6n6nno+eke;
knaccnnecknn napnan+ - npmoyronrna knonka c sakpyrnennrmn kpamn (Rounded Rect
Button). Hocne +oro kak knonka 6ye+ nepe+amena n okno, nr moe+e nsmenn+r nanncr
na knonke; n .+oro cenan+e nonnon mennok na +ekc+e knonkn n nnen+e nynrn
+ekc+. B namem npnnoennn cneye+ nnec+n +ekc+ K+2/(&.
Hakonen, n nncnek+ope pasmepon saan+e macky an+oma+nneckoro nsmenenn
pasmepon +ak, n+o6r knonka 6rna npnnsana k nenomy nnnemy yrny okna n coxpanna
cnon +ekymne pasmepr.
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
191
Pnc. 28.8. Macka an+oma+nneckoro nsmenenn pasmepon n knonkn Insert
H+ak, mr cosann na o6rek+a npec+annennn, neo6xonmrx n npnnoenn
Z'4O##$-/: P%iM-j*jL n P%a.&&#+, a +ake nac+ponnn napame+pr .+nx o6rek+on.
n +a6nnnnoro npec+annenn 6rno saano konnnec+no c+on6non, a n knonkn - +ekc+
nanncn. Mr +ake nosa6o+nnncr o6 nx npannnrnom nsmenennn pasmepon n
nosnnnonnponannn npn nsmenennn pasmepon okna.
Kora nr enae+e nonnon mennok na P%a.&&#+ n nnon+e +ekc+ nanncn,
pesynr+a+ .knnnanen+en +omy, ko+oprn oc+nranc n nperymen rnane o6annennem
cneymmen c+pokn koa:
<*+2/(&a.&&#+ 2/&Z*&-/\CK+2/(&C
B#(%&'&/\QK^#+&(#-%&'&/P#(6'-=H
H+ak, n kaknx cnynax cneye+ ncnonrsona+r Intecface Builder, a kora nynme
cosana+r npec+annenn na nporpammnom yponne? B npoc+rx cnynax noone+ nm6oe
pemenne. Mr mornn noc+pon+r nn+epqenc *Z'4O##$-/ c ncnonrsonannem XIB. Onako
n o6mem cnynae nem cnonee nn+epqenc, +em 6onrme npnnnn n ncnonrsonann
Interface Builder.
Pnc. 28.9. BNRDocument.xib c nac+poennrmn npec+annennmn
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
192
Cnnsmnanne npe(cvannennn
Bosmonoc+n Interface Builder ne orpannnnnam+c cosannem n nac+ponkon
npec+annennn. Paspa6o+nnk +ake moe+ o6renn+r o6rek+r npec+annennn c koom
npnnoenn - n nac+noc+n, onpeenn+r napr npnemnnk/enc+nne n npncnon+r
ykasa+enn.
Cnena o+ make+non ce+kn cnncok samec+n+enen (b-',/4#-$/(2) n o6rek+on.
On nasrnae+c +(',/'4 ,'/63$"#) ($#,.6/+& #.&-*+/). (Ecnn nr nnn+e +onrko na6op
snankon 6es nanncen, menknn+e na knonke
ptg999
Chapter 28 Your Frst Cocoa Appcaton
198
So when do you use nterface Builder and when do you set up views programmatically? Under simple
circumstances, either will work. We could have built iTahDoodle`s interface with a XIB. In general,
however, the more complex your user interface, the more sense it makes to use nterface Builder.
(Now that you`ve seen more of Xcode, take a look at the tear-out card at the back of this book. This
card contains keyboard shortcuts for navigating around Xcode. There are a bunch of them! As you
continue with Xcode, use this card to find shortcuts that will save you time and clicks.)
Making connections
Creating and configuring views is not all that you can do in nterface Builder. You can also connect, or
'wire, these view objects to your application`s code. In particular, you can set target-action pairs and
assign pointers.
To the left of the layout grid is a list of 'placeholders and objects. This is called the document outline.
(If you just see a stack of icons without labels, click the button to reveal the expanded list.)
Fgure 28.10 Mnmzed and expanded document outne
Find File's Owner under the Placeholders section of the document outline. A placeholder stands in for a
particular object that cannot be specified until runtime. File's Owner stands in for whichever object will
load this XIB as its user interface. In your case, File's Owner represents an instance of Nocumen1.
Now select the nsert button in the editor and Control-drag to File's Owner.
, n+o6r o+kpr+r pacmnpennrn cnncok.)
Hann+e n ceknnn b-',/4#-$/(2 cnokn c+poky File's Owner. Bamec+n+enr
o6osnanae+ neknn o6rek+. ko+oprn ne moe+ 6r+r saan o c+ann nrnonnenn. Tak,
File's Owner o6osnanae+ o6rek+, ko+oprn sarpysn+ annrn qann XIB n
qopmnponann cnoero nonrsona+enrckoro nn+epqenca. B namem cnynae File's Owner
npec+anne+ .ksemnnp aPNO#,.6/+&.
Br6epn+e knonky Insert n peak+ope n nepe+amn+e ee na c+poky File's Owner,
yepnna knannmy Control.
Pnc, 28.10. Cnoka okymen+a n cnepny+om n pasnepny+om nne
Kora nr o+nyckae+e knonky mrmn, na .kpane nonne+c manenrkoe okno c
nepennem ncex nosmonrx cnsen. Br6epn+e ,(/'&/P/LK&/6: - +em camrm nr
nr6npae+e enc+nne, ko+opoe onno cpa6a+rna+r npn naa+nn knonkn (pnc. 28.12)
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
193
Pnc. 28.11. Cosanne cnsn
Pnc. 28.12. Br6op enc+nn
Mr nac+ponnn napy npnemnnk/enc+nne. Pesynr+a+ .knnnanen+en cneymmemy
qparmen+y koa n *Z'4O##$-/:
<*+2/(&a.&&#+ '$$Z'(8/&\2/-B
',&*#+\2/-/,&#(7'$$Z'2h\>
B#(^#+&(#-;/+&2\QK^#+&(#-;/+&Z#.,4QAK+2*$/=H
Tenepr cnn+e ccrnky *&j6iM-j*jL knacca aPNO#,.6/+&. Hepe+amn+e c
naa+on knannmen Control c+poky File's Owner (samenmmym aPNO#,.6/+&) na
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
194
P%iM-j*jL n o+nyc+n+e knonky mrmn. Br6epn+e ennc+nennrn napnan+, ko+oprn
6ye+ npenoen: *&j6iM-j*jL.
Hakonen, menknn+e na +a6nnnnom npec+annennn c naa+rmn knannmamn
#+&(#- n %4*B&, n nr6epn+e n cnncke P%iM-j*jL. Hepe+amn+e c naa+on knannmen
#+&(#- +a6nnnnoe npec+annenne na c+poky File's Owner n nr6epn+e n o+kprnmemc
cnncke cnsen napnan+ $'&'%#.|,/.

Pnc. 28.13. Cosanne pyrnx cnsen
Pnc. 28.14, Cnsrnanne nc+onnnka annrx +a6nnnnoro npec+annenn
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
195
H+ak, n+o e mr +onrko n+o cenann? Mr npncnonnn snanenn ykasa+enen.
Knacc aPNO#,.6/+& o6rnne+ ykasa+enr na P%iM-j*jL. Mr coo6mnnn Interface
Builder, n+o .+o+ ykasa+enr onen ykasrna+r na konkpe+nrn .ksemnnp P%Z'M-j*jL,
ko+oprn 6rn nepe+amen n .+o okno. Kpome +oro, +a6nnnnoe npec+annenne coepn+
p co6c+nennrx ykasa+enen - +aknx, kak $'&'%#.(,/. Mr coo6mnnn Interface Builder,
n+o ykasa+enr $'&'%#.(,/ +a6nnnnoro npec+annenn onen ykasrna+r na nam
.ksemnnp aPNO#,.6/+&. Pesynr+a+ .knnnanen+en nporpammnomy nasnanennm
nc+onnnka annrx +a6nnnnoro npec+annenn n *Z'4O##$-/.
<&'2hZ'M-/ 2/&O'&'%#.(,/\2/-B=H
Bno knmnenre cnona Ka.&-/& n Ka!,&*#+, mr nomenaem .+n ccrnkn n
enc+nn n Interface Builder, ronop: Kora nr+amcr cnsa+r ykasa+enr n IB, .+o+
o6rek+ onen 6r+r nknmnen n cnncok nosmonrx cnsen! Bo npem nanncann koa
Interface Builder cnen+ sa knmnenrmn cnonamn Ka.&-/& n Ka!,&*#+ n snae+, kakne
napnan+r cneye+ npenara+r npn cosannn cnsen.
dak+nneckne onpeenenn Ka.&-/& n Ka!,&*#+ +pnnnanrnr:
)$/B*+/ Ka!,&*#+ ;#*$
)$/B*+/ Ka.&-/&
Hs +oro, n+o ronopnnocr panee o npek+nne )$/B*+/, c+anonn+c cno, n+o
knmnenoe cnono Ka!,&*#+ samene+c na ;#*$ eme o +oro, kak ero ynnn+ komnnn+op,
a Ka.&-/& nonnoc+rm ncnesae+ ns nporpammr. Taknm o6pasom, na c+ann
komnnnnponann nce me+kn Ka.&-/& yanm+c nonnoc+rm, a knmnenre cnona
Ka!,&*#+ samenm+c na ;#*$, no+omy n+o enc+nn, nnnnnnpyemre .nemen+amn
ynpannenn nonrsona+enrckoro nn+epqenca, ne onnr nme+r nosnpamaemoro snanenn.
Yn+n+e, n+o 3nemen+r nonrsona+enrckoro nn+epqenca na 6ase XIB ne cosam+c
nno, kak .+o enanocr npn nporpammnom cosannn nn+epqenca. Onn cosam+c
an+oma+nneckn npn sarpyske XIB no npem nrnonnenn.
Hakonen o6pa+n+e nnnmanne na o+cy+c+nne ykasa+en na knonky. eno n +om, n+o
o6rek+y nynr nepemennre .ksemnnpon +onrko n +ex o6rek+on, ko+oprm on
co6npae+c o+npann+r coo6menn. Knonka onna o+npann+r coo6menn .ksemnnpy
aPNO#,.6/+&, no.+omy mr cnsann ee enc+nne c npnemnnkom. Onako .ksemnnp
aPNO#,.6/+& ne co6npae+c o+npann+r coo6menn knonke, no.+omy ykasa+enr na
knonky emy ne nyen.
Cnona o MVC
H+ak, nonrsona+enrcknn nn+epqenc npnnoenn ro+on. anan+e nsrnnem na narpammy
o6rek+on annoro npoek+a:
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
196
P%O#,.6/+& - cynepknacc, o+ ko+oporo nacneye+ knacc aPNO#,.6/+&,- noo6me
nn+epecna m+yka. Ha nepnrn nsrn on nne+c o6rek+om moenn. Ho ns cnpanonnoro
onncann knacca P%O#,.6/+& c+anonn+c nnno, n+o .+o ckopee kon+ponnep, nem n+o-
nn6o pyroe. P%O#,.6/+& koopnnnpye+ pasnnnnre nckonre onepannn n cnsrnae+c
nanpmym c npec+annennmn, o6ecnennnammnmn nno annrx nonrsona+enem. Hpn
cosannn aPNO#,.6/+& kak cy6knacca aPNO#,.6/+& mr o6annnn ykasa+enn na
peanrnre o6rek+r moenen (maccnn P%.&M-j||x o6rek+on P%%&(*+8).
Pnc. 28.15. narpamma o6rek+on TahDoodle
Pe(akvnponanne qanna BNRDocument.m
Tenepr, kora nonrsona+enrcknn nn+epqenc npnnoenn cosan n nac+poen n n nem
cosanr nce cnsn, npnmno npem nepny+rc k nanncannm koa. Eenknn+e na qanne
BNRDocument.m n nannra+ope npoek+a, n+o6r cnona o+kpr+r ero n peak+ope, n
peannsyn+e ,(/'&/P/LK&/6:.
)*6A#(& CaPNO#,.6/+&34C *6A-/6/+&'&*#+ aPNO#,.6/+& HIPhTP TPIp 6 xyS2aT1J3
.51II4w1Q
U 7P%%&(*+8 :>L*+$#LP*MP'6/
?
(/&.(+ CaPNO#,.6/+&CH
J
HIPhTP TPIp 6 234SJQ
6 LN~234SJV2I1P311ON31TL4wVQ1Jw1I
z
FF <BZ& A)BB&> << +< B?<B(>?<(` B;_l)<A <,; lZE [9)+<+&E +;>;f _)l)\&
4K L3SwSN31TQV z
3SwSN31TQ j x}a3P/i1IIPq PIIPqW
|
3SwSN31TQ Pww./0123M1O N31TMW
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
197
FF 6I1iSPwyP3P @9&*)_%>)<( ():Z&\+;A? @9<lB()>Z<+& ;:+;>&(BE
FF & _)@9;B&(] ? &B(;\+&*) l)++%[ L*;(;9%A > l)++;A BZ?\)<
FF E>ZE<(BE ;:e<*( ~yS2aT1J3V +;>%< l)++%< lZE ;(;:9)=<+&E
431ToP/i141O I1iSPwyP3PW
FF 6aHwP317RPJh17SaJ3 B;;:)<( @9&Z;=<+&` B;l<9=&( Z& l;*?A<+(
FF +<B;[9)+<++%< &_A<+<+&E" x7RPJh1ySJ1 @;A<\)<( l;*?A<+( *)*
+<B;[9)+<++%f
Q1iK aHwP317RPJh17SaJ3x7RPJh1ySJ1W
|
Tenepr peannsyn+e o6sa+enrnre me+or nc+onnnka annrx +a6nnnnoro
npec+annenn (onpeenemre npo+okonom P%Z'M-/*/LO'&'%#.(,/):
HIPhTP TPIp yP3P xSaI21 }13RSwQ
6 LxNJ31h1IVJaT/1I.KSOQNJoP/i141OLxoP/i141O XV35
z
FF ():Z&\+;< @9<lB()>Z<+&< @9<l+)_+)\<+; lZE ;(;:9)=<+&E 3SwSN31TQ`
FF @;'(;A? *;Z&\<B(>; B(9;* > ():Z&\+;A @9<lB()>Z<+&& B;>@;l)<(
FF B *;ZZ&\<B(>;A ;:e<*(;> > A)BB&><"
I13aIJ 3SwSN31TQ 2SaJ3W
|
6 L4wV3P/i141OLxoP/i141O XV3P/i141O
S/0123Pia1SIoP/i17SiaTJLxoP/i17SiaTJ XV3P/i17SiaTJ
ISOLxNJ31h1IVISO
z
FF >;_>9;)<( 'Z<A<+( 3SwSN31TQ` B;;(><(B(>?<f E\<f*&
FF *;(;9)E l;Z=+) ;(;:9;=)(]BE > ():Z&\+;A @9<lB()>Z<+&&
I13aIJ 3SwSN31TQ S/01233NJw1ISOW
|
6 L5S4wV3P/i141OLxoP/i141O XV3P/i141O
Q13./0123Pia1L4wVS/0123
KSIoP/i17SiaTJLxoP/i17SiaTJ XV3P/i17SiaTJ
ISOLxNJ31h1IVISO
z
FF *;,l) @;Z]_;>)(<Z] &_A<+E<( _)l)\? > ():Z&\+;A @9<lB()>Z<+&&`
FF A)BB&> 3SwSN31TQ +<;:[;l&A; ;:+;>&(]
3SwSN31TQ I1HiP21./01233NJw1ISO O43R./0123S/0123W
FF _)(<A @;A<(&(] l;*?A<+( *)* B;l<9=)&f +<B;[9)+<++%< &_A&+<+&E"
Q1iK aHwP317RPJh17SaJ3x7RPJh1ySJ1W
|
Hoc+pon+e n sanyc+n+e nporpammy Okno Z'4O##$-/ nonne+c na .kpane, nr
moe+e o6ann+r n nsmen+r saann. Onako n npnnoennn o+cy+c+nye+ ona nana
nosmonoc+r: coxpanenne n non+opna sarpyska cnncka saan. H+o6r peannsona+r ee,
neo6xonmo nepeonpeenn+r cneymmne me+or, ynacneonannre o+ PO#,.6/+& -
cynepknacca aPNO#,.6/+&:
O6pa+n+e nnnmanne: nama peannsann me+oa nonynae+ P%((#(**. B annom
cnynae mr npoc+o nosnpamaem o6rek+ P%((#(, crenepnponannrn
A(#A/(&_c*2&E*&4O'&': #A&*#+2\B#(6'&\/((#(\, no +ake n sanncnmoc+n o+ npnpor
omn6kn mono 6rno 6r cosa+r n nepny+r nonrn o6rek+ P%((#(.
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
198
Cnona noc+pon+e n sanyc+n+e npnnoenne. Tenepr ono ymee+ coxpan+r n
sarpya+r cnnckn saan.
ptg999
Chapter 28Your Frst Cocoa Appcaton
204
- {hS0a1a *)da1a01Type.{hSS1J1hg *)1ypehame
eJJoJ.{hSEJJoJ **)ou1EJJoJ
{
// Ths me1hod s ca11ed When our documen1 s beng saved
// We are expec1ed 1o hand 1he ca11er an N5a1a ob]ec1 Wrappng our da1a
// so 1ha1 1 can be Wr11en 1o dsk
// I1 1here`s no array, We`11 Wr1e ou1 an emp1y array 1or noW
1 {!1odoI1ems) {
1odoI1ems = |N5Nu1ab1eArray array)
)
// Pack our 1odoI1ems array n1o an N5a1a ob]ec1
N5a1a *da1a = |N5Proper1yLs15era1za1on
da1aW1hProper1yLs1:1odoI1ems
1orma1:N5Proper1yLs1XNLForma1_v1_6
op1ons:6
error:ou1Error)
// re1urn our neW1y-packed N5a1a ob]ec1
re1urn da1a
}
- {B00L)JeadFJom0a1a.{hS0a1a *)da1a
o1Type.{hSS1J1hg *)1ypehame
eJJoJ.{hSEJJoJ **)ou1EJJoJ
{
// Ths me1hod s ca11ed When a documen1 s beng 1oaded
// We are handed an N5a1a ob]ec1 and expec1ed 1o pu11 our da1a ou1 o1 1
// Ex1rac1 our 1odoI1ems
1odoI1ems = |N5Proper1yLs15era1za1on
proper1yLs1W1ha1a:da1a
op1ons:N5Proper1yLs1Nu1ab1eCon1aners
1orma1:NuLL
error:ou1Error)
// re1urn success or 1a1ure dependng on success o1 1he above ca11
re1urn {1odoI1ems != n1)
}
Notice that for the first time, you`re implementing a method that takes in an
N5Error**. In this case, we are merely handing back the N5Error generated by
proper1yLs1W1ha1a:op1ons:1orma1:error:, but you could also create and hand back a new
N5Error as well, depending on the nature of the failure.
Build and run the application again. Now you can save and load task lists.

Add a Delete Selected tem button.


Look up the N5Error class reference in the developer documentation and create new N5Error instances
to hand back when saving and loading fails, as mentioned at the end of the chapter.
npawnennn
o6anr+e knonky yanenn nr6pannon saann.
Hpocmo+pn+e cnpanonnoe onncanne knacca P%((#( n okymen+annn
paspa6o+nnka n cosan+e .ksemnnpr P%((#(, nosnpamaemre n cnynae neyannon
nonr+kn coxpanenn nnn sarpyskn (o nem ynomnnae+c n konne rnanr).
Fnana 28. Hepnoe npunoxeuue Cocoa ______________________________________________________________________________________
199
V
Pacmnpennre
nosmonoc+n Objective-C
Cennac nr ye snae+e Objective-C n o6reme, oc+a+onnom n necnonoro
nporpammnponann iOS nnn Cocoa. Ho ne +oponn+ecr! B cneymmnx rnanax
npec+annenr mnorne npnemr n konnennnn, ko+opre cnnrno npnro+c nam n nepnrn
ro camoc+o+enrnoro nporpammnponann na Objective-C.
200
C" 4J43
Knacc P%M/,& coepn+ me+o c nmenem *+*&. Hocne nrenenn nam+n nonomy
.ksemnnpy o+npanne+c coo6menne init, n+o6r .ksemnnp mor nnnnnannsnpona+r c6on
nepemennre .ksemnnpon peanrnrmn snanennmn. Taknm o6pasom, '--#, nrene+
nam+r n o6rek+a, a *+*& ro+onn+ o6rek+ k pa6o+e. Hcnonrsonanne *+*& nrrnn+
npnmepno +ak:
P%.&'M-/!(('_ :&4*+82 k <<P%.&'M-/!(('_ '--#,= *+*&=H
Cneye+ ynn+rna+r, n+o init nne+c me+oom .ksemnnpa, ko+oprn nosnpamae+
apec nnnnnannsnponannoro o6rek+a. On o6ecnennnae+ nnnnnannsannm P%M/,&. B
.+on rnane nr ysnae+e, kak nnmy+c nnnnnannsa+opr.
Hanncanne uevo(on init
Cosan+e nonrn npoek+: nporpammy komannon c+pokn Foundation c nmenem !AA-*'+,/2.
B .+on nporpamme mr cosanm na knacca: Appliance n L+/$!AA-*'+,/ (cy6knacc
Appliance). Knacc !AA-*'+,/ (yc+ponc+no) coepn+ nepemennre productName
(nasnanne npoyk+a) n ;#-&'8/ (nanpenne). 3ksemnnp OwnedAppliance +ake 6ye+
coepa+r mnoec+no c nmenamn nnaenrnen.
Cosan+e nonrn qann: cy6knacc P%M/,& c nmenem !AA-*'+,/. B qanne
Appliance.h cosan+e nepemennre .ksemnnpon n o6rnnenn cnonc+n n {|g$.,&P'6/ n
;#-&'8/:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ !AA-*'+,/ \ P%M/,& z
xx3I4Jh XHISwa23PT1W
4J3 5Si3Ph1W
|
HISH1I3q L2SHqV xx3I4Jh XHISwa23PT1W
HISH1I3q 4J3 5Si3Ph1W
/+$
(A+pn6y+ cnonc+n ,#A_ 6ye+ paccmo+pen n rnane 30.)
B qanne Appliance.m cnn+esnpyn+e me+or oc+yna k nepemennrm .ksemnnpon:
)*6A#(& C!AA-*'+,/34C
*6A-/6/+&'&*#+ !AA-*'+,/
QqJ3R1Q41 HISwa23PT1` 5Si3Ph1W
Fnana 29. init ______________________________________________________________________________________
201
Pnc. 29.1. Knacc Appliance n ero cy6knacc OwnedAppliance
3ksemnnpr !AA-*'+,/ cosam+c cneymmnm o6pasom:
!AA-*'+,/ :' k <<!AA-*'+,/ '--#,= *+*&=H
Tak kak !AA-*'+,/ ne peannsye+ me+o *+*&, 6ye+ nrnonnen me+o *+*&,
onpeenennrn n P%M/,&. Kora .+o nponsone+, nce nepemennre .ksemnnpon,
cnennqnneckne n !AA-*'+,/, 6yy+ o6nynenr. Coo+ne+c+nenno nepemenna
A(#$.,&P'6/ nonoro .ksemnnpa Appliance 6ye+ panna +*-, a nepemenna ;#-&'8/ 6ye+
panna nynm.
Hpocvenmnn uevo( init
B neko+oprx cnynax nnnnnannsann nepemennrx .ksemnnpon nynmn moe+ pa6o+a+r
xopomo. B pyrnx cnynax .ksemnnpr knacca onnr nannna+r cnoe cymec+nonanne c
nepemennrmn .ksemnnpon, nnnnnannsnponannrmn ne nynenrmn snanennmn.
onyc+nm, n kaom .ksemnnpe !AA-*'+,/ nepemenna ;#-&'8/ onna
nnnnnannsnpona+rc snanennem 120. B qanne Appliance.m o6anr+e cnom peannsannm
*+*&, nepeonpeen +em camrm me+o *+*& knacca P%M/,&.
6 L4wV4J43 z
FF >%_;> A<(;l) 4J43 *Z)BB) x./0123
Q1iK j QaH1I 4J43W
FF @9&B>)&>)+&< +)\)Z]+;,; _+)\<+&< 5Si3Ph1^
5Si3Ph1 j !CbW
FF >;_>9)<+&< ?*)_)(<ZE +) +;>%f ;:e<*( S/0123
I13aIJ Q1iKW
|
Tenepr npn cosannn nonoro .ksemnnpa !AA-*'+,/ nepemenna ;#-&'8/ no
ymonnannm nnnnnannsnpye+c snanennem 120. (Yn+n+e, n+o .+o nnkak ne nnne+ na
Fnana 29. init ______________________________________________________________________________________
202
pa6o+y me+oon oc+yna. Hocne +oro kak .ksemnnp 6ye+ nnnnnannsnponan,
nepemenna moe+ 6r+r nsmenena o6rnnrm o6pasom, +o ec+r c ncnonrsonannem
2/&#-&'8/:.)
O6pa+n+e nnnmanne na nrson me+oa *+*& cynepknacca; on nnnnnannsnpye+
nepemennre .ksemnnpa, o6rnnenre n cynepknacce, n nosnpamae+ ykasa+enr na
nnnnnannsnponannrn o6rek+. B 6onrmnnc+ne cnynaen +aka cxema pa6o+ae+
6esynpenno. Tem ne menee neko+opre knaccr coepa+ anomanrnre me+or *+*&.
Anomannn en+c na ne pasnonnnoc+n:

Me+o *+*& nrnonne+ kakym-+o xn+pym on+nmnsannm, ynnn+oae+ ncxonrn


o6rek+, nrene+ nam+r n pyroro o6rek+a n nosnpamae+ nonrn o6rek+.

Honr+ka nrnonnenn *+*& sanepmae+c neyanen, me+o ynnn+oae+ o6rek+ n


nosnpamae+ +*-.
B nepnom cnynae qnpma Apple +pe6ye+, n+o6r 2/-B 6rn npncnoen ykasa+enr na
o6rek+, nosnpamaemrn me+oom *+*& cynepknacca. Mr enaem .+o n nepnon c+poke
cnoero me+oa *+*&.
Bo n+opom cnynae Apple pekomenye+ nponepn+r, n+o nnnnnannsa+op cynepknacca
nosnpamae+ enc+nn+enrnrn o6rek+, a ne +*-. H .+o non+no - 6eccmrcnenno
nrnonn+r nonrsona+enrckym nac+ponky necymec+nymmero o6rek+a. Hsmenn+e me+o
init n coo+ne+c+nnn c pekomenannmn Apple:
U 7*$>*+*& ?
@@ qrgq jngp *+*& otff P%M/,&
2/-B k <2.A/( *+*&=H
@@ jngp qj{uxt ujuwj9 gntwugj gn +*-~
4K LQ1iKV z
@@ s{wfqwquwj utug|g ujuwv ;#-&'8/
;#-&'8/ k XVIH
|
(/&.(+ 2/-BH
J
O+kponenno ronop, +akne nponepkn neo6xonmr +onrko n onenr cnennqnnecknx
cn+yannx, n na npak+nke mnorne nporpammnc+r Objective-C nponyckam+ nx. Tem ne
menee n knnre mr ncera 6yem ncnonrsona+r +akne nponepkn, no+omy n+o qnpma Apple
pekomenye+ nmenno +ak noxon+r k peannsannn me+oon *+*&.
Hcnonsonanne uevo(on (ocvyna
Fnana 29. init ______________________________________________________________________________________
203
Ham me+o *+*& knacca {{-*+fj o+nnnno pa6o+ae+, no xony nokasa+r pyrym
pasnonnnoc+r, ko+opa nac+o nc+penae+c n koe pyrnx nmen. O6rnno nrnonnm
npoc+oe npncnannanne n me+oe *+*& no mnorne nporpammnc+r ncnonrsym+ me+o
oc+yna. Bnecn+e neko+opre nsmenenn n me+o *+*&:
U 7*$>*+*& ?
@@ qrgq jngp *+*& otff P%M/,&
2/-B k <2.A/( *+*&=H
@@ jngp qj{uxt ujuwj9 gntwugj gn +*-~
*B 72/-B> ?
@@ s{wfqwquwj utug|g ujuwv ;#-&'8/
<2/-B 2/&#-&'8/\XVI=H
J
(/&.(+ 2/-BH
J
B 6onrmnnc+ne cnynaen ne+ oco6rx npnnnn npenonn+a+r ono pemenne pyromy,
no sa+o ec+r xopoma +ema n cnopa. C+oponnnk npncnannann ronopn+: Me+or
oc+yna ne onnr ncnonrsona+rc n *+*&! Me+o oc+yna nopasymenae+, n+o o6rek+
ro+on k ncnonrsonannm, a coc+onne-ro+onnoc+n nonne+c +onrko nocne sanepmenn
*+*&. Ha .+o c+oponnnk me+oon oc+yna o+nenae+: a nano. B peanrnom mnpe .+o
non+n nnkora ne cosae+ npo6nem. B ncnonrsym cnon me+o oc+yna noncmy, re
saam snanenne .+on nepemennon. Ha camom ene n noannmmem 6onrmnnc+ne cnynaen
mono ncnonrsona+r nm6on ns nyx cnoco6on.
init c apryuenvaun
Hnora nopmanrna nnnnnannsann o6rek+a +pe6ye+ nepeann ononnn+enrnon
nnqopmannn o+ me+oa, ko+oprn ero nrsrnae+. Hpec+anr+e, n+o .ksemnnp !AA-*'+,/
ne moe+ nopmanrno qynknnonnpona+r 6es nasnann (+*- ne n cne+). B +akom cnynae
neo6xonmo kak-+o nepea+r nnnnnannsa+opy nasnanne, sakpennemoe sa yc+ponc+nom.
Cena+r .+o n *+*& nenrs, no+omy n+o *+*& ne nonynae+ aprymen+on. Bnann+,
neo6xonmo cosa+r nonrn nnnnnannsa+op. Tora cosanne .ksemnnpa !AA-*'+,/ n
pyrom me+oe 6ye+ nrrne+r +ak:
!AA-*'+,/ :' k <<!AA-*'+,/ '--#,= *+*&E*&4b(#$.,&P'6/\CZ#'2&/(C=H
Honrn nnnnnannsa+op knacca !AA-*'+,/ - *+*&E*&4b(#$.,&P'6/: - nonynae+
aprymen+ P%%&(*+8. O6rnn+e nonrn me+o n Appliance.h:
Fnana 29. init ______________________________________________________________________________________
204
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ !AAX*'+,/ \ P%M/,& ?
P%%&(*+8 :{(g$.f&P6jH
*+& ;#-&'8/H
J
A(#A/(&_ 7fg{x> P%%&(*+8 :A(#$.,&P'6/H
A(#A/(&_ *+& ;#-&'8/H
6 L4wV4J4343R{ISwa23PT1Lxx3I4Jh XVHJW
/+$
Hann+e n Appliance.m peannsannm init. Hepenmenyn+e me+o n
*+*&E*&4b(#$.,&P'6/: n A(#$.,&P'6/, ncnonrsy nepeannoe snanenne.
6 L4wV4J4343R{ISwa23PT1Lxx3I4Jh XV9J
?
@@ rgq jngp *+*& otff P%M/,&
2/XB k <2.A/( *+*&=H
@@ jngp qj{uxt ujuwj9 gntwugj gn +*-~
*B 72/-B> ?
@@ uwwtwwv uquwv s{gpxon
Q1iK Q13{ISwa23PT1HJW
@@ z{w,qwquwj utug|g ujuwv ;#-&'8/
<2/-B 2/&#-&'8/\XVI=H
J
(/&.(+ 2/-BH
J
Hpee nem npoona+r, noc+pon+e npoek+ n y6en+ecr n npannnrnoc+n
cnn+akcnca.
Tenepr nr moe+e cosa+r .ksemnnp !AA-*'+,/ c saannrm nasnannem. Ho ecnn
nepea+r qannr H&&li)n+$.h n Appliance.m pyromy nporpammnc+y, on moe+ n ne
non+r, n+o n koe cneye+ nrsna+r *+*&L*&4b(#$.f&P6j:. H+o, ecnn on cosac+
.ksemnnp !AA-*'+,/ c+anap+nrm cnoco6om?
{{-*+fj :' k <<{{-*+fj '--#,= *+*&=H
Takoe enc+nne ne nasonemr nepasymnrm. Onae+c, n+o .ksemnnp !AA-*'+,/
- cy6knacca P%M/,& - cnoco6en ena+r nce, na n+o cnoco6en .ksemnnp P%M/,&. A
.ksemnnpr P%M/,& pearnpym+ na coo6menn *+*&.
Onako n annom cnynae .+o cosae+ npo6nemr, no+omy n+o npnneenna c+poka
koa cosae+ .ksemnnp Appliance c nasnannem npoyk+a +*- n nynenrm snanennem
;#-&'8/. A panee mr oronopnnncr, n+o karn .ksemnnp !AA-*,'&*#+ nopmanrno
pa6o+ae+ +onrko npn snanennn ;#-&'8/, pannom 120, n nasnannn, o+nnnnom o+ +*-. Kak
npeo+npa+n+r noo6nym nenpn+noc+r?
Hpo6nema pemae+c npoc+o: n qanne Appliance.m o6anr+e me+o *+*&,
nrsrnammnn *+*&E*&4b(#$.,&P'6/: c nasnannem no ymonnannm.
Fnana 29. init ______________________________________________________________________________________
205
6 L4wV4J43 z
I13aIJ Q1iK 4J4343R{ISwa23PT1MJpJSOJMW
|
Hona nepeonpeenenna nepcn init ncero nnmr nrsrnae+ me+o
*+*&E*&4b(#$.,&P'6/:, ko+oprn nrnonne+ ncm +enym pa6o+y.
n +ec+nponann nyx nnnnnannsa+opon nam nonao6n+c me+o $/2,(*A&*#+.
Bknmnn+e ero peannsannm n Appliance.m:
6 Lxx3I4Jh XVw1Q2I4H34SJ
z
I13aIJ xx3I4Jh Q3I4Jh43RSITP3Mr rw 5Si3QM` HISwa23PT1` 5Si3Ph1W
|
Hemnoro no .kcnepnmen+npyem c knaccom n main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
4THSI3 MHHi4PJ21"RM
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
HHi4PJ21 XP j HHi4PJ21 PiiS2 4J43W
xShLMP 4Q rM` PVW
P Q13{ISwa23PT1MPQR4Jh }P2R4J1MW
P Q13Si3Ph1CkbW
xShLMP 4Q rM` PVW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy
Hpoonnm name nsynenne nnnnnannsa+opon. Cosan+e nonrn qann - cy6knacc
!AA-*'+,/ c nmenem L+/$!AA-*'+,/.
Pnc. 29.2. Cosanne cy6knacca Appliance
Fnana 29. init ______________________________________________________________________________________
206

Bknmnn+e n qann OwnedAppliance.h nsmenemrn na6op nmen nnaenrnen n +pn
me+oa.
)*6A#(& C!AA-*'+,/34C
*+&/(B',/ L+/$!AA-*'+,/ \ !AA-*'+,/ z x}a3P/i1x13 XSOJ1IPT1QW
|
6 L4wV4J4343R{ISwa23PT1Lxx3I4Jh XVHJ
K4IQ3.OJ1IPT1Lxx3I4Jh XVJW
6 L5S4wVPww.OJ1IPT1Q./0123Lxx3I4Jh XVJW
6 L5S4wVI1TS51.OJ1IPT1Q./0123Lxx3I4Jh XVJW
/+$
Onn ns o6rnnenrx me+oon - nnnnnannsann, nonynammnn na aprymen+a.
Peannsyn+e me+or n qanne OwnedAppliance.m:
)*6A#(& CL+/$!AA-*'+,/34C
*6A-/6/+&'&*#+ L+/$!AA-*'+,/
6 L4wV4J4343R{ISwa23PT1Lxx3I4Jh XVHJ
K4IQ3.OJ1IPT1Lxx3I4Jh XVJ
z
FF >%_;> &+n&)Z&_)(;9) B?@<9*Z)BB)
Q1iK j QaH1I 4J4343R{ISwa23PT1HJW

4K LQ1iKV z
FF B;_l)+&< A+;=<B(>) lZE [9)+<+&E &A<+ >Z)l<Z]n<>
SOJ1IPT1Q j x}a3P/i1x13 PiiS2 4J43W
FF &AE @<9>,; >Z)l<Z]n) ;(Z&\+;,; ;( J4i^
4K LJV z
SOJ1IPT1Q Pww./0123JW
|
|
FF >;_>9)<+&< ?*)_)(<ZE +) +;>%f ;:e<*(
I13aIJ Q1iKW
|
6 L5S4wVPww.OJ1IPT1Q./0123Lxx3I4Jh XVJ
z
SOJ1IPT1Q Pww./0123JW
|
6 L5S4wVI1TS51.OJ1IPT1Q./0123Lxx3I4Jh XVJ
z
SOJ1IPT1Q I1TS51./0123JW
|
/+$
O6pa+n+e nnnmanne: knacc ne nnnnnannsnpye+ ;#X&'8/ nnn A(#$.,&P'6/. 3+nm
sannmae+c me+o *+*&E*&4b(#$.,&P'6/: n knacce !AA-*'+,/. Kora nr cosae+e
cy6knacc, o6rnno neo6xonmo nnnnnannsnpona+r +onrko +e nepemennre .ksemnnpon,
ko+opre n nero o6annnn nr; a o nepemennrx .ksemnnpon, ko+opre o6annn
cynepknacc, nyc+r nosa6o+n+c on cam.
Fnana 29. init ______________________________________________________________________________________
207
Onako +enepr mr c+anknnaemc c cn+yannen, ye snakomon nam no !AA-*'+,/ n
nnnnnannsa+opy ero cynepknacca *+*&. B kakon-+o momen+ onn ns namnx konner moe+
cosa+r yacnym omn6ky, nanncan cneymmym c+poky koa:
L+/$!AA-*'+,/ :' k <<L+/$!AA-*'+,/ '--#,= *+*&E*&4b(#$.,&P'6/\CZ#'2&/(C=H
3+o+ ko npnnee+ k nrnonnennm me+oa *+*&E*&4b(#$.,&P'6/: n knacce
!AA-*'+,/. Me+o nnnero ne snae+ o mnoec+ne #L+/(P'6/2, a cneona+enrno,
nepemenna #L+/(P'6/2 6ye+ nekoppek+no nnnnnannsnponana n annoro .ksemnnpa
OwnedAppliance.
Pemenne npo6nemr +oe oc+ae+c npennm. Bknmnn+e n qann
OwnedAppliance.m peannsannm nnnnnannsa+opa cynepknacca *+*&E*&4b(#$.,&P'6/:,
ko+opa nrsrnae+ *+*&E*&4b(#$.,&P'6/\B*(2&L+/(P'6/: n nepeae+ snanenne no
ymonnannm n B*(2&L+/(P'6/.
6 L4wV4J4343R{ISwa23PT1Lxx3I4Jh XVHJ
z
I13aIJ Q1iK 4J4343R{ISwa23PT1HJ K4IQ3.OJ1IPT1J4iW
|
Bonpoc na sacrnky: nyno nn +ake peannsona+r *+*& n L+/$!AA-*'+,/? He+.
Ha .+on c+ann cneymmnn ko 6ye+ nopmanrno pa6o+a+r:
L+/$!AA-*'+,/ :' k <<L+/$!AA-*'+,/ '--#,= *+*&=H
Honemy? Ho+omy n+o npn o+cy+c+nnn peannsannn *+*& n L+/$!AA-*'+,/ .+a
c+poka npnnee+ k cpa6a+rnannm peannsannn init ns !AA-*'+,/, ko+opa nrsrnae+
<2/-B *+*&E*&4b(#$.,&P'6/\CQ+h+#L+C=.2/-B - .ksemnnp L+/$!AA-*'+,/,
no.+omy on nrsrnae+ nepcnm *+*&E*&4b(#$.,&P'6/: knacca L+/$!AA-*'+,/, ko+opa
nrsrnae+
<2/-B *+*&E*&4b(#$.,&P'6/\ A+ B*(2&L+/(P'6/\+*-=3
B pesynr+a+e o6pasye+c nenonka nnnnnannsa+opon, nrsrnammnx pyrne
nnnnnannsa+opr.
Fnana 29. init ______________________________________________________________________________________
208
Pnc 29.3. Henonka nnnnnannsa+opon
Ha pnc. 29.3 onn nnnnnannsa+op n kaoro knacca nrenen +emnrm qonom.
On nne+c '+"'("<3 !"!7!)1!-)#'&'3 n annoro knacca. Tak, init nne+c
ocnonnrm nnnnnannsa+opom n knacca P%M/,&, *+*&E*&4b(#$.,&P'6/: - n knacca
!AA-*'+,/, a *+*&E*&4b(#$.,&P'6/\B*(2&L+/(P'6/: - n knacca L+/$!AA-*'+,/.
Knacc coepn+ +onrko onn ocnonnon nnnnnannsa+op. Ecnn knacc coepn+ pyrne
nnnnnannsa+opr, +o nx peannsann onna nrsrna+r (npmo nnn kocnenno) ocnonnon
nnnnnannsa+op.
Hpn cosannn knacca, y ko+oporo nm ocnonnoro nnnnnannsa+opa o+nnnae+c o+
nmenn ocnonnoro nnnnnannsa+opa cynepknacca (kak y !AA-*'+,/ c L+/$!AA-*'+,/),
neo6xonmo okymen+npona+r .+o o6c+o+enrc+no n saronononnom qanne. Bknmnn+e
coo+ne+c+nymmnn kommen+apnn n Appliance.h:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ !AA-*'+,/ \ P%M/,& ?
P%%&(*+8 :A(#$.,&P'6/H
*+& ;#-&'8/H
J
A(#A/(&_ 7,#A_> P%%&(*+8 :A(#$.,&P'6/H
A(#A/(&_ *+& ;#-&'8/H
FF ;B+;>+;f &+n&)Z&_)(;9
U 7*$>*+*&E*&4b(#$.,&P'6/\7P%%&(*+8 :>A+H /+$
'+$ *+ L+/$!AA-*'+,/34\
)*6A#(& C!AA-*'+,/34C
*+&/(B',/ L+/$!AA-*'+,/ \ !AA-*'+,/ ?
P%.&'M-/%/& :#L+/(P'6/2H
J
FF ;B+;>+;f &+n&)Z&_)(;9
U 7*$>*+*&E*&4b(#$.,&P'6/\7P%%&(*+8 :>A+
B*(2&L+/(P'6/\7P%%&(*+8 :>+H
U 7;#*$>'$$L+/(P'6/2M/,&\7P%%&(*+8 :>+H
U 7;#*$>(/6#;/L+/(P'6/2M/,&\7P%%&(*+8 :>+H
/+$
H+ak, mr noxonm k npannnam, ko+opre onnr co6nma+rc karm
npnnnnnrm nporpammnc+om Objective-C npn nanncannn nnnnnannsa+opon:
Fnana 29. init ______________________________________________________________________________________
209

Ecnn knacc nmee+ neckonrko nnnnnannsa+opon, +onrko onn ns nnx onen nrnonn+r
peanrnym pa6o+y. 3+o+ me+o nasrnae+c '+"'("<3 !"7!7!)1u-)#'&'3. Bce
oc+anrnre nnnnnannsa+opr onnr nrsrna+r ocnonnon nnnnnannsa+op (npmo nnn
kocnenno).

Ocnonnon nnnnnannsa+op nrsrnae+ ocnonnon nnnnnannsa+op cynepknacca nepe


nnnnnannsannen cnonx nepemennrx .ksemnnpon.

Ecnn nm ocnonnoro nnnnnannsa+opa namero knacca o+nnnae+c o+ nmenn ocnonnoro


nnnnnannsa+opa ero cynep knacca, nr onnr nepeonpeenn+r ocnonnon
nnnnnannsa+op cynepknacca, n+o6r on nrsrnan nonrn ocnonnon nnnnnannsa+op.

Ecnn knacc coepn+ neckonrko nnnnnannsa+opon, ne+ko ykan+e n saronononnom


qanne, kakon ns nnx nne+c ocnonnrm.
+avannmn nmson init
Bpem o+ npemenn nc+penam+c cn+yannn, n ko+oprx 6esonacnoe nepeonpeenenne
ocnonnoro nnnnnannsa+opa cynepknacca okasrnae+c nenosmonrm. onyc+nm, mr
cosaem cy6knacc P%M/,& c nmenem E'--%'B/, a ero ocnonnrm nnnnnannsa+opom
nne+c *+*&E*&4%/,(/&^#$/:. Onako nnnnnannsann 2/,(/&^#$/ snanennem no
ymonnannm ne o6ecnennnae+ neo6xonmoro yponn 6esonacnoc+n n namem npnnoennn.
A .+o osnanae+, n+o ncnonrsyema cxema - nepeonpeenenne init n nrsona ocnonnoro
nnnnnannsa+opa nonoro knacca co snanennmn no ymonnannm - okasrnae+c
nenpnemnemon.
H+o e ena+r? 3ksemnnp E'--%'B/ no-npenemy pearnpye+ na coo6menne init. H
k+o-nn6yr moe+ nerko ncnonrsona+r konc+pyknnm nna:
E'--%'B/ :L2 k <<E'--%'B/ '--#,= *+*&=H
hynmee, n+o nosmono n +akon cn+yannn - +akoe nepeonpeenenne ocnonnoro
nnnnnannsa+opa cynep knacca, ko+opoe coo6mn+ paspa6o+nnky o conepmennon omn6ke n
nockae+ emy, kak .+y omn6ky ncnpann+r:
U 7*$>*+*& ?
&4(#L <P%d,/A&*#+ /d,/A&*#+E*&4P'6/\CE'--%'B/K+*&*'-*'&*#+C
(/'2#+\CQ2/ *+*&E*&4%/,(/&^#$/\9 +#& *+*&C
.2/(K+B#\+*-=H
J
Fnana 29. init ______________________________________________________________________________________
210
cb" ->;fB(>)
B nperymen rnane mr cosann knacc !AA-*'+,/, ko+oprn coepan na cnonc+na:
A(#$.,&P'6/ n ;g-&'8/. anan+e pas6epemc, kak pa6o+am+ .+n cnonc+na.
B qanne Appliance.h o6rnnm+c ne nepemennre .ksemnnpon n xpanenn
annrx:
?
P%%&(*+8 :A(#$.,&P'6/H
*+& ;#-&'8/H
J
Take n nnx 6rnn o6rnnenr me+or oc+yna. O6rnnenn mornn 6r
nrrne+r +ak:
U 7;#*$>2/&b(#$.,&P'6/\7P%%&(*+8 :>2H
U 7P%%&(*+8 :>A(#$.,&P'6/H
U 7;#*$>2/&#-&'8/\7*+&>dH
U 7*+&>;#-&'8/H
Ho mr nmec+o .+oro nocnonrsonanncr konc+pyknnen {|g{j|&x:
A(#A/(&_ 7,#A_> P%%&(*+8 :A(#$.,&P'6/H
A(#A/(&_ *+& ;#-&'8/H
B qanne Appliance.m me+or oc+yna 3'2"' 6rno peannsona+r nno:
U 7;#*$>2/&b(#$.,&P'6/\7P%%&(*+8 :>2
?
A(#$.,&P'6/ k <2 ,#A_=H
J
U 7P%%&(*+8 :>A(#$.,&P'6/
?
(/&.(+ A(#$.,&P'6/H
J
U 7;#*$>2/&#-&'8/\7*+&>d
?
;#-&'8/ k dH J
U 7*+&>;#-&'8/
?
(/&.(+ ;#-&'8/H
J
Onako mr ncnonrsonann n nx peannsannn konc+pyknnm 2_+&4/2*/:
2_+&4/2*/ A(#$.,&P'6/9 ;#-&'8/H
Fnana 30. Cnoc1na ______________________________________________________________________________________
211
hm6onr+nrn qak+ o komnnn+ope Objective-C: npn komnnnnponannn
npnnoenn n iOS nnn 64-paspnon nna+qopmr Mac OS X o6rnn+r nepemennre
.ksemnnpon ne nyno. Brsonon {(g{j(&x@2x+&4j2*j oc+a+onno n +oro, n+o6r
nam+r n annrx 6rna sapesepnnponana.
Bakommen+npyn+e nepemennre .ksemnnpon n qanne Appliance.h: {
?
FF P%%&(*+8 :A(#$.,&P'6/H
FF *+& ;#-&'8/H
J
Banono noc+pon+e n sanyc+n+e nporpammy.
B .+on knnre mr ncera o6rnnem nepemennre .ksemnnpon. O6rnnenne moe+
paccma+pnna+rc kak ononnn+enrna qopma okymen+nponanna n nosnone+
ncnonrsona+r ko n 32-paspnrx nporpammax n Mac OS X. Packommen+npyn+e
nepemennre .ksemnnpon.
Avpn6yvm cnoncvn
A +enepr no6nne nosnakomnmc c pasnnnnrmn a+pn6y+amn, ynpannmmnmn cosannem
me+oon oc+yna n cnonc+n.
Hsuenneuocv
Cnonc+no mono o6rnn+r oc+ynnrm n n+enn/sanncn ((/'$L(*&/) nnn +onrko n
n+enn ((/'$#+-_). Ho ymonnannm ncnonrsye+c snanenne (/'$L(*&/, npn ko+opom
cosae+c kak set-, +ak n 8j&-me+o. Ecnn nr ne xo+n+e, n+o6r n cnonc+na cosananc
2j&-me+o, nome+r+e cnonc+no a+pn6y+om (/'$#+-_:
A(#A/(&_ 7(/'$#+-_> *+& ;#-&'8/H
Cpok wnsnn
Cnonc+no +ake moe+ 6r+r o6rnneno c a+pn6y+om .+2Bje.+(j&*+j$, 2&(#+8,
L/'h nnn copy. 3+o snanenne onpeene+ +em, kak 2j&-me+o oprannsye+ ynpannenne
nam+rm,
Bnanenne .+2Bje.+(j&*+j$ ncnonrsye+c no ymonnannm; .+o camrn npoc+on
napnan+ - cnonc+ny npoc+o npncnannae+c nepeannoe snanenne. n npnmepa nosrmem
cneymmee o6rnnenne n onpeenenne:
A(#A/(&_ 7.+2'B/e.+(/&'*+/$> *+& ';/('8/%,#(/H
Fnana 30. Cnoc1na ______________________________________________________________________________________
212
@@ CA(#A/(&_ *+& ';/('8/%,#(/C L#.-$ '-2# L#(h 4/(/
333
2_+&4/2*/ ';/('8/%,#(/H
Crenepnponannrn 2jn-me+o 6ye+ npak+nneckn .knnnanen+en cneymmemy:
U 7;#*$>2/&!;/('8/%,#(/\7*+&>$
?
';/('8/%,#(/ k $H
J
B Appliance cnonc+no ;#-&'8/ ne nsmene+ cne+nnk ccrnok. A+pn6y+
.+2'B/e.+(/&'*+/$ ncera ncnonrsye+c n cnonc+n, coepamnx ne-o6rek+nre
snanenn.
A+pn6y+ 2&(#+8, kak ynomnnanocr n rnane 20, o6ecnennnae+ coxpanenne cnnrnon
ccrnkn na nepeannrn o6rek+. On +ake ymenrmae+ cne+nnk ccrnok c+aporo o6rek+a
(ko+oprn ynnn+on+ ce6 npn o+cy+c+nnn pyrnx nnaenrnen). n cnonc+n,
coepamnx o6rek+r, o6rnno ncnonrsye+c a+pn6y+ 2&(#+8.
A+pn6y+ L/'h ne nopasymenae+ nnaenn o6rek+om. On cnn+esnpye+ 2/&-me+o,
ko+oprn npncnannae+ cnonc+ny nepeannrn o6rek+. B cnynae ynnn+oenn o6rek+a
cnonc+ny saae+c snanenne +*-. (O6pa+n+e nnnmanne: ecnn ykasa+enr nmee+ a+pn6y+
.+2Bje.+(j&*+j$, +o npn ynnn+oennn o6rek+a, na ko+oprn on ccrnae+c, n
nporpamme nonne+c nncnnn ykasa+enr. O+npanka coo6menn +akomy ykasa+enm
o6rnno npnnon+ k c6om nporpammr.)
A+pn6y+ copy cosae+ cnnrnym ccrnky na konnm nepeannoro o6rek+a. Ho secr
nmee+c ona +onkoc+r, ko+opym mnorne paspa6o+nnkn nonnmam+ nenpannnrno...
copy
A+pn6y+ copy cosae+ konnm o6rek+a n nepe non+ na nee ykasa+enr. onyc+nm,
o6rnnenne n onpeenenne cnonc+na nrrn+ cneymmnm o6pasom:
A(#A/(&_ 7,#A_> P%%&(*+8 :-'2&P'6/H
2_+&4/2*/ -'2&P'6/H
Crenepnponannrn set-me+o nrrnn+ npnmepno +ak:
U 7;#*$>2/&c'2&P'6/\7P%%&(*+8 :>$
?
-'2&P'6/ k <$ ,#A_=H
J
A+pn6y+ copy name ncero ncnonrsye+c c o6rek+nrmn +nnamn, nmemmnmn
nsmenemre cy6knaccr. Hanpnmep, P%%&(*+8 nmee+ cy6knacc c nmenem
Fnana 30. Cnoc1na ______________________________________________________________________________________
213
P%.&M-j%&(*+8. Kak ne+pyno npec+ann+r, me+oy 2/&c'2&P'6/: moe+
nepeana+rc nsmenema c+poka:
@@ fgpuwj wjuvjg fn{gow
P%.&'M-/%&(*+8 :d k <<P%.&'M-/%&(*+8 '--#,= *+*&E*&4%&(*+8\C+#C=H
@@ sj{jp jj 2/&c'2&P'6/\
<6_M 2/&c'2&P'6/\d=H
@@ ,#A_ s{jjn &4*2 wjuvn -'2&P'6/
<d 'AA/+$%&(*+8\C c/++#+C=H
A ecnn nepeanaemrn o6rek+ ne nne+c nsmenemrm? Cosana+r konnm
nensmenemoro o6rek+a 6rno 6r ne.qqek+nnno. Me+o copy npoc+o nrsrnae+
,#A_E*&4#+/: n nepeae+ aprymen+ +*-. Hanpnmep, n P%%&(*+8 me+o fg{xE*&4#+/:
nepeonpeen+c cneymmnm o6pasom:
U 7*$>,#A_E*&4#+/\7P%#+/ :>
?
(/&.(+ 2/-BH
J
Taknm o6pasom, konn noo6me ne cosae+c. (Yn+n+e, n+o P%#+/ n sonnponanne
nam+n noo6me - yc+apenmne, pynmen+apnre nosmonoc+n nporpammnponann Cocoa,
no.+omy secr onn nopo6no ne paccma+pnnam+c. Bnponem, me+o ,#A_E*&4#+/: eme
naxon+ npak+nneckoe npnmenenne n ne cnn+ae+c nonnoc+rm nr+ecnennrm.)
n o6rek+on, cymec+nymmnx n nsmenemon n nensmenemon nepcnx, me+o
copy nosnpamae+ nensmenemym konnm. Hanpnmep, P%.&M-j%&|*+8 coepn+ me+o
copy, ko+oprn nosnpamae+ .ksemnnp P%%&(*+8. Ecnn nr xo+n+e, n+o6r konn 6rna
nsmenemrm o6rek+om, ncnonrsyn+e me+o 6.&M-jg{x.
A+pn6y+a cpoka nsnn cnonc+n c nmenem 6.&M-jg{x ne cymec+nye+. Ecnn 6r
xo+n+e, n+o6r 2j&-me+o nasnanan cnonc+ny nsmenemym konnm o6rek+a, nr onnr
peannsona+r ero camoc+o+enrno, c nrsonom me+oa 6.&M-jg{x n nxonoro
o6rek+a. Hanpnmep, n knacce L+/$!AA-*'+,/ mono cosa+r me+o 2/&L+/(P'6/2: :
U 7;#*$>2/&L+/(P'6/2\7P%%/& :>+/LP'6/2
?
#L+/(P'6/2 k <+/LP'6/2 6.&'M-/^#A_=H
J
Ho(po6nee o konnponannn
Bonrmnnc+no knaccon Objective-C noo6me ne coepn+ me+oa ,#A_E*&4#+/:.
Hporpammnc+r Objective-C cosam+ menrme konnn, nem kae+c na nepnrn nsrn.
Hn+epecno, n+o me+or copy n 6.&M-jg{x onpeenm+c n P%M/,& cneymmnm
o6pasom:
Fnana 30. Cnoc1na ______________________________________________________________________________________
214
U 7*$>,#A_ ?
(/&.(+ <2/-B ,#A_E*&4#+/\PQcc=H
J
U 7*$>6.&'M-/^#A_
?
(/&.(+ <2/-B 6.&'M-/^#A_E*&4#+/\PQcc=H
J
Cneona+enrno, npn nonr+ke nrnonnenn koa
!AA-*'+,/ :M k <<!AA-*'+,/ '--#,= *+*&=H
!AA-*'+,/ :, k <M ,#A_=H
nr nonynn+e omn6ky cneymmero nna:
U<!AA-*'+,/ ,#A_E*&4#+/\=\ .+(/,#8+*/$ 2/-/,&#( 2/+& &# *+2&'+,/ IdXIIXXIXSI
Avouapnocv
3+a knnra nananrnoro yponn, a a+pn6y+ '&#6*,@+#+'&#6*, o+nocn+c k o+nocn+enrno
ne+pnnnanrnon +eme mnorono+onnoc+n. Cennac nam neo6xonmo sna+r cneymmee: n
penme +#+'&#6*, nam 2j&-me+o 6ye+ nrnonn+rc ny+r 6rc+pee. Barnnyn n
sarononkn UlKit, nr ynnn+e, n+o nce cnonc+na nomenenr a+pn6y+om +#+'&#6*,. Bam
+oe "C+on+ o6rnn+r cnon cnonc+na c a+pn6y+om +#+'&#6*,.
(B am .+o+ cone+ ncem 6es ncknmnenn. Onako n kaon rpynne naxon+c
ymnnk, k+o snae+ ponno c+onrko, n+o6r nospasn+r. On ronopn+: Ho ner n
mnorono+onnom npnnoennn mne no+pe6ye+c samn+a, ko+opym mne o6ecnena+
a+omapnre 2j&-me+or. H mne c+onno 6r ckasa+r: He ymam, n+o nr ckopo 6ye+e
nnca+r mnorono+onnrn ko. A kora nanne+e, a+omapnoc+r 2j&-me+oon nam np nn
nomoe+. Ho ronopm pyroe: hano, +ora oc+anr+e cnon 2j&-me+or a+omapnrmn.
Ho+omy n+o ne c+on+ ronopn+r +o, n+o nmn eme ne ro+onr ycnrma+r.)
B Appliance.h o6rnn+e cnon me+or oc+yna c a+pn6y+om +#+'&#6*,:
A(#A/(&_ 7,#A_` JSJP3ST42> P%%&(*+8 :A(#$.,&P'6/H
A(#A/(&_ LJSJP3ST42V *+& ;#-&'8/H
K coanennm, na momen+ nanncann knnrn snanenne '&#6*, ncnonrsye+c no
ymonnannm, no.+omy nam npne+c nnec+n .+o nsmenenne.
Fnana 30. Cnoc1na ______________________________________________________________________________________
215
3annc knu-snauenne
Banncr knmn-snanenne nosnone+ nn+a+r n saana+r cnonc+na no nmenn.
Coo+ne+c+nymmne me+or onpeenenr n P%M/,&, no.+omy karn o6rek+
noepnnae+ annym nosmonoc+r.
O+kpon+e qann main.m n nann+e c+poky:
<' 2/&b(#$.,&P'6/\CE'24*+8 ',4*+/C=H
Hepennmn+e ee c ncnonrsonannem sanncn knmn-snanenne:
P Q13Pia1MPQR4Jh }P2R4J1M KSI1qMHISwa23PT1MW
B .+om cnynae me+o 2/&'-./\B#(/_:, onpeenennrn n P%M/,&, 6ye+ ncka+r
2j&-me+o C nmenem 2/&b(#$.,&P'6/:. Ecnn y o6rek+a ne+ me+oa 2/&b(#$.,&P'6/:,
on o6pamae+c k nepemennon .ksemnnpa nanpmym.
Banncr knmn-snanenne +ake nosnone+ nponn+a+r snanenne nepemennon.
Bknmnn+e n main.m c+poky n nrnoa nmenn yc+ponc+na:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/ ?
!AA-*'+,/ :' k <<!AA-*'+,/ '--#,= *+*&=H
P%c#87C' *2 C9 '>H
<' 2/&'-./\CE'24*+8 ',4*+/C B#(/_\CA(#$.,&P'6/C=H
<' 2/&#-&'8/\V]I=H
P%c#87C' *2 C9 '>H
xShLM3R1 HISwa23 JPT1 4Q rM` P 5Pia1SI1qMHISwa23PT1MVW
J
(/&.(+ IH
J
Ha .+o+ pas me+o ;'-./`#(/_:, onpeenennrn n P%M/,&, nme+ me+o oc+yna
c nmenem A(#$.,&P'6/. Ecnn me+o A(#$.,&P'6/ ne nanen, on o6pamae+c k
nepemennon .ksemnnpa nanpmym.
Ecnn nm cnonc+na ykasano nenepno, nr ne nonynn+e npeynpeenn o+
komnnn+opa, no no npem nrnonnenn nponsone+ omn6ka. Cenan+e omn6ky n main.m:
P%c#87C&4/ A(#$.,& +'6/ *2 C9 <' ;'-./`#(/_\MHISwa23PTTTT1M=>H
Hoc+pon+e n sanyc+n+e nporpammy - nr nonynn+e cneymmee coo6menne o6
omn6ke:
::: Z/(6*+'&*+8 'AA $./ &# .+,'.84& /d,/A&*#+ P%Q+h+#L+/_d,/A&*#+9
(/'2#+\ <1!AA-*'+,/ IdXIIXIW$$I5 ;'-./`#(Q+$/B*+/$/_\=\
&4*2 ,-'22 *2 +#& h/_ ;'-./ ,#$*+8U,#6A-*'+& B#( &4/ h/_ A(#$.,&P'6666/3
Fnana 30. Cnoc1na ______________________________________________________________________________________
216
Hcnpanr+e omn6ky, npee nem npoona+r.
Honemy sanncr .knmn-snanenne~ npec+anne+ nn+epec? Karn pas, kora
c+anap+na 6n6nno+eka sanncrnae+ annre n namn o6rek+r, ona ncnonrsye+ 2/&'-./:
B#(/_:. Karn pas, kora c+anap+na 6n6nno+eka nn+ae+ annre ns namnx
o6rek+on, ona ncnonrsye+ ;'-./`#(/_:. Hanpnmep, 6n6nno+eka g(jO'&' ynpomae+
coxpanenne o6rek+on n 6ase annrx %c*&/ n nx nocneymmym sarpysky. n pa6o+r c
nonrsona+enrcknm o6rek+amn, coepamnmn annre, ncnonrsye+c sanncr knmn-
snanenne.
H+o6r y6en+rc n +om, n+o sanncr knmn-snanenne nrnonnn+ onepannn c
nepemennrmn ae npn o+cy+c+nnn me+oon oc+yna, sakommen+npyn+e o6rnnenne
{(g{j(&x n cnonc+na A(#$.,&P'6/ n qanne Appliance.h:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
*+&/(B',/ !AA-*'+,/ \ P%M/,& ?
P%%&(*+8 :A(#$.,&P'6/H
*+& ;#-&'8/H
J
FF A(#A/(&_ 7,#A_> P%%&(*+8 :A(#$.,&P'6/H A(#A/(&_ 7+#+'&#6*,> *+& ;#-&'8/H
@@ gfugqug wuwtwng{
U 7*$>*+*&E*&4b(#$.,&P'6/\7P%%&(*+8 :>A+H
/+$
Take yann+e nce cnynan ncnonrsonann me+oon 2/&b(#$.,&P'6/: n
{|g$.,&P'6/ ns Appliance.m:
*6A-/6/+&'&*#+ !AA-*'+,/
QqJ3R1Q41 5Si3Ph1W
U 7*$>*+*&E*&4b(#$.,&P'6/\7P%%&(*+8 :>A+
?
2/-B k <2.A/( *+*&=H
*B 72/-B> ?
HISwa23PT1 j HJ 2SHqW
<2/-B 2/&#-&'8/\XVI=H
J
(/&.(+ 2/-BH
J
U 7*$>*+*& ?
(/&.(+ <2/-B *+*&E*&4b(#$.,&P'6/\CQ+h+#L+C=H
J
U 7P%%&(*+8 :>$/2,(*A&*#+
?
(/&.(+ <P%%&(*+8 2&(*+8E*&4`#(6'&\C1\ $ ;#-&25C9 A(#$.,&P'6/9 ;#-&'8/=H
J
/+$
Hoc+pon+e n sanyc+n+e nporpammy. Hecmo+p na o+cy+c+nne me+oon oc+yna n
A(#$.,&P'6/, snanenne nepemennon nce panno moe+ nn+a+r n saana+r ns pyrnx
me+oon. 3+o nne+c onennnrm napymennem nen nnkancynnnn o6rek+a - me+or
o6rek+a o+kpr+r, no nepemennre .ksemnnpon onnr oc+ana+rc npnna+nrmn, +o
Fnana 30. Cnoc1na ______________________________________________________________________________________
217
ec+r neoc+ynnrmn nsnne. Ecnn 6r sanncr knmn-snanenne ne 6rna nenepo+no
nonesnon, nnk+o 6r c .+nm ne cmnpnnc.
He-o6+ekvnme vnnm
Me+or sanncn knmn-snanenne npenasnanenr n pa6o+r c o6rek+amn, no n
neko+oprx cnonc+nax xpan+c ne-o6rek+nre +nnr (nanpnmep, *+& nnn B-#'& - ckaem,
;#-&'8/ o+nocn+c k +nny *+&). Kak saa+r snanenne ;#-&'8/ n sanncn knmn-
snanenne? Hcnonrsyn+e P%P.6M/(.
B qanne main.m nsmenn+e c+poky, saammym snanenne ;#-&'8/:
< 2/&#-&'8/\V]I=H
cneymmen c+pokon:
P Q13Pia1xaT/1I JaT/1I43RNJ3Ckb KSI1qM5Si3Ph1MW
o6anr+e n Appliance.m me+o oc+yna n nrnoa nnqopmannn o nrsone:
U 7;#*$>2/&#-&'8/\7*+&>d
?
P%c#87C2/&&*+8 ;#-&'8/ &# $C9 d>H
;#-&'8/ k dH
J
Hoc+pon+e n sanyc+n+e nporpammy
Ananornnnrm o6pasom no sanpocy ;'-./`#(/_\C;#-&'8/C nr nonynn+e
o6rek+ P%P.6M/(, coepamnn snanenne ;#-&'8/.
Fnana 30. Cnoc1na ______________________________________________________________________________________
218
c!" 8)(<,;9&&
Ka+eropnn nosnonm+ nporpammnc+y o6ann+r nonre me+or n nm6on cymec+nymmnn
knacc. Hanpnmep, qnpma Apple npeoc+annna nam knacc P%%&(*+8. Hcxonrn ko .+oro
knacca neoc+ynen, no ka+eropnn am+ nosmonoc+r o6ann+r nonre me+or n
P%%&(*+8.
Cosan+e nonym nporpammy komannon c+pokn `#.+$'&*#+ c nmenem
#L/-^#.+&/(. Ba+em cosan+e nonrn qann, nnmmnnc ka+eropnen Objective-C
(Objective-C category). Hpncnon+e ka+eropnn nm #L/-^#.+&*+8 n nasnanr+e ee
ka+eropnen n P%%&(*+8.
O+kpon+e NSString+VowelCounting.h n o6rnn+e me+o, ko+oprn nr xo+n+e
o6ann+r n knacc P%%&(*+8:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345 *+&/(B',/ P%%&(*+8 7SO1i7SaJ34Jh>
6 L4J3V5SO1i7SaJ3W
/+$
Peannsyn+e me+o n NSString+ VowelCount.m:
)*6A#(& CP%%&(*+8l#L/-^#.+&*+834C *6A-/6/+&'&*#+ P%%&(*+8 7SO1i7SaJ34Jh>
6 L4J3V5SO1i7SaJ3
z
x7RPIP231Ix13 X2RPIx13 j
x7RPIP231Ix13
2RPIP231Ix1343R7RPIP231IQNJx3I4JhMP14SaqN.MW
xNJ31h1I 2SaJ3 j Q1iK i1Jh3RW
4J3 QaT j bW
KSI L4J3 4 j bW 4 2SaJ3W 4ttV z
aJ42RPI 2 j Q1iK 2RPIP231I3NJw14W
4K L2RPIx13 2RPIP231INQ}1T/1I2V z
QaTttW |
|
I13aIJ QaTW |
/+$
Tenepr ncnonrsyn+e nonrn me+o n main.m:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
4THSI3 Mxx3I4JhtSO1i7SaJ34Jh"RM
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/ ?
xx3I4Jh XQ3I4Jh j M1iiS` SIiwMW
xShLMr RPQ rw 5SO1iQM` Q3I4Jh` Q3I4Jh 5SO1i7SaJ3VW
J
(/&.(+ IH
J
Fnana 31. Ka1eropuu ______________________________________________________________________________________
219
Hoc+pon+e n sanyc+n+e nporpammy. Yo6no, ne npana nn? Ka+eropnn
npesnrnanno nonesnr.
Bano o+me+n+r, n+o ka+eropn npncy+c+nye+ +onrko n .+on nporpamme. Ecnn nr
xo+n+e, n+o6r me+o 6rn oc+ynen n pyron nporpamme. o6anr+e qann n npoek+ n
o+komnnnnpyn+e ka+eropnm npn noc+poennn nporpammr.
Fnana 31. Ka1eropuu ______________________________________________________________________________________
220
cC" Z;*&
B rnane 24 6rnn paccmo+penr mexannsmr o6pa+noro nrsona: enernponanne n
ononemenn. O6pa+nrn nrson nosnone+ pyrnm o6rek+am nrsrna+r me+or namnx
o6rek+on n o+ne+ na nosnnknonenne kaknx-nn6o co6r+nn. Xo+ .+n mexannsmr
a6conm+no pa6o+ocnoco6nr, onn npnno+ k qparmen+annn koa. Hac+n nporpammr.
ko+opre nam xo+enocr 6r epa+r pom pyr c pyrom n 6onrmen cnoc+n, o6rnno
okasrnam+c yanennrmn pyr o+ pyra.
Hanpnmep, n nporpamme ^'--M',h2 ns rnanr 24 mr o6annnn ko pernc+pannn
o6rek+a na ononemenn o6 nsmenennn naconoro noca nonrsona+en n ykasann, n+o npn
nonynennn +akoro ononemenn onen nrsrna+rc me+o #+/^4'+8/:. Ho kora
nn+am nam ko, mne nn+epecno, n+o enae+ me+o #+/^4'+8/: npn nrsone, no.+omy
o6pamamcr k peannsannn .+oro me+oa. B npnmepe ^'--M',h2 ko pernc+pannn o6rek+a
n ononemennn n peannsann me+oa naxo+c no cocec+ny, no ne+pyno npec+ann+r,
n+o n 6onrmem, 6onee cnonom npnnoennn .+n na 6noka 6yy+ yanenr pyr o+ pyra
na co+nn c+pok.
B Mac OS X 10.6 n iOS 4 nonnnacr nona nosmonoc+r - +ak nasrnaemre .1'/!
(M-gfo2). Bnok Objective-C npec+anne+ co6on qparmen+ koa (kak n qynknn C), no
on moe+ nepeana+rc n nporpamme no ananornn cannrmn. Bckope mr ynnnm, kak .+o
nomorae+ xpann+r nsanmocnsannrn ko no cocec+ny.
Bnokn n nx cnn+akcnc onpeenenno o+noc+c k nncny ne+pnnnanrnrx acnek+on
Objective-C, n na nepnrn nsrn onn kay+c ononrno sany+annrmn. Onako n API
qnpmr Apple 6nokn nc+penam+c nce name. B .+on rnane mr paccmo+pnm napy npoc+rx
npnmepon, n+o6r nr 6rnn ro+onr k nc+pene c nnmn.
Ecnn Y nac nmee+c onr+ nporpammnponann na pyrnx srkax, nosmono, nr
ye nc+penanncr c 6nokamn no nasnannmn )"'"!3"<D >6"/7!4, -)3</)"!4 nnn
1*3.,)->6"/7!4. Ecnn nr snakomr c ykasa+enmn na qynknnn, 6nokn mory+ nokasa+rc
nem-+o noxonm, no nckope nr y6en+ecr, n+o npn npannnrnom ncnonrsonannn 6nokon
ko nonynae+c 6onee .neran+nrm, nem npn ncnonrsonannn ykasa+enen na qynknnn.
Onpe(enenne 6nokon
Bnok nrrnn+ +ak:
?
P%c#87CK6 ' -#8 2&'&/6/+& L*&4*+ ' M-#,hFC>H
J
Hoxoe na qynknnn, no nm qynknnn sameneno cnmnonom . 3+o+ cnmnon
nen+nqnnnpye+ cneymmnn qparmen+ koa kak 6nok. Kpome +oro, 6nokn, kak n
qynknnn, mory+ nonyna+r aprymen+r:
Fnana 32. Bnoku ______________________________________________________________________________________
221
7$#.M-/ $*;*$/+$9 $#.M-/ $*;*2#(> ?
$#.M-/ .#&*/+& k $*;*$/+$ @ $*;*2#(H
(/&.(+ .#&*/+&H
J
3+o+ 6nok nonynae+ aprymen+r - na snanenn +nna double. Bnok +ake moe+
nme+r nosnpamaemoe snanenne, no o6 .+om ny+r nosnee.
Ec+r nn y 6nokon nmena? Hoka ne+. Bnok nne+c snanennem (kak, ckaem,
nne+c snanennem nncno 5). H+o6r nme+r nosmonoc+r o6pama+rc k 6noky no nmenn,
neo6xonmo npncnon+r ero 6nonnon nepemennon.
Hcnonsonanne 6nokon
Hon+r, kak pa6o+am+ 6nokn, npome ncero na konkpe+nom npnmepe. B .+om ynpanennn
mr ncnonrsyem 6nok n ncknmnenn ncex rnacnrx 6ykn ns kaon c+pokn maccnna.
Cosan+e nonym nporpammy komannon c+pokn `#.+$'&*#+ c nmenem
#L/-#;/6/+&. B .+on nporpamme mr ncnonrsyem 6nok n nepe6opa maccnna c+pok c
nocneona+enrnrm npeo6pasonannem kaon c+pokn. Cnanana mr cosanm +pn
maccnna: n ncxonrx c+pok, n c+pok c yanennrmn rnacnrmn n n xpanenn
cnmnonon, yanemrx ns c+pok. B qanne main.m samenn+e ko n qnrypnrx cko6kax
.&g(j-j2j{gg-:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
FF B;_l)+&< A)BB&>) B(9;*` &_ *;(;9%[ ?l)ZE(BE ,Z)B+%<`
FF & *;+(<f+<9) lZE +;>%[ B(9;*
xIIPq XSiwx3I4JhQ j xIIPq PIIPq43R./0123Q
MxPa1IpIPa3M` MPqhaJM` M~4h 1Iw PJ2RM` M}4QQ4QQ4HH4M`
J4iW
xShLMSiw Q3I4JhQ rM` Siwx3I4JhQVW
x}a3P/i1IIPq XJ1Ox3I4JhQ j x}a3P/i1IIPq PIIPqW
FF B;_l)+&< B@&B*) B&A>;Z;>` ?l)ZE<A%[ &_ B(9;*&
xIIPq X5SO1iQ j xIIPq PIIPq43R./0123Q
MPM` M1M` M4M` MSM` MaM` J4iW
J
(/&.(+ IH
J
Becr nnnero nonoro; mr npoc+o cosaem maccnnr. Hoc+pon+e n sanyc+n+e
nporpammy. Ha npeynpeenn o ne ncnonrsyemrx nepemennrx noka ne o6paman+e
nnnmann.
Fnana 32. Bnoku ______________________________________________________________________________________
222
O6+nnnenne 6nounon nepeuennon
Tenepr sanmemc koom 6noka. Xo+ 6nokn nnemne noxon na qynknnn, onn mory+
xpann+rc n nepemennrx. Kak n pyrne nepemennre. 6nonnre nepemennre
o6rnnm+c, nocne nero nm npncnannam+c snanenn. Bknmnn+e cneymmnn ko n
main.m, n+o6r o6rnn+r 6nonnym nepemennym.
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
FF B;_l)+&< A)BB&>) B(9;*` &_ *;(;9%[ ?l)ZE(BE ,Z)B+%<`
FF & *;+(<f+<9) lZE +;>%[ B(9;*
P%!(('_ :#-$%&(*+82 k <P%!(('_ '(('_E*&4M/,&2\
C%'./(h('.&C9 CN'_8.+C9 Ca*8 P/($ N'+,4C9 C*22*22*AA*C9
+*-=H
P%c#87C#-$ 2&(*+82\ C9 #-$%&(*+82>H
P%.&'M-/!(('_ :+/L%&(*+82 k <P%.&'M-/!(('_ '(('_=H
FF B;_l)+&< B@&B*) B&A>;Z;>` ?l)ZE<A%[ &_ B(9;*&
P%!(('_ :;#L/-2 k <P%!(('_ '(('_E*&4M/,&2\
C'C9 C/C9 C*C9 C#C9 C.C9 +*-=H
FF ;:eE>Z<+&< :Z;\+;f @<9<A<++;f
5S4w L-w15SO1i41IVL4w` xNJ31h1I` ~.. XVW
J
(/&.(+ IH
J
anan+e nocmo+pnm, ns kaknx nac+en coc+on+ .+o o6rnnenne.
Pnc. 32.1. O6rnnenne 6nonnon nepemennon
O6rnn npnmn+nnnym nepemennym, nr ykasrnae+e ee +nn n nm - nanpnmep,
*+& *. n 6nonnon nepemennon nm ykasrnae+c n cepenne o6rnnenn, cpasy e sa
cnmnonom . Tnn 6nonnon nepemennon sanncn+ o+ cnoco6a konc+pynponann 6noka. B
annom cnynae $/;#L/-*/( o+nocn+c k +nny 6nok, ko+oprn nonynae+ o6rek+, nenoe
nncno n ykasa+enr na ac n ne nosnpamae+ nnnero.
Fnana 32. Bnoku ______________________________________________________________________________________
223
uVn nouuo
nepeVeuuo
1un eosepauaeVoro
suaueuun
npusuak noka
apryVeu1u, paspeneuuue
sann1uVu
Hpncnannanne 6noka
Tenepr npncnonm snanenne namen nonon nepemennon. Bnanenne 6nonnon nepemennon
ncera npec+anne+ co6on na6op nnc+pyknnn n qnrypnrx cko6kax. Bknmnn+e n qann
main.m cneymmnn qparmen+:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ffwq fn{go9 w ogng{r xptvnfv |tfurj9
@@ w ogunjuj{ ptv ugqr fn{go
P%!(('_ :#-$%&(*+82 k <P%!(('_ '(('_E*&4M/,&2\
C%'./(h('.&C9 CN'_8.+C9 Ca*8 P/($ N'+,4C9
C*22*22*AA*C9 +*-=H
P%c#87C#-$ 2&(*+82\ C9 #-$%&(*+82>H
P%.&'M-/!(('_ :+/L%&(*+82 k <P%.&'M-/!(('_ '(('_=H
@@ fgpuwj fswfo fwqgtgq9 xptvjr w fn{gow
P%!(('_ :;#L/-2 k <P%!(('_ '(('_E*&4M/,&2\
C'C9 C/C9 C*C9 C#C9 C.C9 +*-=H
@@ gvqtjuwj tgug sj{juug
;#*$ 7$/;#L/-*/(>7*$9 P%QK+&/8/(9 ac :>H
FF @9&B>)&>)+&< :Z;*) @<9<A<++;f
w15SO1i41I j -L4w Q3I4Jh` xNJ31h1I 4` ~.. XQ3SHV z
x}a3P/i1x3I4Jh XJ1Ox3I4Jh j x}a3P/i1x3I4Jh
Q3I4Jh43Rx3I4JhQ3I4JhW
FF @<9<:;9 A)BB&>) ,Z)B+%[ & _)A<+) >B<[ >[;=l<+&f
FF 'Z<A<+(;> @?B(;f B(9;*&
KSI Lxx3I4Jh XQ 4J 5SO1iQV z
xPJh1 KaiiPJh1 j x}Pp1PJh1Lb` J1Ox3I4Jh i1Jh3RVW
J1Ox3I4Jh I1HiP21.22aII1J21Q.Kx3I4JhQ
O43Rx3I4JhMM
SH34SJQx7PQ1NJQ1JQ43451x1PI2R
IPJh1KaiiPJh1W
|
J1Ox3I4JhQ Pww./0123J1Ox3I4JhW
|W FF *;+<n @9&B>)&>)+&E :Z;*)
J
(/&.(+ IH
J
Cnona noc+pon+e nporpammy n+o6r nponepn+r npannnrnoc+r nnoa.
Hpeynpeenne o ne ncnonrsyemrx nepemennrx ncnesae+.
H+ak, mr cosann 6nok (na6op nnc+pyknnn) n npncnonnn ero 6nonnon nepemennon
devowelizer. O6pa+n+e nnnmanne: npncnannanne 6noka sanepmae+c cnmnonom H, kak n
npncnannanne nm6on pyron nepemennon.
O6rnnenne $/;#L/-*/( mono conmec+n+r c npncnannannem, kak n n cnynae c
nm6on pyron nepemennon:
Fnana 32. Bnoku ______________________________________________________________________________________
224
;#*$ 7$/;#L/-*/(>7*$9 P%QK+&/8/(9 ac :> k 7*$ 2&(*+89 P%QK+&/8/( *9 ac
:2&#A> ?
P%.&'M-/%&(*+8 :+/L%&(*+8 k <P%.&'M-/%&(*+8 2&(*+8E*&4%&(*+8\2&(*+8=H
@@ sj{jg{ ffwq |tfur w ju qfj qgypjuw
@@ tjjungq sxfng fn{gow
B#( 7P%%&(*+8 :2 *+ ;#L/-2> ?
P%N'+8/ B.--N'+8/ k P%'h/N'+8/7I9 <+/L%&(*+8 -/+8&4=>H
<+/L%&(*+8 (/A-',/,,.((/+,/2B%&(*+8\2
L*&4%&(*+8\CC
#A&*#+2\P%^'2/K+2/+2*&*;/%/'(,4
('+8/\B.--N'+8/=H
J
<+/L%&(*+82 '$$M/,&\+/L%&(*+8=H
JH
Kak n n nperymem napnan+e, secr o6rnne+c 6nonna nepemenna c nmenem
$/;#L/-*/(, ko+opa nonynae+ +pn aprymen+a n ne nosnpamae+ nnnero. Ba+em mr
cosaem 6nok n coxpanem ero n $/;#L/-*/(.
Hepe(aua 6noka
Hockonrky $/;#L/-*/( nne+c nepemennon, ee mono nepea+r n aprymen+e. Knacc
P%!(('_ coepn+ me+o c nmenem /+.6/('&/M/,&2Q2*+8a-#fh:. Ennc+nennrm
aprymen+om .+oro me+oa nne+c 6nok, ko+oprn nrnonne+c onn pas n kaoro
o6rek+a n maccnne.
Bknmnn+e n qann main.m cneymmnn ko, ko+oprn nrsrnae+
/+.6/('&/M/,&2Q2*+8a-#,h: c 6nokom $/;#L/-*/(, a sa+em nrnon+ c+pokn c
yanennrmn rnacnrmn.
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
'.&#(/-/'2/A##- ?
@@ fgpuwj ffwq fn{go9 w ogng{r xptvnfv |tfurj9
@@ w ogunjuj{ ptv ugqr fn{go
P%!(('_ :#-$%&(*+82 k <P%!(('_ '(('_E*&4M/,&2\
C%'./(h('.&C9 CN'_8.+C9 Ca*8 P/($ N'+,4C9
C*22*22*AA*C9 +*-=H
P%c#87C#-$ 2&(*+82\ C9 #-$%&(*+82>H
P%.&'M-/!(('_ :+/L%&(*+82 k <P%.&'M-/!(('_ '(('_=H
@@ fgpuwj fswfo fwqgtgq9 xptvjr w fn{gow
P%!(('_ :;#L/-2 k <P%!(('_ '(('_E*&4M/,&2\
C'C9 C/C9 C*C9 C#C9 C.C9 +*-=H

@@ gvqtjuwj tgug sj{juug
;#*$ 7$/;#L/-*/(>7*$9 P%QK+&/8/(9 ac :>H
@@ s{wfqwquwj tgo sj{jjuug
$/;#L/-*/( k 7*$ 2&(*+89 P%QK+&/8/( *9 ac :2&#A> ?
P%.&'M-/%&(*+8 :+/L%&(*+8 k <P%.&'M-/%&(*+8 2&(*+8E*&4%&(*+8\2&(*+8=H
Fnana 32. Bnoku ______________________________________________________________________________________
225

FF @<9<:;9 A)BB&>) ,Z)B+%[ & _)A<+) >B<[ >[;=l<+&f
FF 'Z<A<+(;> @?B(;f B(9;*&
B#( 7P%%&(*+8 :2 *+ ;#L/-2> ?
P%N'+8/ B.--N'+8/ k P%'h/N'+8/7I9 <+/L%&(*+8 -/+8&4=>H
<+/L%&(*+8 (/A-',/,,.((/+,/2B%&(*+8\2
L*&4%&(*+8\CC
#A&*#+2\P%^'2/K+2/+2*&*;/%/'(,4
('+8/\B.--N'+8/=H
J
<+/L%&(*+82 '$$M/,&\+/L%&(*+8=H
JH @@ oguj s{wfqwquwv tgo
FF @<9<:;9 'Z<A<+(;> A)BB&>) B :Z;*;A
Siwx3I4JhQ 1JaT1IP31./0123QQ4Jh~iS2pw15SO1i41IW
xShLMJ1O Q3I4JhQ rM` J1Ox3I4JhQVW
J
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy Ha konconr nrno+c na maccnna. B+opon
maccnn nonynae+c n pesynr+a+e yanenn ns nepnoro ncex rnacnrx.
VIXXUIUIS XI\VY\IV3RXY #L/-#;/6/+&<YWY\YIY= #-$ 2&(*+82\ 7
%'./(h('.&9
N'_8.+9
Ca*8 P/($ N'+,4C9
*22*22*AA*
>
VIXXUIUIS XI\VY\IV3RXW #L/-#;/6/+&<YWY\YIY= +/L 2&(*+82\ 7
%(h(&9
N_8+9
Ca8 P($ N+,4C9
2222AA
>
Bano nomnn+r, n+o n aprymen+e /+.6/('&/M/f&2Q2*+8a-#,h: moe+
nepeana+rc ne nm6on 6nok. Me+o +pe6ye+, n+o6r emy nepeananc 6nok, ko+oprn
nonynae+ o6rek+, nenoe nncno n ykasa+enr na ac, n ne nosnpamae+ nnnero. Bo+
nonemy 6nok; npncnoennrn $/;#L/-*/(, 6rn noc+poen nmenno +ak. Tpn ero aprymen+a
npenasnanenr cnennanrno n nepe6opa coepnmoro maccnna.
Hepnrn aprymen+ coepn+ ykasa+enr na +ekymnn o6rek+. O6pa+n+e nnnmanne:
ykasa+enr nmee+ +nn *$, no.+omy on 6ye+ pa6o+a+r nesanncnmo o+ +oro, kakne o6rek+r
xpan+c n maccnna. Bo n+opom aprymen+e nepeae+c snanenne P%QK+&/8/( - nnekc
+ekymero o6rek+a. Tpe+nn o6rek+ coepn+ ykasa+enr na snanenne ac, no
ymonnannm pannoe P. Ecnn nr xo+n+e, n+o6r nepe6op .nemen+on maccnna sanepmnnc
nocne +ekymen n+epannn, nsmenn+e ero na % .
Bknmnn+e ononnn+enrnym nponepky n nanano npncnannann 6noka:
$/;#L/-*/( k 7*$ 2&(*+89 P%QK+&/8/( *9 ac :2&#A>?
Fnana 32. Bnoku ______________________________________________________________________________________
226
xPJh1 qPJh1 j Q3I4Jh IPJh1.Kx3I4JhMqM
SH34SJQx7PQ1NJQ1JQ43451x1PI2RW
FF +)DZ& q^
4K LqPJh1"iS2P34SJ j xS3SaJwV z
XQ3SH j xW FF ;(A<+) l)Z]+<fD<f &(<9)n&&
I13aIJW FF @9<9%>)+&< (<*?<f &(<9)n&&
|
P%.&'M-/%&(*+8 :+/L%&(*+8 k <P%.&'M-/%&(*+8 2&(*+8E*&4%&(*+8\2&(*+8=H
@@ sj{jg{ ffwq |tfur w ju qfj qgypjuw
@@ tjjungq sxfng fn{gog
B#( 7P%%&(*+8 :2 *+ ;#L/-2> ?
P%N'+8/ B.--N'+8/ k P%'h/N'+8/7I9 <+/L%&(*+8 -/+8&4=>H
<+/L%&(*+8 (/A-',/,,.((/+,/2B%&(*+8\2
J
<+/L%&(*+82 '$$M/,&\+/L%&(*+8=H
JH @@ oguj s{wfqwquwv tgo
Honrn qparmen+ nponepe+, coepn+ nn c+poka +ekymen n+epannn 6ykny _ n
nepxnem nnn nnnem pernc+pe. Ecnn nxoenne naneno, +o ykasa+enm saae+c
snanenne % (o+menmmee nocneymmne n+epannn), nocne nero +ekymnn nepe6op
npeprnae+c.
Hoc+pon+e n sanyc+n+e nporpammy. H cnona na konconr nrno+c na maccnna,
no na .+o+ pas nepe6op o+mene+c na n+opon n+epannn, kora 6nok o6napynnae+ cnono
c 6yknon _. o+ ncero maccnna oc+ae+c +onrko %(h(&.
H+ak, mr nemnoro no+pennponanncr n ncnonrsonannn 6nokon. anan+e nepnemc
n nocmo+pnm, kak 6nokn nomoram+ n pemennn npo6nemr nornneckon yanennoc+n
qparmen+on koa n nporpammax. Hpn ncnonrsonannn o6pa+noro nrsona - kak n rnane 24,
re ncnonrsonanacr cneymma c+poka koa:
<<P%P#&*B*,'&*#+^/+&/( $/B'.-&^/+&/(=
'$$M2/(;/(\-#88/(
2/-/,&#(\2/-/,&#(7#+/^4'+8/\>
+'6/\P%%_2&/6Z*6/#+/O*$^4'+8/P#&*B*,'&*#+ #M/,&\+*-=H
nr saae+e me+o (o6rnno c ncnonrsonannem 2/-/,&#(7 >), nocne nero peannsye+e
.+o+ me+o re-+o n pyrom mec+e qanna:
U 7;#*$>#+/^4'+8/\7P%P#&*B*,'&*#+ :>+#&/
?
P%c#87CZ4/ 2_2&/6 &*6/ #+/ 4'2 ,4'+8/$FC>H
J
Take nosmono ncnonrsonanne me+oa P%P#&*B*,'&*#+^/+&/(
'$$M2/(;/(`#(P'6/\#M/,&\././\.2*+8a-#,h: n nepeanen 6noka. Hpn
ncnonrsonannn .+oro me+oa P%P#&*B*,'&*#+^/+&/( nponcxon+ nenocpec+nenna
nepeana nnc+pyknnn, +ak n+o nam ne npne+c pasmema+r ko o6pa+noro nrsona re-+o
n pyrom mec+e. hm6on nn+a+enr namero koa ynnn+ nnc+pyknnn n coo6menne,
Fnana 32. Bnoku ______________________________________________________________________________________
227
o+npannemoe P%P#&*B*,'&*#+^/+&/(, n onom qparmen+e koa. (Hmenno +akoe
nsmenenne n nporpamme ^'--M',h2 nam 6ye+ npenoeno peannsona+r n ynpanennn n
konne .+on rnanr.)
typedef
Cnn+akcnc 6nokon moe+ 6r+r ononrno cnonrm, no ero mono ynpoc+n+r npn
nomomn knmnenoro cnona &_A/$/B, o ko+opom nr ysnann n rnane 10. Hanomnm, n+o
onpeenenn &_A/$/B pacnonaram+c n nanane qanna nnn sarononka, nne peannsannn
kaknx-nn6o me+oon. Bknmnn+e n main.m cneymmym c+poky koa:
)*6A#(& 1`#.+$'&*#+@`#.+$'&*#+345
3qH1w1K 5S4w L-IIPqJaT1IP34SJ~iS2pVL4w` xNJ31h1I` ~.. XVW
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
Onpeenenne &_A/$/B nen+nnno o6rnnennm nepemennon 6noka. Onako n
annom cnynae mr o6rnnem +nn, a ne nepemennym, no.+omy pom c cnmnonom
ykasrnae+c coo+ne+c+nymmee nm +nna. Hcnonrsonanne &_A/$/B ynpomae+
o6rnnenn 6nokon. Bany+annoe o6rnnenne devowelizer:
;#*$ 7$/;#L/-*/(>7*$9 P%QK+&/8/(9 ac :>H
samene+c namnoro 6onee non+nrm:
IIPqJaT1IP34SJ~iS2p w15SO1i41IW
Takoe o6rnnenne 6nonnon nepemennon nrrnn+ 6onee snakomrm. Cneye+
nomnn+r, n+o +nn 6noka onpeene+ +onrko +nnr ero aprymen+on n nosnpamaemoro
snanenn; on ne nmee+ o+nomenn k na6opy nnc+pyknnn n 6noke .+oro +nna.
Bosnpamaeume snauennn
Hakonen, ecnn 6nok nosnpamae+ snanenne, +o 6nonnym nepemennym mono nrsrna+r
kak qynknnm.
$#.M-/ 7$*;a-#,h>7$#.M-/9$#.M-/> k 7$#.M-/ h9 $#.M-/ > ?
(/&.(+ h@H
J
B .+om koe o6rnne+c 6nonna nepemenna $*;a-#,h, ko+opa nosnpamae+
snanenne double n nonynae+ na snanenn double n aprymen+ax. anee nepemennon
Fnana 32. Bnoku ______________________________________________________________________________________
228
npncnannae+c snanenne - nnc+pyknn, nosnpamamma pesynr+a+ enenn nyx
aprymen+on.
Bosmonoe ncnonrsonanne 6noka moe+ nrrne+r +ak:
$#.M-/ .#&*/+& k $*;a-#,h7]V3I9 XV3T>H
npannenne naunv
Bnokn, kak n npnmn+nnnre nepemennre, cosam+c n xpan+c n c+eke. Cneona+enrno,
6nok ynnn+oae+c nmec+e c kapom c+eka npn nosnpa+e ynpannenn qynknnen nnn
me+oom, re .+o+ 6nok cosae+c. Onako n neko+oprx cn+yannx 6rnae+ nyno,
n+o6r 6nok npoonan cymec+nonanne n nocne nrxoa - na- npnmep, on moe+ 6r+r
nepemennon .ksemnnpa o6rek+a. B +akom cnynae 6nok neo6xonmo ckonnpona+r ns
c+eka n kyny.
H+o6r ckonnpona+r 6nok ns c+eka n kyny, o+npanr+e emy coo6menne fg{x:
!(('_+.6/('&*#+a-#,h *'(O/;#L/-*/( k <$/;#L/-*/( ,#A_=H
Tenepr konn 6noka cymec+nye+ n kyne, a nona 6nonna nepemenna coepn+
ykasa+enr na .+o+ 6nok.
Me+or, nonynammne 6nokn n aprymen+ax (+akne, kak
/+.6/('&/M/,&2Q2*+8a-#,h: knacca P%!(('_ nnn '$$M2/(;/(`#(P'6/\#M/,&\././:
.2*+8a-#,h: knacca P%P#&*B*,'&*#+^/+&/(), onnr ckonnpona+r nepeannre 6nokn.
Tem camrm onn cosam+ ykasa+enn - n cnnrnre ccrnkn - na .+n 6nokn.
H+ak, 6nokn mono o6rnn+r, npncnanna+r snanenn n nepeana+r, kak
nepemennre. Mr +ake nnenn, n+o 6nokn o+nac+n noxon na qynknnn. A +enepr mr
o+npannem 6noky coo6menne +ak, cnonno on nne+c o6rek+om.
Haxoenne 6noka n kyne n ero o6rek+noe noneenne cosae+ neko+opre
npo6nemr c ynpannennem nam+rm.
N#' 0&'!+D',!# + 0$&$3$""<3!, !+0'19-6$3<3! ( .1'/$?
B cnoem koe 6nok o6rnno ncnonrsye+ pyrne nepemennre (n npnmn+nnnre, n
ykasa+enn na o6rek+r), cosannre sa ero npeenamn. H+o6r nnemnne nepemennre
oc+ananncr oc+ynnrmn na nce npem nx ncnonrsonann n 6noke, nepemennre
saxna+rnam+c 6nokom npn cosannn konnn.
n npnmn+nnnrx nepemennrx .+o osnanae+ konnponanne n coxpanenne nx n
nokanrnrx nepemennrx 6noka. n ykasa+enen 6nok xpann+ cnnrnym ccrnky na nce
o6rek+r, k ko+oprm on o6pamae+c. 3+o osnanae+, n+o nm6re o6rek+r, ccrnka na
ko+opre xpann+c n 6noke, saneomo 6yy+ cymec+nona+r no kpannen mepe o +ex nop,
noka cymec+nye+ cam 6nok. (Bac nn+epeconano, nem 6nokn o+nnnam+c o+ ykasa+enen na
qynknnn? Hyc+r-ka ykasa+enr na qynknnm nonpo6ye+ cena+r nen+o noo6noe!)
Bepnemc k nporpamme #Lj-#;j6j+&. B 6noke $/;#L/-*/( ynomnnam+c na
o6rek+a, cosannrx sa npeenamn .+oro 6noka: +/L%&(*+82 (maccnn n xpanenn
nsmenennrx nepcnn c+pok) n 2&(*+8 (+ekyma c+poka, konnpyema n nsmenenn).
Fnana 32. Bnoku ______________________________________________________________________________________
229
$/;#L/-*/( coxpane+ cnnrnre ccrnkn na o6a o6rek+a, ncnec+nne nero onn saneomo
npoonam+ cymec+nona+r o +ex nop, noka cymec+nye+ cam 6nok.
J'%6# 1! +!19"<$ ++<1/! 0&!($+#! / 0'*(1$"!: 7!/1!=$+/!D ++<1'/?
Konenno, mory+. Pemenne npo6nemr nam ye nsnec+no: ona ns ccrnok onna
c+a+r cna6on. n .+oro sa npeenamn 6noka o6rnne+c cna6rn ykasa+enr (eL/'h),
ko+oprn no+om ncnonrsye+c n 6noke.
J'%6 1! * !-3e"!#9 0$&$3$""<$, +/'0!&'()""<$ .1'/'3?
Ho ymonnannm nepemennre, ckonnponannre 6nokom, n 6noke nsmen+rc ne
mory+, +o ec+r qak+nneckn npenpamam+c n konc+an+r. Hanpnmep, nepemennre
ykasa+enen na o6rek+r coxpanm+ noc+onnoe snanenne n npeenax 6noka. (Xo+ nr
moe+e o+npann+r o6rek+y coo6menne, nsmenmmee ero coepnmoe, cam ykasa+enr
nsmenn+r nenrs.) Onako n neko+oprx cnynax +pe6ye+c nsmenn+r nnemnmm
nepemennym nny+pn 6noka. n .+oro nnemn nepemenna onna 6r+r o6rnnena c
knmnenrm cnonom eM-gfh. Hanpnmep, n cneymmem qparmen+e ynennnnnae+c nnemn
nepemenna ,#.+&/(:
eeM-#,h *+& ,#.+&/( k IH
;#*$ 7,#.+&/(a-#,h>7> k ? ,#.+&/(llH JH
333
,#.+&/(a-#,h7>H @@ ujuwj ,#.+&/( xqjtwjqjnfv pg X
,#.+&/(a-#,h7>H @@ ujuwj ,#.+&/( xqjtwjqjnfv pg V
Bes knmnenoro cnona eM-gfh nr nonynn+e omn6ky komnnnnnn n onpeenennn
6noka, c coo6mennem o +om, n+o snanenne ,#.+&/( ne moe+ nsmen+rc.
By(ymee 6nokon
Hon+r, kak pa6o+am+ 6nokn n kak nx npannnrno ncnonrsona+r, moe+ 6r+r nenpoc+o.
Onako 6nokn npesnrnanno nonesnr n npnnoennx, nn+encnnno ncnonrsymmnx
o6pa6o+ky co6r+nn, +ak nac+o nc+penammnxc n nporpammnponannn Mac n iOS. B
nporpammnrx nn+epqencax Apple 6nokn ncnonrsym+c nce name. Hanpnmep,
6n6nno+ekn !c!22/&c*M('(_ n "'6/*& coepa+ mnoronncnennre me+or c
ncnonrsonannem 6nokon. C+apan+ecr no nosmonoc+n ncnonrsona+r 6nonnre me+or
Apple, n+o6r npnnrkny+r k pa6o+e c 6nokamn.
npawnennn
Anonnmnrn 6nok
B npnmepe .+on rnanr onepannn o6rnnenn, npncnannann n ncnonrsonann 6noka
naxo+c n +pex pasnrx c+pokax koa. 3+o cenano n yo6c+na n+enn koa.
Fnana 32. Bnoku ______________________________________________________________________________________
230
Ecnn nam no+pe6ye+c nepea+r nenoe nncno me+oy (nanpnmep, +.6M/(E*&4K+&:
knacca P%P.6M/(),nr moe+e nepea+r +nn *+& anonnmno:
@@ {wun X\ zgftjpgqnjtugj qrsgtujuwj qfj gsj{w
*+& *H
* k TH
P%P.6M/( :+.6 k <P%P.6M/( +.6M/(E*&4K+&\*=H
@@ {wun V\ ujuwj sj{jpjnfv j gvqtjuwv sj{jjuug
P%P.6M/( :+.6 k <P%P.6M/( +.6M/(E*&4K+&\T=H
Tak kak 6nokn nnm+c nepemennrmn, ananornnnrm o6pasom mono noc+yna+r
n c 6nokamn. Bonee +oro, .+o camrn pacnpoc+panennrn cnoco6 nx ncnonrsonann.
Paspa6o+nnkn non+n nnkora ne o6rnnm+ 6nonnre nepemennre n nepeann 6nokon
me+oam; onn non+n ncera nr6npam+ napnan+ c anonnmnrm ncnonrsonannem.
Hsmenn+e ynpanenne .+on rnanr +ak, n+o6r 6nok nepeananc anonnmno n
aprymen+e /+.6/('&/M/,&2Q2*+8a-#,h:. Hnane ronop, nr oc+anne+e 6nok, no
ns6anne+ecr o+ 6nonnon nepemennon.
NSNoticationCenter
B rnane 24 me+o '$$M2/(;/(\2/-/,&#(\+'6/\#M/,&: knacca P%P#&*B*,'&*#+^/+&/(
ncnonrsonanc n pernc+pannn o6pa+nrx nrsonon nepes me+o #+/^4'+8/:. Hsmenn+e
.+o ynpanenne +ak, n+o6r nmec+o nero ncnonrsonanc me+o
'$$M2/(;/(`#(P'6/\#M/,&\././\.2*+8a-#,h:. Ba nopo6noc+mn o6paman+ecr k
okymen+annn paspa6o+nnka.
Me+o nonynae+ 6nok n aprymen+e, a sa+em nrnonne+ ero npn nonynennn
saannoro ononemenn nmec+o +oro, n+o6r o6pama+rc k o6rek+y c o6pa+nrm nrsonom.
3+o osnanae+, n+o me+o #+/^4'+8/: nrsrna+rc ne 6ye+, a ko, naxomnnc n .+om
me+oe, cneye+ nepemec+n+r n 6nok.
Hepeanaemrn 6nok onen nonyna+r onn aprymen+ 7P%P#&*B*,'&*#+ :> n ne
nosnpama+r nnnero, kak .+o enae+ #+/^4'+8/:.
B aprymen+e ././: mono nepea+r +*-; .+o+ aprymen+ ncnonrsye+c n
cnnxponnsannn - +ema, ko+opa n knnre ne paccma+pnnae+c.
Fnana 32. Bnoku ______________________________________________________________________________________
231
N
<(9&>&)Z]+%<
>;_A;=+;B(& -
Knannqnnnponannrn nporpammnc+ Objective-C +ake onen xopomo nnae+r srkom
C. Cymec+nye+ eme nemano nosmonoc+en nporpammnponann na C, o ko+oprx nam
neo6xonmo sna+r. Bosmono, konnennnn, npec+annennre n .+on nac+n, ne 6yy+
noc+onno ncnonrsona+rc n namen noncenennon pa6o+e. Tem ne menee npem o+
npemenn nr nens6eno 6ye+e c+anknna+rc C nnmn, no.+omy xony npec+ann+r nx
nam.
232
cc" G;9)_9El+%< ;@<9)n&&
B nepnon nac+n knnrn cpannnn nam+r komnrm+epa c orpomnrm maccnnom
nepeknmna+enen, ko+opre mory+ naxon+rc no nknmnennom nnn nrknmnennom
coc+onnn. Karn nepeknmna+enr npec+anne+ onn 6n+; snanenne 1 o6rnno
cnn+ae+c nknmnennrm, a 0 - nrknmnennrm.
Onako n cnonx nporpammax nr ne apecye+e o+enrnre 6n+r, a nmee+e eno c
rpynnamn 6n+on - 6an+amn. Ecnn npec+ann+r 6an+ kak 8-paspnoe nenoe nncno 6es
snaka, ero 6n+r coo+ne+c+nym+ nocneona+enrnrm c+enenm 2 (pnc. 33.1 ).
Pnc. 33.1. Ban+, npec+annmmnn ec+nnnoe nncno 60
hmn npenonn+am+ pa6o+a+r n ec+nnnon cnc+eme cnncnenn - no6onnrn
.qqek+ +oro, n+o y nac na pykax no 10 nanrnen. Onako komnrm+epam yo6nee nme+r
eno co c+enenmn 2. Hporpammnc+r nac+o ncnonrsym+ 8$+#"),7)#$&!="6: cnc+emy
(16 = 2
4
) - oco6enno npn pa6o+e c o+enrnrmn paspamn nenrx nncen.
B kanec+ne ononnn+enrnrx nnqp n mec+nana+epnnnon cnc+eme ncnonrsym+c
6yknr a, b, c, d, e n f. Taknm o6pasom, cne+ n mec+nana+epnnnon cnc+eme nrrnn+ +ak:
0,1,2,3,4,5,6,7,8,9, a, b, c, d, e, f, 10, 11, ...
Hncna, sanncannre n mec+nana+epnnnon cnc+eme, o6osnanam+c npeqnkcom 0x.
Ha pnc. 33.2 nokasano, kak nrrnn+ nncno n 6an+ na pnc.33.1 n mec+nana+epnnnon
sanncn.
Pnc. 33.2. Onn 6an+, npec+annmmnn mec+nana+epnnnoe nncno 0x3c
Onn 6an+ ncera moe+ 6r+r npec+annen mec+nana+epnnnrm nncnom ns nyx
nnqp (nanpnmep, 3c). Ho .+on npnnnne mec+nana+epnnnre nncna yo6nr n pa6o+r c
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
233
nonnnrmn annrmn. Hanepnka nr cnrmann, kak onr+nre nporpammnc+r ronop+ n+o-
nn6yr npoe: B pacmnqponan qopma+ qanna, npocma+pnna okymen+ n
mec+nana+epnnnom peak+ope. Xo+n+e npocmo+pe+r coepnmoe qanna n nne
nocneona+enrnoc+n 6an+on n mec+nana+epnnnon sanncn? Brnonnn+e n Terminal
komany 4j$.6A c nmenem qanna:
R1waTH TqK4i1"33
IIIIIII S, SB YW R$ R, VI YR RT YV YS R RB R/ S$ VV SX
IIIIIXI V/ SI VV SB S/ I' S, RV RB RB RM VI YW R$ R, R/
IIIIIVI YS S$ VV RW Y] Y] YI S' VB VB R] RB RS RV RB RB
IIIIISI RM V/ RB YV RY VB R/ YS VB R] RB RS RV RB RB RM
IIIII]I VV
IIIII]X
B nepnom c+on6ne ykasano cmemenne (mec+nana+epnnnoe) n 6an+a, ykasannoro
no n+opom c+on6ne. Kaoe mec+nana+epnnnoe nncno ns nyx nnqp npec+anne+ onn
6an+.
Hopaspn(nan onepannn HH
na 6an+a mono o6rennn+r nopaspnon onepannen ; pesynr+a+ npec+anne+
co6on +pe+nn 6an+. Bn+ +pe+rero 6an+a coepn+ 1 n +om cnynae, ecnn xo+ 6r onn ns
nyx coo+ne+c+nymmnx 6n+on nepnrx nyx 6an+on panen 1.
Hopaspna onepann nrnonne+c onepa+opom 1. n .kcnepnmen+on c
o+enrnrmn 6n+am cosan+e nonrn npoek+: nporpammy komannon c+pokn C (ne
`#.+$'&*#+!) c nmenem M*&L*/.
O+peak+npyn+e qann main.c:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
aJQ4hJ1w 2RPI P j bc2W
aJQ4hJ1w 2RPI / j bPW
aJQ4hJ1w 2RPI 2 j P /W
HI4J3KLM1 r r j rUJM` P` /` 2VW
HI4J3KLMy124TPi rw rw j rwUJM` P` /` 2VW
(/&.(+ IH J
Pnc. 33.3. O6rennenne nyx 6an+on nopaspnon onepannen HhH
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
234
Hporpamma nrae+ pesynr+a+ o6rennenn nyx 6an+on nopaspnon onepannen
:
D/d\ S, ' k M$
O/,*6'-\ RI XR k XW
Banem .+o nyno? B Objective-C nenre nncna nac+o ncnonrsym+c n
onpeenenn nac+poek. Henoe nncno ncera npec+anne+ co6on nocneona+enrnoc+r
6n+on, n karn 6n+ npec+anne+ onn acnek+ o6men nac+ponkn, ko+oprn moe+
nknmna+rc/nrknmna+rc nesanncnmo o+ pyrnx. 3+o nenoe nncno (+ake nasrnaemoe
6n+onon mackon) c+pon+c ns konc+an+, nxomnx n sapanee onpeenennrn na6op.
Konc+an+r +ake nnm+c nenrmn nncnamn, n kaa konc+an+a saae+ onn acnek+
nac+ponkn, +o ec+r yc+anannnnae+ +onrko onn 6n+ n o6mem snanennn. H+oronoe
snanenne o6pasye+c nocpec+nom o6rennenn konc+an+, npec+annmmnx nynre
acnek+r, nopaspnon onepannen .
Paccmo+pnm konkpe+nrn npnmep. B iOS nxon+ knacc c nmenem P%O'&'O/&/,&#(.
3ksemnnpr P%O'&'O/&/,&#( nmy+ n +ekc+e nxoenn c+anap+nrx o6osnanennn -
+aknx, kak a+r nnn URL-apeca. Hckomre o6osnanenn onpeenm+c o6rennennem
nenonncnennrx konc+an+ ns qnkcnponannoro na6opa nopaspnon onepannen .
B qanne NSDataDetector.h onpeenm+c cneymmne konc+an+r:
P%Z/d&^4/,h*+8Z_A/O'&/, P%Z/d&^4/,h*+8Z_A/!$$(/22, P%Z/d&^4/,h*+8Z_A/c*+h,
P%Z/d&^4/,h*+8Z_A/b4#+/P.6M/(, n P%Z/d&^4/,h*+8Z_A/Z('+2*&K+B#(6'&*#+. Hpn
cosannn .ksemnnpa P%O'&'O/&/,&#( nr ykasrnae+e, n+o nmenno on onen ncka+r.
Hanpnmep, n+o6r .ksemnnp nckan n +ekc+e +eneqonnre nomepa n a+r, nr
onpeene+e ero cneymmnm o6pasom:
P%((#( :/H
P%O'&'O/&/,&#( :$ k <P%O'&'O/&/,&#( $'&'O/&/,&#(E*&4Z_A/2\
P%Z/d&^4/,h*+8Z_A/b4#+/P.6M/(P%Z/d&^4/,h*+8Z_A/O'&/
/((#(\/=H
O6pa+n+e nnnmanne na onepa+op nopaspnon onepannn nnn. 3+o+ npnem onenr
nac+o nc+penae+c n nporpammnponannn Cocoa n iOS; +enepr nr snae+e, n+o npn .+om
nponcxon+ sa kynncamn.
Hopaspn(nan onepannn H
Take na 6an+a mono o6rennn+r nopaspnon onepannen n cosann +pe+rero
6an+a. B .+om cnynae 6n+ +pe+rero 6an+a panen 1 +onrko n +om cnynae, ecnn o6a
coo+ne+c+nymmnx 6n+a nepnrx nyx 6an+on pannr 1.
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
235
Pnc. 33.4. O6rennenne nyx 6an+on nopaspnon onepannen H
Hopaspna onepann nrnonne+c onepa+opom . Bknmnn+e n main.c
cneymmne c+pokn:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
.+2*8+/$ ,4'( ' k IdS,H
.+2*8+/$ ,4'( M k Id'H
.+2*8+/$ ,4'( , k ' MH
A(*+&B7CD/d\ d d k dG+C9 '9 M9 ,>H
A(*+&B7CO/,*6'-\ $ $ k $G+C9 '9 M9 ,>H
aJQ4hJ1w 2RPI w j P /W
HI4J3KLM1 r r j rUJM` P` /` wVW
HI4J3KLMy124TPi rw rw j rwUJM` P` /` wVW
(/&.(+ IH
J
Hporpamma nrae+ pesynr+a+ o6rennenn nyx 6an+on nopaspnon onepannen
:
D/d\ S, ' k VW
O/,*6'-\ RI XR k ]I
B Objective-C nopaspna onepann ncnonrsye+c n nponepkn coc+onn
konkpe+noro 6n+a (nnn qnara). Hanpnmep, npn nonynennn .ksemnnpa P%O'&'O/&/,&#(
nr moe+e nponepn+r, nknmnen nn n nero nonck +eneqonnrx nomepon:
*B 7<,.((/+&O/&/,&#( ,4/,h*+8Z_A/2= P%Z/d&^4/,h*+8Z_A/b4#+/P.6M/(> ?
P%c#87CZ4*2 #+/ *2 -##h*+8 B#( A4#+/ +.6M/(2C>H
J
Me+o ,4/,h*+8Z_A/2 nosnpamae+ nenoe nncno, ko+opoe nne+c pesynr+a+om
o6rennenn nopaspnon onepannen ncex qnaron, yc+anonnennrx n annom
.ksemnnpe P%O'&'O/&/,&#(. Hncno o6renne+c nopaspnon onepannen c
konkpe+non konc+an+on P%Z/d&^4/,h*+8Z_A/, nocne nero nponepe+c pesynr+a+. Ecnn
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
236
6n+, yc+anonnennrn n P%Z/d&^4/,h*+8Z_A/b4#+/P.6M/(, ne yc+anonnen n nac+ponkax
P%O'&'O/&/,&#(, pesynr+a+ nopaspnon onepannn 6ye+ coc+o+r ns onnx nynen. B
npo+nnnom cnynae pesynr+a+ 6ye+ o+nnnen o+ nyn, a nr snae+e, n+o P%O'&'O/&/,&#(
nme+ n +ekc+e +eneqonnre nomepa.
pyrne nopaspn(nme onepavopm
n nonno+r kap+nnr +ake cneye+ ynomny+r o pyrnx nopaspnrx onepa+opax.
Onn pee ncnonrsym+c n Objective-C, no sna+r o nnx nonesno.
Hcknuaman onepannn HH
Hcknmnamma onepann o6renne+ na 6an+a n cosae+ +pe+nn 6an+. Bn+
pesynr+a+a panen 1 n +om cnynae, ecnn ponno onn ns nyx coo+ne+c+nymmnx 6n+on
nxonrx 6an+on panen 1.
Pnc. 33.5. O6rennenne nyx 6an+on nopaspnon ncknmnammen onepannen HhH
Onepann nrnonne+c onepa+opom . Bknmnn+e n main.c cneymmnn qparmen+:
aJQ4hJ1w 2RPI 1 j P - /W
HI4J3KLM1 r - r j rUJM` P` /` 1VW
HI4J3KLMy124TPi rw - rw j rwUJM` P` /` 1VW
(/&.(+ IH J
z{g|{ qrqgpwn ftjpxw {jxtnn\
D/d\ S, ' k T
O/,*6'-\ RI XR k X]
Y neonr+nrx nporpammnc+on nnora nosnnkam+ +pynoc+n c ncnonrsonannem
.+oro onepa+opa. B 6onrmnnc+ne .nek+ponnrx +a6nnn onepa+op n nrnonne+ onepannm
nosneenn n c+enenr: 2^3 osnanae+ 2
3
. B C nosneenne n c+enenr nrnonne+c
qynknnen A#L7>:
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
237
$#.M-/ ( k A#L7V3I9 S3I>H @@ rwftvjn V q n{jnj fnjsjuw
ononnenne
O'0'1"$"!$3 k cymec+nymmemy 6an+y nasrnae+c 6an+, 6n+r ko+oporo naxo+c n
npo+nnonononom coc+onnn: nce nynn samenm+c ennnnamn, a nce ennnnr
samenm+c nynmn.
Onepann nrnonne+c onepa+opom ~. o6anr+e neckonrko c+pok n qann main.c:
aJQ4hJ1w 2RPI K j /W
HI4J3KLM1 oR1 2STHi1T1J3 SK r 4Q rUJM` /` KVW
HI4J3KLMy124TPi oR1 2STHi1T1J3 SK rw 4Q rwUJM` /` KVW
(/&.(+ IH
J
Pnc. 33.6. ononnenne
Pesynr+a+ onen nrrne+r +ak:
D/d\ Z4/ ,#6A-/6/+& #B ' *2 TR
O/,*6'-\ Z4/ ,#6A-/6/+& #B XR *2 WR
C(nnr nneno
Hpn nrnonnennn onepannn cnnra nneno karn 6n+ cmemae+c n nanpannennn
c+apmero 6n+a. Bn+r, nrxomne sa npeenr nncna, +epm+c, a nosnnkammne cnpana
nyc+o+r sanonnm+c nynmn.
Pnc. 33.7. Cnnr nneno na na paspa
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
238
Onepann cnnra nneno nrnonne+c onepa+opom 11. Bknmnn+e n qann main.c
cnnr na ne nosnnnn nneno:
aJQ4hJ1w 2RPI h j P CW
HI4J3KLM1 r QR4K31w i1K3 3OS HiP21Q 4Q rUJM` P` hVW
HI4J3KLMy124TPi rw QR4K31w i1K3 3OS HiP21Q 4Q rwUJM` P` hVW
(/&.(+ IH J
Hporpamma nrnon+ cneymmnn pesynr+a+:
D/d\ S, 24*B&/$ -/B& &L# A-',/2 *2 BI
O/,*6'-\ RI 24*B&/$ -/B& &L# A-',/2 *2 V]I
Hpn kaom cnnre nneno na onn pasp snanenne nncna ynannae+c.
C(nnr nnpano
Onepann cnnra nnpano pa6o+ae+ nnonne npeckasyemo. Bknmnn+e n qann main.c
cneymmnn qparmen+:
aJQ4hJ1w 2RPI R j P !W
HI4J3KLM1 r QR4K31w I4hR3 SJ1 HiP21 4Q rUJM` P` RVW
HI4J3KLMy124TPi rw QR4K31w I4hR3 SJ1 HiP21 4Q rwUJM` P` RVW
(/&.(+ IH J
Pesynr+a+ nrnonnenn:
D/d\ S, 24*B&/$ (*84& #+/ A-',/2 *2 X/
O/,*6'-\ RI 24*B&/$ (*84& &L# A-',/2 *2 SI
Pnc. 33.8. Cnnr nnpano na onn pasp
Hcnonsonanne enum (nn onpe(enennn
6nvonmx uacok
Hac+o n nporpamme 6rnae+ nyno onpeenn+r cnncok konc+an+; kaa ns ko+oprx
npec+anne+ nenoe nncno c onnm yc+anonnennrm 6n+om. anee .+n nncna
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
239
o6rennm+c nopaspnon onepannen n nponepm+c c ncnonrsonannem
nopaspnon onepannn (cm. nrme).
3neran+noe pemenne .+on saann ocnonano na cosannn nepenncnenn,
ncnonrsymmero onepa+op cnnra nneno n onpeenenn snanennn. Hanpnmep,
konc+an+r UIDataDetector onpeenm+c cneymmnm o6pasom:
/+.6 ?
QKO'&'O/&/,&#(Z_A/b4#+/P.6M/( kX11I9
QKO'&'O/&/,&#(Z_A/c*+h kX11X9
QKO'&'O/&/,&#(Z_A/!$$(/22 kX11V9
QKO'&'O/&/,&#(Z_A/^'-/+$'(;/+& kX11S9
QKO'&'O/&/,&#(Z_A/P#+/ k I9
QKO'&'O/&/,&#(Z_A/!-- k P%QK+&/8/('d
JH
Bonme 6anvon
B .+on rnane mr pa6o+ann c +nnom .+2*8+/$ f4'(, coc+omnm ns onoro 8-paspnoro
6an+a. hm6on 6essnakonrn nenrn +nn pa6o+ae+ ananornnnrm o6pasom. Hanpnmep,
P%Z/d&^4/,h*+8Z_A/b4#+/P.6M/( n enc+nn+enrnoc+n o6rnne+c kak .+*&R]e& - 64-
paspnoe nncno 6es snaka.
npawnenne
Hannmn+e nporpammy, ko+opa cosae+ 64-paspnoe nenoe nncno 6es snaka, n ko+opom
yc+anonnen karn n+opon 6n+. (Bosmonr na pesynr+a+a: ne+nrn n nene+nrn;
cosan+e nene+nrn.) Brnen+e nonynennoe nncno.
Fnana 33. Hopaspnpume onepaquu ______________________________________________________________________________________
240
ck" -(9;*& -
Ecnn y nporpammnc+a Objective-C ec+r nr6op, on ncera npenon+e+ pa6o+a+r c
o6rek+amn P%%&(*+8 nmec+o c+pok C. Bnponem, nnora nr6opa ne+. Cama
pacnpoc+panenna npnnnna n ncnonrsonann c+pok C? Pa6o+a c 6n6nno+ekamn C ns
koa Objective-C. Hanpnmep, cymec+nye+ 6n6nno+eka qynknnn C, nosnonmma namen
nporpamme nsanmoenc+nona+r c cepnepom 6as annrx b#2&8(/%c. dynknnn .+on
6n6nno+ekn ncnonrsym+ c+pokn C, a ne .ksemnnpr P%%&(*+8.
char
B nperymem pasene paccma+pnnanacr nosmonoc+r nn+epnpe+annn 6an+on kak
nncen. Ban+r +ake mory+ nn+epnpe+npona+rc kak cnmnonr. Kak ynomnnanocr panee,
cymec+nye+ mnoro pasnrx konponok cnmnonon. Cama c+apa, n noanyn, cama
nsnec+na - konponka ASCII (American Standard Code for Information Interchange). B
konponke ASCII pasnre 6an+r onpeenm+ pasnre cnmnonr. Hanpnmep, ko 0x4b
coo+ne+c+nye+ cnmnony K. Cosan+e nonym nporpammy komannon c+pokn C c nmenem
_#2&(*+8. Hporpamma 6ye+ nrnon+r neko+opre cnmnonr ns c+anap+a ASCII.
O+peak+npyn+e main.c:.
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
2RPI j bC!W FF B&A>;Z gg
OR4i1 L j b1V z FF B&A>;Z gg
HI4J3KLMr 4Q r2UJM` ` VW
ttW
|
(/&.(+ IH J
Bosnnkae+ sakonnrn nonpoc: Ban+ moe+ coepa+r ono ns 256 nncen. Mr
+onrko n+o nrnenn 94 cnmnona. A n+o nponcxon+ c oc+anrnrmn? Bano nonnma+r, n+o
konponka ASCII paspa6a+rnanacr n ynpannenn c+aprmn +epmnnanamn,
pa6o+anmnmn no npnnnnny +ene+anna, ko+opre nena+ann annre na 6ymare (nmec+o
nrnoa na .kpan). Hanpnmep, npn nrnoe nncna 7 n konponke ASCII yc+ponc+no nsae+
snykonon cnrnan. Cnmnonr 0-31 n ASCII coo+ne+c+nym+ nenena+aemrm ynpannmmnm
cnmnonam: 32 - cnmnon npo6ena, 127 - yanenne nperymero cnmnona n +. . Kak nacne+
cnmnonon 128-255? B ASCII ncnonrsym+c +onrko 7 6n+on, n koa 128 ASCII -
cnmnona ne cymec+nye+.
Kor ASCII mory+ ncnonrsona+rc n koe kak nn+epanr, oc+a+onno saknmnn+r
nx n anoc+poqr. Bnecn+e nsmenenn n cnon ko:
Fnana 34. C1poku C ______________________________________________________________________________________
241
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
,4'( d k ggH @@ Z4/ ,4'(',&/( F
L4*-/ 7d 1k gg> ? @@ Z4/ ,4'(',&/( A(*+&B7Cd *2 ,G+C9 d9 d>H
dllH
J
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy
Henena+aemre cnmnonr mory+ 6r+r npec+annenr n nne ynpannmmnx
0'+1$,'()#$19"'+#$4, nannnammnxc c cnmnona \. Banncr \n ye ncnonrsonanacr n
cnmnona nonon c+pokn. Hpnney neckonrko pacnpoc+panennrx nocneona+enrnoc+en:
Ta6nnna 34.1. Hac+o ncnonrsyemre ynpannmmne nocneona+enrnoc+n
\n
nona c+poka
\t
+a6ynnn
\'
anoc+poq
\"
kanrnka
\0
nynr-6an+ (0x00)
\\
o6pa+na koca nep+a
Char *
C+poka C npec+anne+ co6on npoc+on na6op cnmnonon, pacnonoennrx n
cmenon o6nac+n nam+n. Konen c+pokn o6osnanae+c cnmnonom 0x00.
Pnc. 34.1. C+poka C co cnonom Love
dynknnn, ko+oprm nepeam+c c+pokn C, paccnn+rnam+ nonynn+r apec nepnoro
cnmnona c+pokn. Hanpnmep, qynknn 2&(-/+7> cnn+ae+ konnnec+no cnmnonon n c+poke.
Honpo6yn+e cosa+r c+poky n ncnonrsona+r 2&(-/+7> n nocne+a cnmnonon:
)*+,-.$/ 12&$*#345 @@ `#( A(*+&B
4J2iaw1 Q3wi4/"R FF SI TPiiS2FKI11
4J2iaw1 Q3I4Jh"R FF SI Q3Ii1J
Fnana 34. C1poku C ______________________________________________________________________________________
242
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
,4'( d k FH @@ Z4/ ,4'(',&/( F
L4*-/ 7d 1k > ? @@ Z4/ ,4'(',&/(
A(*+&B7Cd *2 ,G+C9 d9 d>H
dllH
J
FF @;Z?\<+&< ?*)_)(<ZE +) s :)f(@)AE(& > *?\<
2RPI XQ3PI3 j TPiiS2LsVW

FF > @<9>%f :)f( _)@&B%>)<(BE gg
XQ3PI3 j ggW
FF >; >(;9;f :)f( _)@&B%>)<(BE gSg
XLQ3PI3 t !V j gSgW
FF > (9<(&f :)f( )@&B%>)<(BE g5g
XLQ3PI3 t CV j g5gW
FF > \<(><9(%f :)f( _)@&B%>)<(BE g1g
XLQ3PI3 t cV j g1gW
FF > @E(%f :)f( _)@&B%>)<(BE +?Z]
XLQ3PI3 t kV j gUbgW
FF >%>;l B(9;*& & << lZ&+%
HI4J3KLMrQ RPQ ra 2RPIP231IQUJM` Q3PI3` Q3Ii1JLQ3PI3VVW
FF >%>;l (9<(]<f :?*>%
HI4J3KLMoR1 3R4Iw i1331I 4Q r2UJM` XLQ3PI3 t CVVW
FF ;B>;:;=l<+&< @)AE(& lZE @;>(;9+;,; &B@;Z]_;>)+&E
KI11LQ3PI3VW
Q3PI3 j W
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e npo rpammy.
O6pa+n+e nnnmanne na mec+a c cymmnponannem ykasa+en n nncna. Hepemenna
2&'(& o6rnne+c c +nnom ,4'( :. Tnn ,4'( sannmae+ onn 6an+. Cneona+enrno,
snanenne 2&'(&lX npec+anne+ co6on ykasa+enr, cmemennrn na 1 6an+ n nam+n no
o+nomennm k 2&'(&; 2&'(&lV - na 2 6an+a n +. .
Pnc. 34.2. Apeca cnmnonon c+pokn
Hpn6annenne k ykasa+enm n pasrmenonanne pesynr+a+a - onepann nac+onrko
pacnpoc+panenna, n+o n nee 6rno cosano cnennanrnoe cokpamenne: sanncr
Fnana 34. C1poku C ______________________________________________________________________________________
243
2&'(&<V= .knnnanen+na :72&'(&l V>. Bnecn+e nsmenenn n cnon ko:
,4'( :2&'(& k 6'--#,7T>H
Q3PI3b j ggW
Q3PI3! j gSgW
Q3PI3C j g5gW
Q3PI3c j g1gW
Q3PI3k j gUbgW
A(*+&B7C2 4'2 . ,4'(',&/(2G+C9 2&'(&9 2&(-/+72&'(&>>H A(*+&B7CZ4/ &4*($
-/&&/( *2 ,G+C9 Q3PI3C>H
B(//72&'(&>H
2&'(& k PQccH
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy.
Cneye+ o+me+n+r, n+o .+o+ cnn+akcnc pa6o+ae+ c nm6rmn +nnamn annrx.
Hanpnmep, mory cosa+r cnncok +pex nemec+nennrx nncen n nrnec+n nx:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
@@ qrpjtjuwj tgo svnw9 {j{gq ogng{g|g pgfnngju
@@ ptv {ujuwv n{j ujuw nws B-#'&
B-#'& :B';#(*&/2 k 6'--#,7S : 2*/#B7B-#'&>>H
@@ujfjuwj ujuw q vjow xj{
B';#(*&/2<I= k S3X]XTWH
B';#(*&/2<X= k V3YXWVWH
B';#(*&/2<V= k X3]X]VXH
@@ qrqgp qfj wfjt q fswfoj
B#( 7*+& * k IH * 1 SH *ll> ?
A(*+&B7C3]B *2 B';#(*&/ $G+C9 B';#(*&/2<*=9 *>H
J
@@ gfqggypjuwj svnw ptv sgqng{ug|g wfsgtgquwv
B(//7B';#(*&/2>H
B';#(*&/2 k PQccH
(/&.(+ IH J
Ennc+nennoe nn+epecnoe pasnnnne saknmnae+c n +om, n+o nepemenna
B';#(*&/2 o6rnnena c +nnom B-#'& :. Tnn oat sannmae+ 4 6an+a. Cneona+enrno,
pesynr+a+ B';#(*&/2lX cmemen na 4 6an+a n nam+n no o+nomennm k B';#(*&/2.
Cvpokonme nnvepanm
Ecnn nam npnxon+c mnoro pa6o+a+r co c+pokamn C, nren+r nam+r nrsonom 6'--#,
n nocneona+enrno sanocn+r cnmnonr 6rno 6r kpanne neyo6no. Bmec+o .+oro nr
cosae+e ykasa+enr na c+poky cnmnonon (sanepmennym nynr-cnmnonom), saknmna
Fnana 34. C1poku C ______________________________________________________________________________________
244
c+poky n kanrnkn. Hsmenn+e cnon ko +ak, n+o6r n nem ncnonrsonanc c+pokonrn
nn+epan:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
,4'( d k FH @@ Z4/ ,4'(',&/( F
L4*-/ 7d 1k > ? @@ Z4/ ,4'(',&/(
A(*+&B7Cd *2 ,G+C9 d9 d>H
dllH
J
2RPI XQ3PI3 j MS51MW
A(*+&B7C2 4'2 . ,4'(',&/(2G+C9 2&'(&9 2&(-/+72&'(&>>H
A(*+&B7CZ4/ &4*($ -/&&/( *2 ,G+C9 2&'(&<V=>H
(/&.(+ IH
J
Hoc+pon+e n sanyc+n+e nporpammy
n c+pokonrx nn+epanon ne nyno nno nren+r nam+r qynknnen 6'--#, n
ocno6oa+r ee. 3+o konc+an+r, ko+opre pasmemam+c n nam+n +onrko onn pas, n
ynpannenne nam+rm o6ecnennnae+c komnnn+opom. Y konc+an+noc+n ec+r
no6onnrn .qqek+: nonr+ka nsmenenn cnmnonon c+pokn npnnee+ k nenpn+noc+m.
o6annenne cneymmen c+pokn nrsone+ c6on nporpammr:
,4'( :2&'(& k Cc#;/CH
Q3PI3C j ggW
A(*+&B7C2 4'2 . ,4'(',&/(2G+C9 2&'(&9 2&(-/+72&'(&>>H
Hpn nonr+ke noc+pon+r n sanyc+n+r nporpammy nrae+c coo6menne o6 omn6ke
EXC_BAD_ACCESS. Br nonr+anncr nrnonnn+r sanncr n nam+r, n ko+opym sanncr
sanpemena.
H+o6r komnnn+op npeynpean o nonr+kax sanncn n nensmenemre o6nac+n
nam+n, nocnonrsyn+ecr monqnka+opom ,#+2& - on osnanae+, n+o annre, na ko+opre
ccrnae+c ykasa+enr, ne onnr nsmen+rc. Honpo6yn+e:
2SJQ3 ,4'( :2&'(& k Cc#;/CH
2&'(&<V= k H
A(*+&B7C2 4'2 . ,4'(',&/(2G+C9 2&'(&9 2&(-/+72&'(&>>H
Tenepr npn noc+poennn nporpammr komnnn+op nrae+ coo6menne o6 omn6ke.
Yann+e npo6nemnym c+poky 72&'(&<V= k H>, npee nem npoona+r.
Br +ake moe+e nponyc+n+r c+pokn ynomny+re nrme. Honpo6yn+e:
2SJQ3 2RPI XQ3PI3 j M /P2pQiPQR PK31I 3OS J1Oi4J1Q PJw P 3P/UJUJU3UUMW
A(*+&B7C2 4'2 . ,4'(',&/(2G+C9 2&'(&9 2&(-/+72&'(&>>H A(*+&B7CZ4/ &4*($
-/&&/( *2 Ug,UgG+C9 2&'(&<V=>H
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 34. C1poku C ______________________________________________________________________________________
245
Hpeo6pasonannn k NSString n o6pavno
Ecnn nr pa6o+ae+e co c+pokamn C n nporpamme Objective-C, nr onnr yme+r
cosana+r P%%&(*+8 ns c+pok C. n .+oro n knacce P%%&(*+8 nmee+c cnennanrnrn
me+o:
ptg999
Chapter 34C Strngs
252
chaJ *gJee11hg = "heo!",
hSS1J1hg *x = |hSS1J1hg s1J1hgW11hCS1J1hg.gJee11hg ehcod1hg.hSbTF8S1J1hgEhcod1hg],
You can also get a C string from an N551rng. This is a little trickier because N551rng can handle
some characters that certain encodings cannot. It is a good idea to check that the conversion can occur:
hSS1J1hg *gJee11hg = "heo!",
cohs1 chaJ *x = hbLL,
11 {|gJee11hg cahBeCohveJ1edToEhcod1hg.hSbTF8S1J1hgEhcod1hg]) {
x = |gJee11hg cS1J1hgbs1hgEhcod1hg.hSbTF8S1J1hgEhcod1hg],
}
You do not own the resulting C string; the system will eventually free it for you. You are guaranteed
that it will live at least as long as the current autorelease pool, but if you are going to need the C string
to live for a long time, you should copy it into a buffer you`ve created with ma11oc{).

Write a function called spaceCoun1{) that counts the space characters (ASCII 0x20) in a C string. Test
it like this:
#1hcude <s1d1o.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
cohs1 chaJ *seh1ehce = "he was ho1 1h 1he cab a1 1he 11me.",
pJ1h11{"\"s\" has d spaces\h", seh1ehce, spaceCouh1{seh1ehce)),
Je1uJh 0,
}
Remember: when you run into `\0`, you have reached the end of the string!
Take nosmona n o6pa+na onepann - cosanne c+pokn C na 6ase P%%&(*+8.
Cena+r .+o nemnoro cnonee, no+omy n+o P%%&(*+8 moe+ pa6o+a+r c neko+oprmn
cnmnonamn, ne noepnnaemrmn pyrnmn konponkamn. Xena+enrno cnanana
y6en+rc n +om, n+o npeo6pasonanne nosmono:
ptg999
Chapter 34C Strngs
252
chaJ *gJee11hg = "heo!",
hSS1J1hg *x = |hSS1J1hg s1J1hgW11hCS1J1hg.gJee11hg ehcod1hg.hSbTF8S1J1hgEhcod1hg],
You can also get a C string from an N551rng. This is a little trickier because N551rng can handle
some characters that certain encodings cannot. It is a good idea to check that the conversion can occur:
hSS1J1hg *gJee11hg = "heo!",
cohs1 chaJ *x = hbLL,
11 {|gJee11hg cahBeCohveJ1edToEhcod1hg.hSbTF8S1J1hgEhcod1hg]) {
x = |gJee11hg cS1J1hgbs1hgEhcod1hg.hSbTF8S1J1hgEhcod1hg],
}
You do not own the resulting C string; the system will eventually free it for you. You are guaranteed
that it will live at least as long as the current autorelease pool, but if you are going to need the C string
to live for a long time, you should copy it into a buffer you`ve created with ma11oc{).

Write a function called spaceCoun1{) that counts the space characters (ASCII 0x20) in a C string. Test
it like this:
#1hcude <s1d1o.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
cohs1 chaJ *seh1ehce = "he was ho1 1h 1he cab a1 1he 11me.",
pJ1h11{"\"s\" has d spaces\h", seh1ehce, spaceCouh1{seh1ehce)),
Je1uJh 0,
}
Remember: when you run into `\0`, you have reached the end of the string!
Br ne nne+ecr nnaenrnem cosannon c+pokn C; cnc+ema n konennom n+ore
ocno6on+ ee sa nac. Fapan+nponano, n+o c+poka 6ye+ cymec+nona+r no kpannen mepe o
+ex nop, noka cymec+nye+ +ekymnn nyn autorelease; ecnn c+poka C onna
cymec+nona+r 6onee onroe npem, ckonnpyn+e ee n 6yqep, cosannrn nrsonom
6--gf7>.
npawnenne
Hannmn+e qynknnm c nmenem 2A',/^#.+&7> n nocne+a npo6enon (ASCII 0x20) n
c+poke C. Hponepr+e pa6o+y qynknnn cneymmnm o6pasom:
ptg999
Chapter 34C Strngs
252
chaJ *gJee11hg = "heo!",
hSS1J1hg *x = |hSS1J1hg s1J1hgW11hCS1J1hg.gJee11hg ehcod1hg.hSbTF8S1J1hgEhcod1hg],
You can also get a C string from an N551rng. This is a little trickier because N551rng can handle
some characters that certain encodings cannot. It is a good idea to check that the conversion can occur:
hSS1J1hg *gJee11hg = "heo!",
cohs1 chaJ *x = hbLL,
11 {|gJee11hg cahBeCohveJ1edToEhcod1hg.hSbTF8S1J1hgEhcod1hg]) {
x = |gJee11hg cS1J1hgbs1hgEhcod1hg.hSbTF8S1J1hgEhcod1hg],
}
You do not own the resulting C string; the system will eventually free it for you. You are guaranteed
that it will live at least as long as the current autorelease pool, but if you are going to need the C string
to live for a long time, you should copy it into a buffer you`ve created with ma11oc{).

Write a function called spaceCoun1{) that counts the space characters (ASCII 0x20) in a C string. Test
it like this:
#1hcude <s1d1o.h>
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
cohs1 chaJ *seh1ehce = "he was ho1 1h 1he cab a1 1he 11me.",
pJ1h11{"\"s\" has d spaces\h", seh1ehce, spaceCouh1{seh1ehce)),
Je1uJh 0,
}
Remember: when you run into `\0`, you have reached the end of the string!
H nomnn+e: o6napyenne cnmnona GI osnanae+, n+o nr o6panncr o konna c+pokn!
Fnana 34. C1poku C ______________________________________________________________________________________
246
cs" )BB&>% -
B nperymen rnane mr pa6o+ann co c+pokamn C. Kak nr ysnann, c+poka C
npec+anne+ co6on nocneona+enrnoc+r cnmnonon, pacnonoennrx pom pyr c
pyrom n nam+n. Maccnnr C +ake npec+annm+ co6on nocneona+enrnoc+n pyrnx
+nnon annrx, pacnonoennrx pom pyr c pyrom n nam+n. Kak n n cnynae co
c+pokamn, onepannn co cnnckom 6asnpym+c na apece nepnoro .nemen+a.
onyc+nm, nam no+pe6onanocr nannca+r nporpammy n nrnncnenn cpenero
apnqme+nneckoro +pex onenok. Cosan+e npo rpammy komannon c+pokn C c nmenem
8('$/K+Z4/%4'$/.
O+peak+npyn+e qann main.c:
)*+,-.$/ 12&$*#345
4J2iaw1 Q3wi4/"R @@ 6'--#,7>9 B(//7>
KiSP3 P51IPh1iSP3QLKiSP3 XwP3P` 4J3 wP3P7SaJ3V
z
KiSP3 QaT j b"bW
KSI L4J3 4 j bW 4 wP3P7SaJ3W 4ttV z
QaT j QaT t wP3P4W
|
I13aIJ QaT F wP3P7SaJ3W
|
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
FFB;_l)+&< A)BB&>) _+)\<+&f KiSP3Q
KiSP3 XhIPw1~SSp j TPiiS2Lc X Q41SKLKiSP3VVW
hIPw1~SSpb j b"CW
hIPw1~SSp! j k"sW
hIPw1~SSpC j !"!W
FF >%\<BZ<+&< B9<l+<,;
KiSP3 P51IPh1 j P51IPh1iSP3QLhIPw1~SSp` cVW
FF ;B>;:;=l<+&< A)BB&>)
KI11LhIPw1~SSpVW
hIPw1~SSp j W
HI4J3KLM51IPh1 j r"CKUJM` P51IPh1VW
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy
Fnana 35. Maccunm C ______________________________________________________________________________________
247
Pnc. 35.1. Ykasa+enn na 6yqep snanennn oat
dynknn 6'--#,7> nrene+ 6yqep n kyne, no.+omy nr onnr npocnen+r sa
+em, n+o6r on 6rn ocno6oen nocne sanepmenn pa6o+r. A pasne ne yo6nee 6rno 6r
o6rnn+r 6yqep kak nac+r kapa (n c+eke), n+o6r on an+oma+nneckn ocno6oanc
nocne sanepmenn nrnonnenn qynknnn? a, .+o nosmono.
Bnecn+e nsmenenn n main.c:
*6A#(& 12&$*#345
KiSP3 P51IPh1iSP3QLKiSP3 XwP3P` 4J3 wP3P7SaJ3V
z
KiSP3 QaT j b"bW
KSI L4J3 4 j bW 4 wP3P7SaJ3W 4ttV z
QaT j QaT t wP3P4W
|
I13aIJ QaT F wP3P7SaJ3W
|
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
FF ;:eE>Z<+&< A)BB&>) *)* \)B(& *)l9)
KiSP3 hIPw1~SSpcW
8('$/a##h<I= k RI3VH
8('$/a##h<X= k ]3TH
8('$/a##h<V= k WX3XH
@@ qrwftjuwj f{jpuj|g
B-#'& ';/('8/ k ';/('8/`-#'&278('$/a##h9 S>H
FF ;B>;:;=l)(] A)BB&> +< +?=+;
FF ?+&\(;=<+&< @9;&_>;l&(BE )>(;A)(&\<B*& @9& >;_>9)<+&& ?@9)>Z<+&E
m?+*n&<f
A(*+&B7C!;/('8/ k 3VBG+C9 ';/('8/>H
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy.
C+pokonrn nn+epan ynpomae+ sanonnenne maccnna cnmnonamn. Take
cymec+nym+ cnennanrnre nn+epanr maccnnon. Hpnmep ncnonrsonann +akoro nn+epana
n nnnnnannsannn 8('$/a##h:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( :'(8;<=>
?
KiSP3 hIPw1~SSp j zb"C` k"s` !"!|W
B-#'& ';/('8/ k ';/('8/`-#'&278('$/a##h9 S>H
Fnana 35. Maccunm C ______________________________________________________________________________________
248
A(*+&B7C!;/('8/ k 3VBC9 ';/('8/>H
(/&.(+ IH J
Hoc+pon+e n sanyc+n+e nporpammy.
O6pa+n+e nnnmanne: ykasrna+r, n+o nnna 8('$/a##h panna 3, ne o6sa+enrno;
komnnn+op cam nrnncne+ ee no nn+epany maccnna. 3+o+ +nn moe+ ncnonrsona+rc no
mnornx mec+ax, n ko+oprx o6rnno ncnonrsye+c *. Hanpnmep, nsmenn+e o6rnnenne
';/('8/`-#'&27> cneymmnm o6pasom:
B-#'& ';/('8/`-#'&27KiSP3 wP3P9 *+& $'&'^#.+&> ?
B-#'& 2.6 k I3IH
B#( 7*+& * k IH * 1 $'&'^#.+&H *ll> ?
2.6 k 2.6 l $'&'<*=H
J
(/&.(+ 2.6 @ $'&'^#.+&H
J
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 35. Maccunm C ______________________________________________________________________________________
249
c" 9,?A<+(% *;A)+l+;f
B(9;*&
Hpn nrsone 6'*+7> +oe nepeam+c aprymen+r, ko+opre +ak c+apa+enrno o6xonn
nnnmannem:
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
333
Ho +enepr nr ro+onr k snakomc+ny c nnmn. '(8; - maccnn c+pok C; '(8,
coo6mae+ konnnec+no c+pok n maccnne. H+o npec+annm+ .nemen+r maccnna?
Aprymen+r komannon c+pokn.
Hporpammr komannon c+pokn, ko+opre mr cosanann, sanyckanncr ns Terminal-
npnnoenn, ko+opoe ncero nnmr npeoc+anne+ yo6nrn nn+epqenc k +ak
nasrnaemomy /'3)","'36 0&'7$++'&6 (shell). Cymec+nye+ neckonrko pasnrx
komannrx nponeccopon c noxonmn nmenamn: csh, sh, zsh n ksh, no non+n nce
nonrsona+enn Mac ncnonrsym+ bash. Hpn sanycke nporpammr ns bash nocne nmenn
nporpammr mono saa+r nm6oe konnnec+no aprymen+on, pasenennrx npo6enamn. 3+n
aprymen+r ynakonrnam+c n maccnn argv nepe nrsonom main ().
Hec+no ronop, nporpammnc+r Cocoa n iOS peko ncnonrsym+ '(8; n '(8,. Tem
ne menee n nanncann nm6on ncnomora+enrnon nporpammr komannon c+pokn nam
non+n nanepnka npne+c nonrsona+rc nmn.
Cosan+e n Xcode nonrn npoek+ nporpammr komannon c+pokn C c nmenem
!BB*(6'&*#+. Hporpamma !BB*(6'&*#+ nonynae+ na aprymen+a: nm nenoneka n nncno
n, n n pas nrnon+ coo6menne c saannrm nmenem.
KK4ITP34SJ }4p1q c *h/_ *2 ,##-3
*h/_ *2 ,##-3
*h/_ *2 ,##-3
Hpee nem pema+r ocnonnym saany, nsmenn+e qynknnm 6'*+7> +ak, n+o6r ona
nrnonna karn ns aprymen+on n (8;:
)*+,-.$/ 12&$*#345
*+& 6'*+ 7*+& '(8,9 ,#+2& ,4'( : '(8;<=>
?
KSI L4J3 4 j bW 4 PIh2W 4ttV z
HI4J3KLMPIh rw j rQUJM` 4` PIh54VW
|
(/&.(+ IH J
Fnana 36. Apryueu1m kouaupuo c1poku ______________________________________________________________________________________
250
Ecnn nporpamma sanyckae+c ns M'24, aprymen+r mono npoc+o nnec+n n
komannon c+poke:
KK4ITP34SJ PISJ k
Pnc. 36.1, o6annenne aprymen+on
Ho n nrnonnenn nporpammr c aprymen+amn n Xcode neo6xonmo
npenapn+enrno o+peak+npona+r cxemy. Br6epn+e n menm Product komany
EditScheme...
B o+kprnmemc okne nr6epn+e cnena komany Run Afrmation n nepenn+e na
nknaky Arguments n nepxnen nac+n okna. Hann+e cnncok Arguments Passed On Launch
n npn nomomn knonkn + o6anr+e na aprymen+a: nm n nomep.
Eenknn+e na knonke OK,n+o6r sakpr+r okno.
Hpn sanycke nporpammr n maccnne '(8; nepeae+c cnncok c+pok. Hannnammnx
paspa6o+nnkon o6rnno 6onrme ncero ynnne+ '(8;<I=:
'(8 I k @Q2/(2@''(#+@c*M('(_@O/;/-#A/(@,#$/@O/(*;/$O'&'@
!BB*(6'&*#+U/+hB28';B2A(#/88#dLM(6,#L;+@a.*-$@b(#$.,&2@O/M.8@!BB*(6'&*#+
'(8 X k !'(#+
'(8 V k ]
3nemen+ '(8;<I= coepn+ ny+r k ncnonnemomy qanny nporpammr.
Pnc. 36.2. argv n argc n nporpamme Afrmation
Fnana 36. Apryueu1m kouaupuo c1poku ______________________________________________________________________________________
251
Ecnn nporpamma nonynae+ aprymen+r, npee ncero neo6xonmo y6en+rc n +om,
n+o en nepeano npannnrnoe konnnec+no aprymen+on. Bnecn+e nsmenenn n main.m:
ptg999
259
Fgure 36.2argv and argc n Affrmaton
If your program takes arguments, the first thing you should do is make sure that the number of
arguments is correct. Edit ma1h.m:
#1hcude <s1d1o.h>
#nc1ude <s1d1b.h> // a1o1{)
1h1 ma1h {1h1 aJgc, cohs1 chaJ * aJgv|])
{
1 {argc != 3) {
1prn11{s1derr, "usage: A11rma1on <name> <number>\n")
re1urn 1
)
n1 coun1 = a1o{argv|2))
1or {n1 ] = 6 ] < coun1 ]++) {
prn11{"s s coo1.\n", argv|1))
)
Je1uJh 0,
}
a1o{) is a standard C function that reads a C string and tries to make an 1h1 out of it.
Build and run the program.
'&#* () - c+anap+na qynknn C, ko+opa nn+ae+ c+poky C n nr+ae+c
npeo6pasona+r ee n nncno +nna *+&.
Hoc+pon+e n sanyc+n+e nporpammy.
Fnana 36. Apryueu1m kouaupuo c1poku ______________________________________________________________________________________
252
c" 8;A)+l) QO432R
B nporpammax ononrno nac+o nc+penae+c cn+yann, kora nepemennym +pe6ye+c
nponepn+r no na6opy snanennn. C ncnonrsonannem koman if-else nponepka 6ye+
nrrne+r +ak:
*+& _/'2&Z_A/ k 333H
*B 7_/'2&Z_A/ kk X> ?
6'h/a(/'$7>H
J /-2/ *B 7_/'2&Z_A/ kk V> ?
6'h/a//(7>H
J /-2/ *B 7_/'2&Z_A/ kk S> ?
6'h/E*+/7>H
J /-2/ ?
6'h/`./-7>H
J
n ynpomenn noo6nrx nponepok n C nmee+c komana 2L*&,4. Hpnneennrn
ko mono samenn+r cneymmnm:
*+& _/'2&Z_A/ k 333H
2L*&,4 7_/'2&Z_A/> ?
,'2/ X\
6'h/a(/'$7>H
M(/'hH
,'2/ V\
6'h/a//(7>H
M(/'hH
,'2/ S\
6'h/E*+/7>H
M(/'hH
$/B'.-&\
6'h/`./-7>H
M(/'hH J
O6pa+n+e nnnmanne na komanr M(/'h. Bes nnx nocne nrnonnenn noxomen
ceknnn ,'2/ cnc+ema npoonn+ nrnonnenne ncex nocneymmnx ceknnn ,'2/.
Hanpnmep, ecnn konc+pyknn 2L*&,4 sanncana cneymmnm o6pasom:
*+& _/'2&Z_A/ k VH
2L*&,4 7_/'2&Z_A/> ?
,'2/ X\
6'h/a(/'$7>H
,'2/ V\
6'h/a//(7>H
,'2/ S\
6'h/E*+/7>H
Fnana 37. Kouaupa switch ______________________________________________________________________________________
253
$/B'.-&\
6'h/`./-7>H
J
nporpamma nrnonnn+ 6'h/a//(7>, 6'h/E*+/7> n 6'h/`./-7>. B ocnonnom .+o cenano
n +oro, n+o6r onn ko mor nrnonn+rc n neckonrknx nosmonrx snanennn:
*+& _/'2&Z_A/ k 333H
2L*&,4 7_/'2&Z_A/> ?
,'2/ X\
,'2/ ]\
6'h/a(/'$7>H
M(/'hH
,'2/ V\
,'2/ T\
6'h/a//(7>H
M(/'hH
,'2/ S\
6'h/E*+/7>H
M(/'hH
$/B'.-&\
6'h/`./-7>H
M(/'hH J
Kak ne+pyno npec+ann+r, o+cy+c+nne M(/'h n konne ceknnn ,'2/ -
pacnpoc+panenna omn6ka nporpammnponann, ko+opa o6napynnae+c +onrko npn
c+pannom noneennn nporpammr.
B C n koman 2L*&,4 yc+anonneno ono ec+koe orpannnenne: snanenne n
ceknnx ,'2/ moe+ 6r+r +onrko nenonncnennon konc+an+on. Ho .+on npnnnne
komanr 2L*&,4 o+nocn+enrno peko nc+penam+c n nporpammax Objective-C, a
onncan .+y konc+pyknnm non+n n camom konne knnrn.
Fnana 37. Kouaupa switch ______________________________________________________________________________________
254