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

Become an Xcoder

Mac OS X
Objective-C
Bert Altenberg, Alex Clarke, Philippe Mougin



Copyright 2008 Bert Altenburg, Alex Clarke Philippe Mougin. 1.15
Creative Commons 3.0

http://creativecommons.org/licenses/by/3.0/
: Bert Altenburg, Alex Clarke Philippe Mougin
, .
.
: , ,
.
, ,
.

CocoaLab
CocoaLab Wiki pdf
, http://www.cocoalab.com.

6
6

1:

2: ? , !


?

3:

8
8
9
10
10
12
12
12
13
13
14
14

16
16
16
17

18

main()




18
19
20
21
22
23

4:

24

NSLog

24




Foundation

5:

Xcode
Build and Go

6:
if()
if() ( )

7:
for()
while()

25
27
27
27

29
29
31
33
33
35
36

39
39
39
40

42
42
43

8: (GUI)45


GUI



Interface Builder-



Interface Builder

46
46
47
47
47
48
49
49
50
51
51
52
53
55

9:

57
59

60
61

10: awakeFromNib

64

11:

66

12:

66
67

69

NSString
NSMutableString
.

69
70
72

13:

74

74

14:

15:


:


16:

78
78
79
79
80

82
82
82
82
83
83
83

84
85


Apple ,
Cocoa . Xcode,
Mac OS X. -
Apple.
Mac,
, . .
, Objective-C, Xcode.
5
(GUI).
, GUI.
, .
, .. .
.


, :
, .
( ) .
.
. , ,
, .
, .
,
. ,
, , [1]. .
, . , [1.2]
[1]. ,
, :
//[1]
volume = baseArea * height; // [1.2]

.
.
.
. ,
Xcode. 5,
. ,
, .


. , ,
Mac.
Macintosh
. .
Mac ,
. ,
Mac. , Objective-C AppleScript,
. AppleScript
.
, PC, Macintosh.
Mac, Mac, .
(
), , Mac
. (),
Folding@home SETI@home, ,
.

.
. Mac,
, .
, , . Mac? . Mac,
.
, Mac.
: , .. :
http://distributedcomputing.info/projects.html
, !
Macintosh .
. ,
. - ,
.
, .
, . Macintosh ,
.
, Macintosh,
, .
, , !

1:

, .
, .
, .
, .
Xcode, . ,
,
. Objective-C,
.
, :
2+6=
= 3 * 4 ( *
)
, x
y ( ). , :
?
2+6=x
y=3*4

Objective-C .
, .
Objective-C, ,
.
//[1]
x = 4;


x 4. C .
. ? , , [1],
, .
, ,
, Mac. ,
. .
, , ..
, Mac. ,

, .
, , .


,

. .
(. bugs).
.

x. ,
pictureWidth [2].
//[2]
pictureWidth = 8;

, ,
. ,
(case-sensitive). :
pictureWidth, pictureWIDTH PictureWidth .
, , ,
, [2].
camelCase. ,
, .
, (
).
, ,
, .
, ,
Objective-C.
, , pictureWidth,
. ,
, .
, . ,
, .
_.
.

10

:
door8k
do8or
do_or

:
door 8 ( )
8door ( )

:
Door8 ( )


, , ,
. [3],
.
//[3]
pictureWidth=8;
pictureHeight=6;
pictureSurfaceArea=pictureWidth*pictureHeight;

, (
, ..) .
//[4]
pictureWidth = 8;
pictureHeight = 6;
pictureSurfaceArea = pictureWidth * pictureHeight;


[5], .
//[5]
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;

: . [5.2] [5.3]
. ,
(. 7).
,
.
[5] . ,

11

, .

//[6]
int pictureWidth;
float pictureHeight, pictureSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;

[6.2] int , pictureWidth (integer).


, ,
. , float,
. , , pictureWidth
, int float
float. pictureSurfaceArea
float [6.3].
,
?
, .
() .
, int float,
,
.
,
? ,
, ? : -,
, , (
). long double, .
, : (
) . , ,
.
, , ,
. , ,
, .
//[7]
unsigned int chocolateBarsInStock;

, , ,
unsigned int . unsigned int
.

12


.
//[8]
int x = 10;
float y= 3.5, z = 42;


, ,
, .
Objective-C, ,
. Objective-C:

void

int

-1, 0, 1, 2

unsigned

0, 1, 2

float

-0.333, 0.5, 1.23

double

0.5252525233323409

char

a, b, c

BOOL

0, 1, TRUE, FALSE, YES, NO


, .
, , .
+
/
*

, .
Objective-C , ,
, .
x = x + 1; - , [9]
[10].

13
//[9]
x++;
//[10]
++x;

: . ,
++ . [11] [12].
//[11]
x = 10;
y = 2 * (x++);
//[12]
x = 10;
y = 2 * (++x);

[11], , y = 20, x = 11. ,


[12.2], x 2. ,
, = 11, y = 22. [12] [13] .
//[13]
x = 10;
x++;
y = 2 * x;

, . ,
. , ,
, .

, ,
, . * /
+ . 2 * 3 + 4 = 10. ,
: 2 * (3 + 4) = 14.

,
,
. [14, 15].
//[14]
int x =
ratio =
//[15]
float x
ratio =

5, y = 12, ratio;
y / x;
= 5, y = 12, ratio;
y / x;

[14], 2. [15],
, , , : 2.4.

14


true false. 1(YES)
0(NO) true false
, :
TRUE

FALSE

YES

NO

,
.


, , % ( ). ,
:
. %
( , %
).
//[16]
int x = 13, y = 5, remainder;
remainder = x % y;

remainder 3, x 2*y + 3.
:
21
22
23
24
27
30
31
32
33
34
50
60

%
%
%
%
%
%
%
%
%
%
%
%

7 0
7 1
7 2
7 3
7 6
2 0
2 1
2 0
2 1
2 0
9 5
29 2

,
.

15

, . ,
. . :
//[17]
int anInt;
// , anInt
if ((anInt % 2) == 0)
{
NSLog(@"");
}
else
{
NSLog(@"");
}

2: ? ,
!
,
[18].
//[18]
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;

,
.
, , ,
. .
, , .
, .
.
- .
, - . ,
-,
.


, .
//

Xcode .
, /* */.
/*
*/


, Xcode
. outcommenting.
/* */, , ,
. . outcommented
, , ,
,
.

17

?
. (
) ,
. , ,
, .
, ,
. ,
, ,
, .
.
, , .
, ,
.

3:
, , 5 .
,
Objective-C .
, ,
.
,
. ,
. : ( )! ,
, .
,
. , ,
. , ,
, . , , ,
.
, : main().
, .

main()
main() [19].
//[19]
main()
{
// main(). .
}

[19.2] , .. main,
. main ,
main() , ,
, . ,
. [19.3, 19.5] .
{}. ,
, . [20].

19
//[20]
main()
{
//
float pictureWidth, pictureHeight, pictureSurfaceArea;
// ( )
pictureWidth = 8.0;
pictureHeight = 4.5;
//
pictureSurfaceArea = pictureWidth * pictureHeight;
}


main(),
. .
. main()
circleArea() [21].
//[21]
main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
}
circleArea() // [21.10]
{
}

, [21.10] . ,
main(). .
circleArea() main(). ,
[22].

20
//[22]
main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea; // [22.5]
pictureWidth = 8.0;
pictureHeight = 4.5;
circleRadius = 5.0; // [22.8]
pictureSurfaceArea = pictureWidth * pictureHeight;
// !
circleSurfaceArea = circleArea(circleRadius); // [22.11]
}

: (. [21]).


[22.5]
circleRadius, .. [22.8]. [22.11],
circleArea(). ,
circleRadius .
circleArea(). circleRadius circleArea().
circleArea() , .
circleArea() [21], [23].
: circleArea().
//[23]
circleArea(float theRadius) // [23.2]
{
float theArea;
theArea = 3.14159 * theRadius * theRadius; // S = R2 [23.5]
return theArea;
}

[23.2] , circleArea()
. theRadius.
theArea [23.5],
[23.4] , main() [22.4].
C , theRadius [23.2].
[23.6] , .
[22.11] circleSurfaceArea .
[23] . ,
. ,
, [24.2].

21
//[24]
float circleArea(float theRadius) //[24.2]
{
float theArea;
theArea = 3.14159 * theRadius * theRadius;
return theArea;
}

[24.2] , , (..
theArea), . ,
circleSurfaceArea main() [22.5] ,
.
. , () ,
.
//[25]
int throwDice()
{
int noOfEyes;
// 1 6
return noOfEyes;
}


. ,
void. return . void,
return / .
//[26]
void beepXTimes(int x)
{
// x
return;
}

, ,
pictureSurfaceArea() , .
//[27]
float pictureSurfaceArea(float theWidth, float theHeight)
{
//
}

main(), , ,
return. 0 [28.9], ,
. .. main() ,
int main() [28.1]. ,
.

22
//[28]
int main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = pictureWidth * pictureHeight;
circleSurfaceArea = circleArea(circleRadius); // [28.10]
return 0; // [28.11]
}
float circleArea(float theRadius) // [28.14]
{
float theArea;
theArea = 3.14159 * theRadius * theRadius;
return theArea;
}


[28], main() [28.2] ,
[28.14]. ,
. [28.10] , circleArea(). ?
, main() ,
. . ,
main() [29.2].
, , [28.14],
. , .
//[29]
float circleArea(float theRadius); //
int main()
{
// main
}

: (. [28]).
. .

. rectangleArea(),
[30], main().
, . main()
. , .
, .

23

, , ,
, .
//[30]
float rectangleArea(float length, float width)
{
return (length * width);
}

, , ,
[30.3]. ,
theArea [28.16]
.
, , ,
,
. ,
(.. , ,
).
, ,
( ). -
, , .
, :

;
, ;
( ),
.
[30], :

rectangleArea;
2 , , ,
;

- float (
[30.2]).


(
)
,
, .
Objective-C. 5,
. , Xcode, [29].

4:
,
. Objective-C ,
, .
. Apple Cocoa
NSLog(). ,
- .
NSLog() ,
. - ,
.
Cocoa, .

NSLog
, NSLog().
//[31]
int main()
{
NSLog(@" .");
return 0;
}

[31] : .
.
@" " .
, NSLog ()
, . [31]
:
2005-12-22 17:39:23.084 test[399] - .

.
: ,
.
//[32]
NSLog(@"");
NSLog(@" ");

[32.2] 0 , (, ,
0). [32.3] , ,
. , 1.

25

.
-.
, ,
, [33.2] .
\n, .
//[33]
NSLog(@" \n.");

(
):

.

\ ( ) [33.2] -,
NSLog() , , ,
: n .
, \, ,
. ,
? ,
. NSLog(),
.
:
//[34]
NSLog(@" .\\n");

[34.2]
.\n


.
.
//[35]
int x, integerToDisplay;
x = 1;
integerToDisplay = 5 + x;
NSLog(@" %d.", integerToDisplay);

, , .
: %d. \, % .
d ( ), %d
, , .. integerToDisplay.
[35]
6

%f %d

26
//[36]
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@" %f.", floatToDisplay);

, . 2
,
.2 % f, :
//[37]
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@" 2 %.2f.",
floatToDisplay);

, , .
, , .
,
. ( )
% f ( % d)
, ,
.
//[38]
int x = 123456;
NSLog(@"%2d", x);
NSLog(@"%4d", x);
NSLog(@"%6d", x);
NSLog(@"%8d", x);

[38] :
123456
123456
123456
123456

, [38.3, 38.4]
, ,
. , [38,6] , ,
,
.
, ,
.

27
//[39]
float x=1234.5678;
NSLog(@" 10 , 2 .");
NSLog(@"%10.2d", x);


, , [40.4].
, (int, float), %d
%f .
//[40]
int x = 8;
float pi = 3.14159;
NSLog(@" %d, %f.", x, pi);



NSLog() .
, .
!
, ,
.
//[40b]
int x = 8;
float pi = 3.1416;
NSLog(@" %f, %f.", x, pi);
// : NSLog(@" %d,
%f.", x, pi);

:
0.000000, 0.000000.

Foundation
.
NSLog()? , ,
. , ,
, NSLog().
:
#import <Foundation/Foundation.h>

28

. ,
, , :
//[41]
#import <foundation/foundation.h>
float circleArea(float theRadius);
float rectangleArea(float width, float height);
int main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = rectangleArea(pictureWidth, pictureHeight);
circleSurfaceArea = circleArea(circleRadius);
NSLog(@" : %10.2f.", circleSurfaceArea);
NSLog(@" : %f. ", pictureSurfaceArea);
return 0;
}
float circleArea(float theRadius) //
{
float theArea;
theArea = 3.14159 * theRadius * theRadius;
return theArea;
}
float rectangleArea(float width, float height)
{
return width*height;
}

5:

, , . -
, Mac .
!
Mac , .
Xcode, ,
Mac OS X. , , ,
http://developer.apple.com
( ).


, , Xode, /Development/Applecation/
Spotlight xcode .
.
.
, (FileNewProjectN).
.
Objective-C,
(GUI). Foundation Tool
Command Line Utility.
, justatry. ,
( Save).
, , (Terminal).
, , ,
. , ,
, . ,
.

30

Xode .

31

Xcode
, , , .
.
. Groups & Files
, .
, , ,
.
, .
Xcode (Groups) .
Groups & Files, jystatray,
justatry.m [42]. ,
main()? , ,
main() . ,
. justatry.m, ,
. Apple main() .

Xcode, main() .

32
//[42]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) // [42.3]
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // [42.5]
// insert code here
NSLog(@"Hello, World!");
[pool drain]; //[42.9]
return 0;
}

, :

import, #,
NSLog;

main();
, ;
, ;
NSLog() ;
return 0.
, :

main() [42.3];
, NSAutoreleasePool [42.5];
pool drain [42.9].
- , , ,
, ,
. , .
,
.
, ,
main(), . , ,
, [42]. ,
([42.3, 42.5 42.9]).
Objective-C,
.
, (
).

33

- ,
:

main() [42.3] .
(. );

[42.5]. ,
, .
, ;

: , [42.9].
Build and Go
[42].
Build and Go () .

Build and Go

Console (
, Xcode, RunConsoleR),
. , 0.
0, main(), 3 [28.11].

. !

[42] , , .
, NSLog(),
.

34
//[43]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@" ") // ,
!
[pool drain]; //[43.9]
return 0;
}

, Build and Go .
[43.9] .

Xcode .

, :
error: syntax error before "drain"

, :
, .
, . , import
#, c (;). ,
[43.9], , - . ,
, , ,
( ,
. ). ,

35

, ,
(
).
, ,
.


,
Apple ([42]). [44].
//[44]
#import <Foundation/Foundation.h>
float circleArea(float theRadius); // [44.4]
float rectangleArea(float width, float height); // [44.5]
int main (int argc, const char * argv[]) // [44.7]
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int pictureWidth;
float pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = pictureWidth * pictureHeight;
circleSurfaceArea = circleArea(circleRadius);
NSLog(@" : %f. : %10.2f.",
pictureSurfaceArea, circleSurfaceArea);
[pool drain];
return 0;
}
float circleArea(float theRadius) // [44.24]
{
float theArea;
theArea = 3.14159 * theRadius * theRadius;
return theArea;
}
float rectangleArea(float width, float height) // [44.31]
{
return width *height;
}

, . [44.4, 44.5]
circleArea() [44.24] rectangleArea() [44.31],
main() [44.7], .

36

main() [44.8, 44,22].


main(), Apple.
, :
: 36.000000. :
The Debugger has exited with status 0.

78.54.

, .
, .
Xcode,
,
Xcode breakpoint,
.

, ,
.
Build and Go (Debug) (RunDebugY), Xcode
.

37

Xcode .

.
(Debugger.
RunDebuggerY), .
,
. Continue.
, ,
. ,
.
, ,
Mac OS X.
(GUI),
Objective-C,
.
. GUI.

38

6:
if()
,
, [45.3].
//[45]
// age ,
if (age > 30) // > " "
{
NSLog(@" 30 ."); //[45.5]
}
NSLog(@"."); //[45.7]

[45.3] if(), , .
, ,
, .
, [45.5] , age > 30 .
[45.7] , ,
.

if() ( )
,
, if else [46].
//[46]
// age ,
if (age > 30) //[46.3]
{
NSLog(@" 30 ."); //[46.5]
}
else
{
NSLog(@" 30 ."); //[46.9]
}
NSLog(@".");

[46.9] , [46.3]
.

40

, ,
:
==
>
<
>=
<=
!=

, .
.
, ,
, , . :
!

. .
,
.
, .
.
Objective-C 1 0 .
BOOL,
. 1 YES.
0 NO.
//[47]
int x = 3;
BOOL y;
y = (x == 4); // y 0.

. ,
, &&.
, ,
||.
//[48]
if ( (age >= 18) && (age < 65) )
{
NSLog(@" .");
}

41

, .
. , ,
, , :
//[49]
if (age >= 18)
{
if (age < 65)
{
NSLog(@" .");
}
}

7:

, ,
. , [50].
//[50]
NSLog(@" .");
NSLog(@" .");
NSLog(@" .");

.
. ,
Objective-C .

for()
( ),
, [51].
, , , 2.7 .
//[51]
int x;
for (x = 1; x <= 10; x++)
{
NSLog(@" - ."); //[51.5]
}
NSLog(@" x %d", x);

c [51.5] 10 . -, 1.
x <= 10.
(x 1), .
, x ,
x++. x, 2, 10. x
10, , .

x 11, x <= 10, .


[51.7] , ,
x = 11, 10, .

-, x++.
, . [52]
.

43
//[52]
float celsius, tempInFahrenheit;
for (tempInFahrenheit = 0; tempInFahrenheit <= 200;
tempInFahrenheit = tempInFahrenheit +20)
{
celsius = (tempInFahrenheit - 32.0) * 5.0 / 9.0;
NSLog(@"%6.2f %6.2f", tempInFahrenheit, celsius);
}

:
0.00
20.00
40.00
60.00
80.00
100.00
120.00
140.00
160.00
180.00
200.00

-17.78
-6.67

4.44
15.56
26.67
37.78
48.89
60.00
71.11
82.22
93.33

while()
Objective-C :
while () {}

do {} while ()

while () {} for, .
. ,
.
//[53]
int counter = 1;
while (counter <= 10)
{
NSLog(@" - .\n");
counter = counter + 1;
}
NSLog(@" %d", counter);

, 11,
.

44

do {} while (),
1 .
//[54]
int counter = 1;
do
{
NSLog(@" - .\n");
counter = counter + 1;
} while (counter <= 10);
NSLog(@" %d", counter);

11
,
.
(GUI).

8:
(GUI)
Objective-C,
(GUI). . Objective-C
C. ,
C. Objective-C C?
Objective . Objective-C ,
.
. , Objective-C
. ,
.
, (, ).
, , , : ,
, ..? ,
, , , ..? ,
Objective-C,
?
. C Objective-C,
, ,
.
, Objective-C,
Finder.
Finder Mac. , . .
, , ?
. , ,
.
. . .
. ,
. ,
Safari .
( ) ( ).

46

Safari, . ,
Apple:

,
, ;

Safari .
2. , ,
, .
, .
(, , 76
).

.
Dock, , .

47

? .
() .

.

, ,
. .
, .
. ,
,
.


, (RAM)
. ,
. ,
.
, .
, , ,
. ,
. .

GUI
, .
.
.
, -
. , , .
.
, .
, . ,
, ? ,
. ,
.

48


.
. , ,
,
. .
,
. (
). , .
?
. . ,
. ? ,
( ).
, .
, . ,

. (
). ,
, (),
.

49

, Objective-C,
[55.2], [55.3]:
//[55]
[receiver message];
[receiver messageWithArgument:theArgument];

, ,
, . ,
- , .
,
[55.3].


, . Xcode
. Cocoa Application ,
( , GUI
). Groups & Files Xcode
Resources. MainMenu.xib ( Xcode ,
GUI, nib ).


Interface Builder. ,
Hide Others (Interface BuilderHide OthersN).
. , Window, ,
. , , .
Library.

50

, GUI. Views & Cells


Buttons GUI
Window. Label.

GUI .

,
, .
,
.
, , ,
NSButton NSTextView. , Apple.
,
.
, Window , .
, .
. ,
,
.

Interface Builder-
,
( ToolsInspectorI). .
, Button (
xib). Button attributes,
. ,
Title. ,
, .

51


, . ,
.
, ,
,
. , , (
), , ,
, ,
. ,
.
Objective-C C.
? , (NSWindow),
.
- .
Close?
, .
, ,
,
(.. ). , ,
(
).
, .
, .
Objective-C .


, ,
? ,
. , ,
, , .
,
Apple. , ,
, Apple. ,
, .
, Apple.
-,
, ,
.
//[56]
// .
[super close]; // close

,
, .

52

, , ,
NSObject. , , -
, - NSObject, .
NSWindow NSResponder,
NSObject. NSObject
( ,
. .).
, , , .


Xcode (FileNew FileN).
Objective-C class , Next. .
MAFoo ( MAFoo.m ).
Finish.
MAFoo My Application.
, . ,
,
, .
NS, . NS
Apple. NeXTStep, NeXTStep ,
Mac OS X , Apple, Inc. NeXT
.
CocoaDev , .
:
http://www.cocoadev.com/index.pl?ChooseYourOwnPrefix
,
. , , Cocoa ,
NSWindow. ,
, NSColor. , MAFoo
,
. -
.

53

MAFoo

Interface Builder
Interface Builder, Library Objects &
Controllers . Object ( )
MainMenu.xib.
Identity (6) MAFoo
Class.
MAFoo Xcode xib-.
.

54

55



MAFoo. ,
MAFoo ,
. ,
. MAFoo, ,
MAFoo.
MAFoo.
.
, .
, .
MAFoo, .
MAFoo MAFoo, .
, ,
, . , , MAFoo,
MAFoo, -. ,
. , ,
. ,
, ( ,
?), , .
2 (action), .
(outlet), , , (..
) .
, MAFoo MainFile.xib. Identity
(6). Actions,
( +, ), (..,
) MAFoo. , Interface Builder
(, setTo5:
, 5 ). ,
( reset:, ,
0 ). ,
(:). .
Outlet, (outlet)
(, textField).
(actions) (outlets) MAFoo ,
, . ..
, MAFoo 5 ,
Set to 5 ( , :
, ).
, Reset. ,
. ,
.

56

"Reset" MAFoo;
"Set to 5" MAFoo;
MAFoo .
ctrl
Set to 5 MAFoo MainMenu.xib (
!). , , ,
. setTo5: .

ctrl (. ).

MAFoo, setTo5:
.
Reset MAFoo .
MAFoo MAFoo
ctrl . textField
.
? , ,
.

57


File Interface Builder' Write Class Files. Interface Builder
, .
MAFoo ,
.
, Xcode,
, Classes. Editor
MAFoo.h.

Xcode.

4, , .
[41.2]?
, ,
. , , MAFoo.h,
: . : ,
[57.5], NSObject, ,
NSObject.

58
//[57]
/* MAFoo */
#import <cocoa/cocoa.h> // [57.3]
@interface MAFoo : NSObject
{
IBOutlet id textField; // [57.7]
}
- (IBAction)reset:(id)sender;
- (IBAction)setTo5:(id)sender;
@end

, (IBOutlet) [57.7] . Id .
IB Interface Builder, .
IBAction [57.9, 57.10] void. , ,
: MAFoo

.
, 2 (action) Inteface Builder. 2
. , ,
. .
#import <Foundation/Foundation.h> [57.3],
GUI, #import <cocoa/cocoa.h> GUI .
, MAFoo.m. .
//[58]
#import "MAFoo.h"
@implementation MAFoo
- (IBAction)reset:(id)sender // [58.5]
{
}
- (IBAction)setTo5:(id)sender
{
}
@end

-, MAFoo.h, ,
. : Reset: setTo5:. .
, .
MAFoo,
. .
MAFoo Interface Builder , .
, .. , .
MAFoo
textField, [58b.7, 58b.11].

59
//[58b]
#import "MAFoo.h"
@implementation MAFoo
- (IBAction)reset:(id)sender
{
[textField setIntValue:0]; // [58b.7]
}
- (IBAction)setTo5:(id)sender
{
[textField setIntValue:5]; // [58b.11]
}
@end

, ,
(outlet) textField. .. , Interface
Builder, .
setIntValue: . setIntValue:
. ,
.


.
Build and Go Xcode. Xcode ,
.
.

, ( ) ,
2 .

9:
. 2 ,
1, Apple. setIntValue:
. ?
, , Apple,
. , .
,
.
Interface Builder,
(tooltip, ).
, NSButton. NSTextField.
. NSTextField, ,
.
Xcode (Help
Documentation?). Apple Mac OS X 10.5,
NSTextField ( API).
,
NSTextField.
NSTextField (of type Class)
NSTextField, .
, , .
NSObject.

61

Cocoa Xcode


- . ,
, . -
,
NSTextField, NSControl (
, NSControl, ).
HTML-,
NSControl ( Inherits).
NSControl.
NSControl
NSView : NSResponder : NSObject
. :
(Setting the Controls Value)
, . :
setIntValue:
,
setIntValue:.

62
setIntValue:
(void)setIntValue:(int)anInt

( ) anInt.
,
, NSActionCell
(NSActionCell )
NSTextField ,
. :
- (void)setIntValue:(int)anInt

Objective-C (
, ). Void
, .
, setIntValue textField, MAFoo
. .
(int) , anInt .
5 0, ,
.
. , ,
.
, , ,
textField? , ?
. , ,
, , .
, setIntValue, :
//[59]
- (int) intValue

, . ,
, ,
:
//[60]
int resultReceived = [textField intValue];

, ( )
. , ,

. , , ,
. Objective-C
: ,
.
, ,
.
, ,
( -),
, - .

63

,
. ,
, .
, ,
. , GUI, ,
setIntValue,
- .

10: awakeFromNib
Interface Builder nib.
3.0 xib.
Interface Builder. (. )
Apple ,
.
.
.
Foundation Kit, [41] 4,
, .
Application Kit, ,
, .
.
GUI-. , ,

.
xib- (nib NeXT Interface Builder').
,
Application Kit. - .
Xcode (Help
Documentation?). ,
( Title).
Application Kit Return .
XCode (). Application Kit

Framework Reference.

, .
(Protocol Reference) NSNibAwaking.
, NSNibAwaking.
NSNibAwaking
( )
Framework
/System/Library/Frameworks/AppKit.framework


AppKit/NSNibLoading.h


(Resource Programming Guide)

65


: awakeFromNib.

Interface Builder (nib-).
,
nib-. :
.
,
.
, .
, ,
.
,
.
, , , MAFoo.m
[61.13].
//[61]
#import "MAFoo.h"
@implementation MAFoo
- (IBAction)reset:(id)sender
{
[textField setIntValue:0];
}
- (IBAction)setTo5:(id)sender
{
[textField setIntValue:5];
}
- (void)awakeFromNib // [61.13]
{
[textField setIntValue:0];
}
@end

, awakeFromNib . ,
.

11:
C.
. ,
! ,
Objectice-C.
Mac ,
.
:
//[62]
int x = 4;

, Mac ,
x (
). [62] .
( int)
x. long double,
.
x = 4 4 . ,
.
. , ,
, ( )
.
, .


& . ,
x, &x.
x x (
4). ,
&x, x, .
(-
)

67


:
//[63]
int *y;

y,
( , ..
. ). : int,
, . y
x :
//[64]
y = &x;

y x. y
x. .
y
. , *y 4.
, x. :
*y = 5;

:
x = 5;

,
, . , ,
1. , :
//[65]
void increment(int x)
{
x = x + 1;
}

. ,
, .
//[66]
int myValue = 6;
increment(myValue);
NSLog(@"%d:\n", myValue);

6. ?
myValue ? . , [65]
myValue, ...
. ,
, , .
x, , .

68

, .
x . increment(5);
?
, ,

. , :
//[67]
void increment(int *y)
{
*y = *y + 1;
}

, , :
//[68]
int myValue = 6;
increment(&myValue); //
NSLog(@"%d:\n", myValue); // myValue 7

12:
: int, long, float, double, BOOL.
, , .
(strings) NSLog().
, %-,
%d, .
//[69]
float piValue = 3.14159;
NSLog(@" .\n");
NSLog(@" %10.4f.\n", piValue);
NSLog(@" %d .\n", 6);

. int
float, string .
NSString NSMutableString. .
NSString.

NSString

//[70]
NSString *favoriteComputer;
favoriteComputer = @"Mac!";
NSLog(favoriteComputer);

, ,
. , ,
. 11 [71].
//[71]
int *y;

, y
.
[70.2] , favoriteComputer
NSString .
Objective-C ,
.
, , . ,
, NSString
NSMutableString ( ), *.
@
@ ( . ),
? , Objective-C C

70

. ,
, , Objective-C @.

, C? Objective-C
, ASCII .
, .
, [72].
//[72]
NSString *favoriteActress = @"";

favoriteActress
.
, favoriteComputer,
, [73.9]
NSString. .
//[73]
#import <foundation/foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *favoriteComputer;
favoriteComputer = @"iBook"; // [73.9]
favoriteComputer = @"MacBook Pro";
NSLog(@"%@", favoriteComputer);
[pool release];
return 0;
}

:
MacBook Pro

NSMutableString
NSString ,
.
, ? , ,
,
, .
Objective-C , ,
.
, . ,
, . NSMutableString.
.

71

, , string. ..
, . ,
string.
//[74]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int theLength;
NSString * foo;
foo = @"!";
theLength = [foo length]; // [74.10]
NSLog(@" : %d.", theLength);
[pool release];
return 0;
}

, :
: 4

foo ( . ,
. ) bar - .
, .. ,
x. , ,
.
[74.10] foo . length
NSString :
- (unsigned int)length

.
[75].
, uppercaseString,
( , NSString ).
, ,
, .

72
//[75]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *foo, *bar;
foo = @"!";
bar = [foo uppercaseString];
NSLog(@"%@ %@.", foo, bar);
[pool release];
return 0;
}

:
! !

.
NSMutableString
. NSMutableString ,
. , appendString:
.
//[76]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString *foo; // [76.7]
foo = [@"!" mutableCopy]; // [76.8]
[foo appendString:@" ."];
NSLog(@": %@.", foo);
[pool release];
return 0;
}

:
: ! .

[76.8], mutableCopy ( NSString )


, . ,
[76,8], foo ,
!.

.
, Objective-C,
, . , ,
[76.7] . ,

73

Objective-C, , , .
, . ,
,
: .
, [76,8], foo ,
!.
. ,
foo bar, :
bar = foo;

, foo bar .
.
, foo, ([foo
dosomething];) , , bar
([bar dosomething];):
//[77]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString *foo = [@"!" mutableCopy];
NSMutableString *bar = foo;
NSLog(@"foo : %@.", foo);
NSLog(@"bar : %@.", bar);
NSLog(@"\n");
[foo appendString:@" ."];
NSLog(@"foo : %@.", foo);
NSLog(@"bar : %@.", bar);
[pool release];
return 0;
}

:
foo
bar
foo
bar

:
:
:
:

!
!
! .
! .


- . ,
. , 8,
MAFoo .

13:
. , .
,
. .
( ,
). , ,
. .
, . , ,
, 1 .
.
.
: NSArray NSMutableArray.
, () .
.
Objective-C Cocoa. ,
C ( Objective-C),
. ,
C ,
, NSArrays
NSMutableArrays.


:
[NSMutableArray array];

. ... ... ,
?
, NSMutableArray
. ,
, ?
, - : Objective-C
( , , ; , ).
, autoreleased
, .. NSAutoreleasePool ,
. :
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];

, , ,
-, retain.

75

Cocoa + ,
(. 8 [58.5]). ,
array:
array
+ (id)array

.
NSArray. : + arrayWithObject:, + arrayWithObjects:
. ,
, .
//[78]
#import <foundation/foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@" "];
[myArray addObject:@" "];
[myArray addObject:@" "];
int count = [myArray count];
NSLog(@" %d ", count);
[pool release];
return 0;
}

:
3


0 . objectAtIndex
[79.11].
//[79]
#import <foundation/foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@" "];
[myArray addObject:@" "];
[myArray addObject:@" "];
NSString *element = [myArray objectAtIndex:0]; // [79.11]
NSLog(@" 0: %@", element);
[pool release];
return 0;
}

76

:
0:

, - .
, ,
.
//[80]
#import <foundation/foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@" "];
[myArray addObject:@" "];
[myArray addObject:@" "];
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
NSString *element = [myArray objectAtIndex:i];
NSLog(@" %d: %@", i, element);
}
[pool release];
return 0;
}

:
0:
1:
2:

, .
.
NSArray NSMutableArray ,
,
. ,
. replaceObjectAtIndex:withObject:.
, .
, : .
, . Objective-C
. :
//[81]
[myArray replaceObjectAtIndex:1 withObject:@""];

, 1 .
, . ,

77

, , . ,
, .
, Objective-C, (
).
, . ,
Smalltalk
Objective-C, .
, ,
, . Objective-C
, .

14:
, , ; ,
, .
, ?
, ( )
(). , .
,
. ,
.


Cocoa ,
( , , , ) .
, .
, , .
. ,
.
. , ,
NSObject.
, calculator ,
.
, , , GUI.
.
:
//[82]
@interface MyCalculatorController : NSObject
{
//
NSArray * buttons;
NSTextField * resultField;
}
//
- (NSNumber *)mutiply:(NSNumber *)value;
- (NSNumber *)add:(NSNumber *)value;
- (NSNumber *)subtract:(NSNumber *)value;
- (NSNumber *)divide:(NSNumber *)value;
@end

79

:
. , 3,
(loop), . ,
, .
, ,
. ?
. , ,
.
, .
(. http://ru.wikipedia.org/wiki/
_()).
8 setIntValue: NSTextField
intValue. NSTextField.

, ? .
//[83]
@interface MyDog : NSObject
{
NSString * _name;
//[83.4]
}
- (NSString *)name;
- (void)setName:(NSString *)value;
@end

MyDog. MyDog : ,
_name [83.4]. _name,
: name setName:.
. :
//[84]
@implementation MyDog
- (NSString *)name
{
return _name; //[84.5]
}
- (void)setName:(NSString *)value
{
_name = value; //[84.9]
}
@end

[84.5] . [84.9]
. ,

80

; .
:
//[85]
@implementation MyDog
- (NSString *)name
{
return [[_name retain] autorelease];
}
- (void)setName:(NSString *)value
{
if (_name != value)
{
[_name release];
_name = [value copy];
}
}
@end

(. 15),
, , [84],
, , .
.
,
, .
name.
, .

Mac OS X Leopard Objective-C 2.0


. ,
, . ,

. .
?
, .
, , ,
.
[83] , Objective-C 2.0 :
//[86]
@interface MyDog : NSObject
{
NSString * name;
}
@property (copy) NSString *name;
@end

81

:
//[87]
@implementation MyDog
@synthesize name;
@end

[85]. , .
, ,
, !

15:
, .
. Mac,
( ) .
, (),
. ,
, !
, , Mac
.


, Cocoa ,
.
.
Mac OS X 10.5 Leopard Objective-C 2.0 :
(Cocoa Garbage Collection). ,
Cocoa-.
Cocoa ,
NSObject NSProxy. ,
Objective-C. .
, !


Xcode .
Inspector (Tools InspectorI).
Enable Objective-C Garbage Collection. ,
, ,
.

:
,
Mac OS X Leopard.
- ,
. ,
. , , ,
- , .
, ,
, , ,
. ,

83


, ,
, Cocoa ,
(retain count). ,
.
, , , .
, ,
.
, ,
, . ,
. ( )
, .
.


, ,
:
[anObject retain];

, :
[anObject release];


Cocoa , Autorelease pool,
. ..
, .
,
(Autorelease pool), :
[anObject autorelease];

Autorelease pool .
, Autorelease pool,
, , Autorelease pool
.

16:
, Objective-C
Xcode. ,
, , Killer apps, .
. ,
. ,
: !
, Apple, , ,
, . , - ,
, , .
, .
Apple , :
http://developer.apple.com
:

http://osx.hyperjeff.net/reference/CocoaArticles.php
http://www.cocoadev.com
http://www.cocoadevcentral.com
http://www.cocoabuilder.com
http://www.stepwise.com

. cocoa-dev.
http://lists.apple.com/mailman/listinfo/cocoa-dev. ,
. , ,
,
(http://www.cocoabuilder.com).
, How To Ask Questions The
Smart Way, http://www.catb.org/~esr/faqs/smartquestions.html.
Cocoa. Programming in Objective-C
(Stephen Kochan) .
, .
Cocoa Programming for Mac OS X Aaron Hillegass Big Nerd
Ranch, Xcode. Cocoa with Objective-C,
James Duncan Davidson Apple, O'Reilly.
, . Mac.
, ,
Apple.
, , !
.
Xode.
Bert, Alex, Philippe.

Вам также может понравиться