You are on page 1of 68

[Enter Document Title]

Become an
Xcoder
Xcode

/ Bert Altenburg, Alex Clarke, Philippe Mougin


/

Become an Xcoder (Simple-Chinese Language Edition)

Copyright 2006 by Bert Altenburg, Alex Clarke and Philippe Mougin. Version 1.2.

Attribution: The licensors, Bert Altenburg, Alex Clarke and Philippe Mougin, permit others to copy, modify
and distribute the work. In return, the licensees must give the original authors credit.

Non-commercial: The licensors permit others to copy, modify and distribute the work and use the work in
paid-for and free courses. In return, licensees may not sell the work itself, although it may accompany other
work that is sold.

Bert Altenburg, Alex Clarke and Philippe Mougin 2006 1.2


Bert Altenburg, Alex Clarke and Philippe Mougin

Xcode

___________________________________________________________ 2
______________________________________________________________________ 3
______________________________________________________________________ 4
0 _________________________________________________________ 5
1 ___________________________________________________ 6
2 ______________________________________________ 11
3 ______________________________________________________________ 12
4 ______________________________________________________ 18
5 ________________________________________________ 23
6 __________________________________________________________ 30
7 ______________________________________________________________ 32
8 ________________________________________________ 34
9 __________________________________________________________ 48
10 awakeFromNib ________________________________________________ 51
11 _____________________________________________________________ 53
12 ___________________________________________________________ 55
13 _____________________________________________________________ 61
14 _________________________________________________________ 65
15 _________________________________________________________ 67
________________________________________________________________ 68

Become an Xcoder (Simple-Chinese Language Edition)

Apple Computer, Inc.Cocoa


XcodeMac OS X
Mac
Objective-C
Xcode
GUI

[4.3]4

volume = baseArea * height; // [4.3]

Xcode

Xcode

0
Mac
Mac

1MacMac
MacObjective-CAppleScriptAppleScript
AppleScriptAppleScript for Absolute Starters

http://www.macscripter.net/books
2PCMacT
Activity Monitor
Mac
distributed computing projectsDC
Folding@homeSETI@homeMac
DCDC client DCDC client
DCDC client
MacDC
Mac
MacDC
DC
http://distributedcomputing.info/projects.html

3Mac

bug

4Mac

53Mac
4
DCDC client

Become an Xcoder (Simple-Chinese Language Edition)

Xcode
Objective-C

26
3*4*
xyvariables

26x
y=3*4
Objective-C
Objective-Cstatement

[1]
x = 4;

x4
[1]
compiler
Mac01

Mac

(bugs)(debugging)
x
pictureWidth[2]

Xcode

[2]
pictureWidth = 8;

pictureWidth
pictureWIDTHPictureWidth
[2]

Objective-C Objective-C
pictureWidth

[3]
_
[3]

pictureWidth=8;
door8kdo8ordo_or
pictureHeight=6;
door 88door
pictureSurfaceArea=pictureWidth*pictureHeight;
Door8

[4]
pictureWidth = 8;
pictureHeight = 6;
pictureSurfaceArea = pictureWidth * pictureHeight;
[5]
[5]
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;

[5.1][5.2]

[5]

Become an Xcoder (Simple-Chinese Language Edition)

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

[6.1]intpictureWidthint
[6.2]float
pictureWidth
[6.2]
pictureSurfaceArea

intfloat

intfloat
long longdouble

[7]
unsigned int chocolateBarsInStock;

unsigned int
unsigned int0
[8]
[8]
int x = 10;
float y= 3.5, z = 42;

Xcode

+
-
/
*
Objective-C

[9][10]x = x + 1
[9]
x++;
[10]
++x;

x1 ++
[11][12]
[11]
x = 10;
y = 2 * (x++);
[12]
x = 10;
y = 2 * (++x);

[11]y20x11xx = x + 1
[12.2]x12(x = x + 1x)
x11y22[12][13]
[13]
x = 10;
x++;
y = 2 * x;

*
2 * 3 + 4102 * (3 + 4)14
[14]2
[15]2.4

Become an Xcoder (Simple-Chinese Language Edition)

[14]
int x = 5, y = 12, ratio;
ratio = y / x;
[15]
float x = 5, y = 12, ratio;
ratio = y / x;

0
[16]
int x = 13, y = 5, remainder;
remainder = x % y;

[16]3x 2 * y + 3

21 % 7 = 0

30 % 2 = 0

50 % 9 = 5

22 % 7 = 1

31 % 2 = 1

60 % 29 = 2

23 % 7 = 2

32 % 2 = 0

24 % 7 = 3

33 % 2 = 1

27 % 7 = 6

34 % 2 = 0

10

Xcode

2
[1]
[1]
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;

// This is a comment

Xcode/* */
/* This is a comment
extending over two lines */

Xcode
/* */

11

Become an Xcoder (Simple-Chinese Language Edition)

3
5
Objective-C

function
main( ) function
[1]
[1]
main( )
{
// Body of the main( ) function. Put your code here.
}

[1.1]main{ }main
main( )

[2]
[2]
main( )
{
// Variables are declared below
float pictureWidth, pictureHeight, pictureSurfaceArea;
// We initialize the variables (we give the variables a value)
pictureWidth = 8.0;
pictureHeight = 4.5;
// Here the actual calculation is performed
pictureSurfaceArea = pictureWidth * pictureHeight;
}

12

Xcode

main( )
[3]main( )
circleArea( )
[3]
main( )
{
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
}
circleArea( ) // [3.9]
{
}

[3.9]circleArea( )main( )

circleArea( )main( )[4]


[4]

[3]

main( )
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea; // [4.4]
pictureWidth = 8.0;
pictureHeight = 4.5;
circleRadius = 5.0; // [4.7]
pictureSurfaceArea = pictureWidth * pictureHeight;
// Here we call our function!
circleSurfaceArea = circleArea(circleRadius); // [4.11]
}

[4.4][4.7]circleRadius[4.11]
circleArea( )circleRadius
circleArea( )circleRadiuscircleArea( )circleArea( )
[5][3]circleArea( )

13

Become an Xcoder (Simple-Chinese Language Edition)

[5]

circleArea( )

circleArea(float theRadius) // [5.1]


{
float theArea;
theArea = 3.1416 * theRadius * theRadius; // pi times r square [5.4]
return theArea;
}

[5.1]circleArea( )
theRadius[5.4]theArea[5.3]
[4][5.1]
theRadius [5.5] [4.11]
circleSurfaceArea
[5]
[6]
[6]
float circleArea(float theRadius)
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}

[6.1]theArea
main( )[4.8]circleSurfaceArea

14

Xcode

[7]
int throwDice()
{
int noOfEyes;
// Code to generate a random value from 1 to 6
return noOfEyes;
}

voidreturn

[8]
void beepXTimes(int x);
{
// Code to beep x times.
return;
}

[9]
float pictureSurfaceArea(float theWidth, float theHeight)
{
// Code here
}

main( )return0
[10.9]main( )int
[10.1]
[10]
int main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;

15

Become an Xcoder (Simple-Chinese Language Edition)

pictureWidth = 8;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = pictureWidth * pictureHeight;
circleSurfaceArea = circleArea(circleRadius); // [10.9]
return 0;
}
float circleArea(float theRadius) // [10.13]
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}

[10]main( )[10.1]
[10.13][10.9]
circleArea( )main( )
main( )
function declaration,[11.1][10.13]

[11]

[10]

float circleArea(float theRadius); // function declaration


int main()
{

[12]
rectangleArea( )main( )
main( )

[12]
float rectangleArea(float length, float width)
{
return (length * width);
}

16

Xcode

[12.3]
[10.5]theArea

-
-
-
[12]
- rectangleArea
- 2
- [12.14]
Objective-C
Xcode[11]

17

Become an Xcoder (Simple-Chinese Language Edition)

CocoaNSLog( )
NSLog( )
Cocoa
NSLog( )
[1]
int main()
{
NSLog(@"Julia is a pretty actress.");
return 0;
}

[1]Julia is a pretty actress.@""


NSLog( )
[1]
2005-12-22 17:39:23.084 test[399] Julia is a pretty actress.

[2]

main( )

NSLog(@"");
NSLog(@" ");

[2.1][2.2]
1

\n[3.1]\n
[3]
NSLog(@"Julia is a pretty \nactress.");

[3]
Julia is a pretty
actress.

[3.1]\escape characterNSLog( )
n

18

Xcode

\
\\
NSLog( )\
[4]
NSLog(@"Julia is a pretty actress.\\n");

[4]
Julia is a pretty actress.\n

[5]
int x, integerToDisplay;
x = 1;
integerToDisplay = 5 + x;
NSLog(@"The value of the integer is %d.", integerToDisplay);

%d%d
%d
integerToDisplay[5]
The value of the integer is 6.

%f %d
[6]
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@"The value of the float is %f.", floatToDisplay);

%f.2
[7]
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@"The value of the float is %.2f.", floatToDisplay);

19

Become an Xcoder (Simple-Chinese Language Edition)

%f
%d

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

[8]
123456
123456
123456
123456

[8.18.2]
[8.4]

[9]
float x=1234.5678
NSLog(@"Reserve a space of 10, and show 2 significant digits.";
NSLog(@"%10.2d", x);

[10.3]intfloat
%d%f
[10]
int x = 8;
float pi = 3.1416;
NSLog(@"The integer value is %d, whereas the float value is %f.", x, pi);

20

Xcode

[10b]
int x = 8;
float pi = 3.1416;
NSLog(@"The integer value is %f, whereas the float value is %f.", x, pi);

The integer value is 0.000000, whereas the float value is 0.000000.

NSLog( )
NSLog( )

#import <Foundation/Foundation.h>

[11]
#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(@"Area of circle: %10.2f.", circleSurfaceArea);
NSLog(@"Area of picture: %f. ", pictureSurfaceArea);

21

Become an Xcoder (Simple-Chinese Language Edition)

return 0;
}

float circleArea(float theRadius) // first custom function


{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}

float rectangleArea(float width, float height) // second custom function


{
return width*height;
}

22

Xcode

Mac
MacXcodeMac OS X
http://developer.apple.com
Apple ID
DeveloperXcodeXcode
Xcode
FileNew ProjectAssitant

XcodeAssitant
Objective-CFoundation
ToolCommand Line Utility

23

Become an Xcoder (Simple-Chinese Language Edition)

justatryFinish
Terminal

Groups & Files

Xcode
GroupGroups

Groups & FilesjustatrySource


justatr. m[1]main( )
main( )
justatry. mmain( )

24

Xcode

Xcodemain( )
[1]
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) // [1.3]


{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // [1.5]
// insert code here...
NSLog(@"Hello, World!");

[pool release]; //[1.9]


return 0;
}

- NSLog( )import#
- main( )
-

25

Become an Xcoder (Simple-Chinese Language Edition)

-
- NSLog( )
- return 0

- main( )[1.3]
- NSAutoreleasePool[1.5]
- poolrelease[1.9]

main( )[1]
[1.31.51.9]
Objective-C

main( )
Terminal

pool
[1]Build and Go

Build and Go
Run Log
00main( )[7.9]

[1]NSLog( )

[2]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{

26

Xcode

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


// insert code here...
NSLog(@"Julia is a pretty actress") //Whoops, forgot the semicolon!
[pool release]; //[2.9]
return 0;
}

Build and Go[2.9]

Xcode

error: parse error before "release".

import#[2.1][2.8]
[2.9]

Xcode[1][3]
[3]
#import <Foundation/Foundation.h>

float circleArea(float theRadius); // [3.3]

int main (int argc, const char * argv[]) // [3.5]


{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int pictureWidth;
float pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;

27

Become an Xcoder (Simple-Chinese Language Edition)

circleRadius = 5.0;
pictureSurfaceArea = pictureWidth * pictureHeight;
circleSurfaceArea = circleArea(circleRadius);
NSLog(@"Area of picture: %f. Area of circle: %10.2f.",
pictureSurfaceArea, circleSurfaceArea);
[pool release];
return 0;
}
float circleArea(float theRadius) // [3.22]
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
float rectangleArea(float width, float height) // [3.29]
{
return width*height;
}

[3.33.4]main( )
circleArea( )rectangleArea( )
main( )main( )Xcode

Area of picture: 36.000000. Area of circle: 78.54.


justatry has exited with status 0.

Xcode

Xcodebreakpoint

28

Xcode

Build and Go

Build and Go
Build and Debug

XcodeDebugger

Continue
Debugger
Mac OS X
Objective-C

29

Become an Xcoder (Simple-Chinese Language Edition)

6
[1.2]

[1]
int age = 42;
if (age > 30) // The > symbol means "greater than"
{
NSLog(@"age is older than thirty."); //[1.4]
}
NSLog(@"Finished."); //[1.6]

[1.2]if
[1]age > 30[1.4]
[1.6]if
if
[2]ifelse
[2]
int age = 42;
if (age > 30)
{
NSLog(@"age is older than thirty."); //[2.4]
}
Else
{
NSLog(@"age is not older thirty."); //[2.7]
}
NSLog(@"Finished."); //[1.6]

[2.7][2]
[2.2]
==

>=

>

<=

<

!=

30

Xcode

= =
=

if

Objective-C10
BOOL1YES0NO
[3]
int x = 3;
BOOL y;
y = (x == 4); // y will be 0.

ANDAND
&&OROR
||
[4]
if ( (age >= 18) && (age < 65) )
{
NSLog(@"Probably has to work for a living.");
}

[5]
if (age >= 18)
{
if (age < 65)
{
NSLog(@"Probably has to work for a living.");
}
}

31

Become an Xcoder (Simple-Chinese Language Edition)

[1]
NSLog(@"Julia is a pretty actress.");
NSLog(@"Julia is a pretty actress.");
NSLog(@"Julia is a pretty actress.");

Objective-C
[2]for
2.7
[2]
int x;
for (x = 1; x <= 10; x++)
{
NSLog(@"Julia is a pretty actress.");
}
NSLog(@"The value of x is %d", x);

[2][2.4]10x1x < =
10x1
xx++1x21010
x11x<=10[2.6]
x1110
x++[3]

[3]
float celsius, tempInFahrenheit;
for

(tempInFahrenheit

0;

tempInFahrenheit

<=

200;

tempInFahrenheit

=tempInFahrenheit + 20)
{
celsius = (tempInFahrenheit - 32.0) * 5.0 / 9.0;

32

Xcode

NSLog(@"%10.2f -> %10.2f", tempInFahrenheit, celsius);


}

[2]

Objective-Cwhile () { }do {} while ()


for

[4]
int counter = 1;
while (counter <= 10)
{
NSLog(@"Julia is a pretty actress.\n");
counter = counter + 1;
}
NSLog(@"The value of counter is %d", counter);

counter11
do {} while ()1
[5]
int counter = 1;
do
{
NSLog(@"Julia is a pretty actress.\n");
counter = counter + 1;
}
while (counter <= 10);
NSLog(@"The value of counter is %d", counter);

counter11

33

Become an Xcoder (Simple-Chinese Language Edition)

8
Objective-C
CCObjective-C
CObjectiveObjective-C
Objective-C

Objective-C
Objective-C

Objective-CSafari
MacSafari

Safari
Web

34

Xcode

Safaria.
b.Safari
bclass

76
Dock

closemethods

RAM

35

Become an Xcoder (Simple-Chinese Language Edition)

Mac

Objective-C[1.1][1.2]
[1]
[receiver message];
[receiver message:argument];

[1.2]

XcodeApplication
Cocoa Application
Groups & FilesResourcesMainMenu.nib

36

Xcode

XcodeMainMenu.nib
Interface Builder
Window,
Window
Cocoa-
Window
System Font Text

NSButton

37

Become an Xcoder (Simple-Chinese Language Edition)

NSTextView

Window

command-shift-i
WindowInstances
command-shift-iAttributesTextured
Window

Window

Objective-CC
NSWindow

superclass

38

Xcode

Objective-C

[2]
// Code to move the window out of sight here.
[super close]; // Use the close method of the superclass.

NSObject
subclassNSWindowNSResponder
NSResponderNSObjectNSObject

MainMeni.nibClassesNSObject
ClassesSubclass NSObjectMainMeni.nib
MAFoo

MAFoo

39

Become an Xcoder (Simple-Chinese Language Edition)

MAFooMy Application

NSNSNeXTStepMac OS
XNeXTStepNeXT
Steve Jobs
CocoaDev wiki
http://www.cocoadev.com/index.pl?ChooseYourPrefix
Cocoa
NSWindowNSColor
MAFoo

ClassesInstantiate MAFooInstances
MAFoo

MAFoo
MAFoo
MAFoo
MAFoo
MAFooMAFoo
MAFoo

MAFooMAFooMAFoo
MAFooMAFooMAFoo
MAFoo

40

Xcode

Mainfile.nibClassesMAFoocommand-shift-i
ActionAddMAFoo

setTo5:5
reset:0

MAFoo
OutlettextField

41

Become an Xcoder (Simple-Chinese Language Edition)

MAFoo
5
Set to 5Reset

aResetMAFoo
bSet to 5MAFoo
cMAFoo
MainFile.nibInstancesCtrlReset
MAFoo
MAFoo

42

Xcode

MAFoo
Connect

MAFooreset:

Set to 5MAFoo
MAFooCtrlMAFoo
Connect

MainMenu.nibMAFooClasses
MAFooClassesCreate Files for MAFoo

43

Become an Xcoder (Simple-Chinese Language Edition)

MAFoo
XcodeOther Sources
Classes

Xcode
[11.1]

44

Xcode

MAFoo.h,[3.5]NSObject
NSObject
[3]
/* MAFoo */

#import <Cocoa/Cocoa.h> // [3.3]

@interface MAFoo : NSObject // [3.5]


{
IBOutlet id textField; // [3.7]
}
- (IBAction)reset:(id)sender; // [3.9]
- (IBAction)setTo5:(id)sender; // [3.10]
@end

[3.7]idIB

[3.9,3.10]IBAction
MAFoo
Interface Builder Actions
[3.3]#import <Foundation/Foundation.h>

MAFoo.m
[4]
#import "MAFoo.h"

@implementation MAFoo

(IBAction)reset:(id)sender // [4.5]

{
}
-

(IBAction)setTo5:(id)sender

45

Become an Xcoder (Simple-Chinese Language Edition)

{
}
@end

MAFoo.hreset:setTo5:

MAFoo
MAFoo
MAFoo[5.7,5.12]

[5]
#import "MAFoo.h"

@implementation MAFoo

(IBAction)reset:(id)sender

{
[textField setIntValue:0]; // [5.7]
}

(IBAction)setTo5:(id)sender

{
[textField setIntValue:5]; // [5.12]
}
@end

textField

setIntValue: setIntValue:

XcodeBuild and GoXcode

46

Xcode

47

Become an Xcoder (Simple-Chinese Language Edition)

setIntValue:

NSButtonSystem Font Text


NSTextField
NSTextField
XcodeHelpDocumentationCocoa
NSTexFieldAPI-Search
NSTextField
NSTexFieldClasses

Cocoa

48

Xcode

NSObject
Method Types

NSControlNSControl
NSViewNSControl
Inherits fromNSControl

Setting the controls value


setIntValue:
setIntValue:

NSTextField
signature- (void)setIntValue:(int)anInt
Objective-Cinstance methoodclass
methoodvoid
setIntValue:textFieldMAFoo
intanInt
05
documentation

49

Become an Xcoder (Simple-Chinese Language Edition)

textField

AccessorssetIntValue:

[1]
- (int) intValue

textfield

[2]
resultReceived = [textField intValue];

Objective-C

polymorphism

setIntValue:

50

Xcode

10 awakeFromNib

framework4[12]
Foundation Kit
Application Kit

nibnibNeXT
Application Kit
XcodeHelpDocumentation
DocumentationFull-Text Search
Application KitXcode
Application Kit Reference for Objective-C
ProtocolsNSNibAwakening

nib

51

Become an Xcoder (Simple-Chinese Language Edition)

MAFoo.m
[1.15]
[1]
#import "MAFoo.h"

@implementation MAFoo

- (IBAction)reset:(id)sender
{
[textField setIntValue:0];
}

- (IBAction)setTo5:(id)sender
{
[textField setIntValue:5];
}

- (void)awakeFromNib // [1.15]
{
[textField setIntValue:0];
}
@end

awakeFromNib0

52

Xcode

11
C

Objective-C
Mac

[1]
int x = 4;

Macx
[1]
x

x = 44
xxaddressx
x

&x
&x
xx4
&xx

[2]
int *y;

y x
yxy
[3]
y = &x;

**y
4x*y = 5x 5

53

Become an Xcoder (Simple-Chinese Language Edition)

[4]
void increment(int *y)
{
*y = *y + 1;
}

[5]
int x = 4;
increment(&x);
// now x is equal to 5

54

Xcode

12

NSLog ( )

%d
[1]
float piValue = 3.1416;
NSLog(@"Here are three examples of strings printed to the screen.\n");
NSLog(@"Pi approximates %10.4f.\n", piValue);
NSLog(@"The number of eyes of a dice is %d.\n", 6);

NSString
NSMutableStringNSString
[2]
NSString *favoriteComputer;
favoriteComputer = @"Mac!";
NSLog(favoriteComputer);

[2.1]
11[2]
[3]
int *y;

y[2.1]
favoriteComputerNSString
Objective-C
@Objective-CC
Objective-C@
Objective-CCObjective-C
UnicodeASCIIUnicode

[4]

55

Become an Xcoder (Simple-Chinese Language Edition)

[4]
NSString *favoriteActress = @"Julia";

favoriteActressJulia
favoriteComputer
NSString[5]
[5]
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *x;
x = @"iBook"; // [5.7]
x = @"MacBook Pro Intel"; // Hey, I'm just trying to make
// this book look up to date!
NSLog(x);
[pool release];
return 0;
}

MacBook Pro Intel

NSString

Objective-C

NSMutableString

[6]length
[6]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int theLength;

56

Xcode

NSString * foo;
foo = @"Julia!";
theLength = [foo length]; // [6.10]
NSLog(@"The length is %d.", theLength);
[pool release];
return 0;
}

The length is 6.

foobar
x

[10.6]foolengthlengthNSString

[7]
uppercaseStringNSString

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

Julia! is converted into JULIA!

57

Become an Xcoder (Simple-Chinese Language Edition)

NSMutableStringNSMutableString
appendString
[8]
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])


{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSMutableString *foo; // [8.7]


foo = [@"Julia!" mutableCopy]; // [8.8]
[foo appendString:@" I am happy"];
NSLog(@"Here is the result: %@.", foo);
[pool release];
return 0;
}

Here is the result: Julia! I am happy.

[8.8]mutableCopyNSString
[8.8]fooJulia
Objective-C
[8.7]Objective-C

[8.7]
fooJulia

foobar
bar = foo;

58

Xcode

foobar

foo [foo dosomething]bar


[bar dosomething]
[9]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString *foo = [@"Julia!" mutableCopy];
NSMutableString *bar = foo;
NSLog(@"foo points to the string: %@.", foo);
NSLog(@"bar points to the string: %@.", bar);
NSLog(@"------------------------------");
[foo appendString:@" I am happy"];
NSLog(@"foo points to the string: %@.", foo);
NSLog(@"bar points to the string: %@.", bar);
[pool release];
return 0;
}

foo points to the string: Julia!


bar points to the string: Julia!
-----------------------------foo points to the string: Julia! I am happy
bar points to the string: Julia! I am happy

59

Become an Xcoder (Simple-Chinese Language Edition)

8MAFoo

60

Xcode

13

array

101

0
NSArrayNSMutableArray

[NSMutableArray array]

NSMutalbeArray

Objective-C
meta-class
Cocoa
8[4.5] array

61

Become an Xcoder (Simple-Chinese Language Edition)

[1]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int count = [myArray count];
NSLog(@"There are %d elements in my array", count);
[pool release];
return 0;
}

There are 3 elements in my array

0
[2.13]objectAtIndex:
[2]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
NSString *element = [myArray objectAtIndex:0]; // [2.13]
NSLog(@"The element at index 0 in the array is: %@", element);
[pool release];
return 0;
}

62

Xcode

The element at index 0 in the array is: first string

[3]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"first string"];
[myArray addObject:@"second string"];
[myArray addObject:@"third string"];
int i;
int count;
for (i = 0, count = [myArray count]; i < count; i = i + 1)
{
NSString *element = [myArray objectAtIndex:i];
NSLog(@"The element at index %d in the array is: %@", i, element);
}
[pool release];
return 0;
}

The element at index 0 in the array is: first string


The element at index 1 in the array is: second string
The element at index 2 in the array is: third string

63

Become an Xcoder (Simple-Chinese Language Edition)

NSArrayNSMutableArray

replaceObjectAtIndex: withObject:

Objective-C

[4]
[myArray replaceObjectAtIndex:1 withObject:@"Hello"];

1@Hello

Objective-C
SmalltalkXerox
Objective-C

64

Xcode

14

MacRAM

Mac
Mac

Cocoa

retain message
release messageCocoaautorelease pool

autorelease poolautorelease pool

65

Become an Xcoder (Simple-Chinese Language Edition)

autorelease pool
Cocoareference counting
Cocoa15
Cocoaautomatic garbage
collection

66

Xcode

15
Objective-CXcode

http://www.apple.com/developer

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

cocoa-dev
http://lists.apple.com/mailman/listinfo/cocoa-dev

http://www.cocoabuilder.com http://www.catb.org/~esr/faqs/smart-questions.html

How To Ask Questions The Smart Way


CocoaStephen KochanProgramming in Objective-C
Aaron Hillegass
Cocoa Programming for Mac OS XBig Nerd Ranch
James Duncan DavidsonO'ReillyCocoa with
Objective-C
Mac
The Apple human interface guidelines
http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/
index.html

Xcode0

Bert, Alex, Philippe

67

Become an Xcoder (Simple-Chinese Language Edition)

Become an Xcoder

WindowsMac OS
GW-BasicQ-BasicVBBasic
68020
Apple

C
C

http://www.cocoalab.com/BecomeAnXcoder.pdf

http://www.macx.cn/a/a5544I156848.mac

http://www.wally.in/

68

You might also like