Download - Become an xcoder
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.
BECOME AN XCODER
2
__________________________________________________ 4
0 ______________________________________ 5
1 _____________________________ 7
2 __________________________ 13
3 ______________________________________________ 14
4 _______________________________________ 20
5 _____________________________________ 25
6 __________________________________________ 33
7 while _______________________________ 36
8 GUI ___________________ 39
9 _______________________________________ 55
10 _____________________________________awakeFromNib 59
11 ______________________________________________ 61
12 ______________________________________________ 63
13 ______________________________________________ 69
14 _________________________________________73
15 __________________________________________ 75
BECOME AN XCODER
3
Apple Cocoa
Xcode Mac OS X Apple
Mac
Objective-C Xcode
(GUI)
[4]
[4.3] [4]
volume = baseArea * height; // [4.3]
Xcode
BECOME AN XCODER
4
0
Mac Macintosh
1. Mac Mac Mac
Mac Objective-C AppleScript
AppleScript
AppleScript for Absolute Starters
http://www.macscripter.net/books
2. Macintosh PC
Mac T Mac
Mac Mac
distributed computing, DC
Folding@home SETI@home
DC DC
Mac DC
Mac
DC Mac
Mac
Mac DC
DC
http://distributedcomputing.info/projects.html
3. Macintosh
4. Macintosh
BECOME AN XCODER 0
5
5. Macintosh
DC
BECOME AN XCODER 0
6
1
Xcode
Objective-C
2 + 6 =
= 3 * 4 *
x y
2 + 6 = x
y = 3 * 4
Objective-C
Objective-C statement
[1]
x = 4;
x 4
... [1]
compiler
Mac 0 1
Mac
BECOME AN XCODER 1
7
b u g
debugging
x
pictureWidth [2]
[2]
pictureWidth = 8;
pictureWidth pictureWIDTH PictureWidth
[2]
Objective-C Objective-C
pictureWidth
"_"
door8k do8or do_or
door 8 8door
Door8
[3]
[3]
pictureWidth=8;
pictureHeight=6;
pictureSurfaceArea=pictureWidth*pictureHeight;
BECOME AN XCODER 1
8
[4]
pictureWidth = 8;
pictureHeight = 6;
pictureSurfaceArea = pictureWidth * pictureHeight;
[5]
[5]
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
integers [5.1] [5.2]
floating-
point
[5]
" "
[6]
int pictureWidth;
float pictureHeight, pictureSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
[6.1] int pictureWidth integer
[6.2]
float
pictureWidth int
float pictureSur-
faceArea [6.2]
int float
int float
long long double
BECOME AN XCODER 1
9
[7]
unsigned int chocolateBarsInStock;
unsigned int unsigned int
[8]
[8]
int x = 10;
float y= 3.5, z = 42;
operators
+
-
/
*
Objective-C
x = x + 1; [9] [10]
[9]
x++;
[10]
++x;
x ++
[11] [12]
[11]
x = 10;
BECOME AN XCODER 1
10
y = 2 * (x++);
[12]
x = 10;
y = 2 * (++x);
[11] y 20 x 11 [12.2]
x 2 1 x 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 = 5, y = 12, ratio;
ratio = y / x;
[15]
float x = 5, y = 12, ratio;
ratio = y / x;
[14] 2 [15]
2.4
% % operand
%
[16]
int x = 13, y = 5, remainder;
remainder = x % y;
BECOME AN XCODER 1
11
remainder 3 x 2*y + 3
21 % 7 0
22 % 7 1
23 % 7 2
24 % 7 3
27 % 7 6
30 % 2 0
31 % 2 1
32 % 2 0
33 % 2 1
34 % 2 0
50 % 9 5
60 % 29 2
BECOME AN XCODER 1
12
2
[1]
[1]
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
//
Xcode /* */
/*
*/
Xcode
outcomment /* */
BECOME AN XCODER 2
13
3
Objective-C
function
main() main()
main() [1]
[1]
main()
{
// main()
}
[1.1] "main" "main"
main()
[1.2,1.5]
[2]
[2]
main()
{
//
float pictureWidth, pictureHeight, pictureSurfaceArea;
//
BECOME AN XCODER 3
14
pictureWidth = 8.0;
pictureHeight = 4.5;
//
pictureSurfaceArea = pictureWidth * pictureHeight;
}
main()
main()
circleArea() [3]
[3]
main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
}
circleArea() // [3.9]
{
}
[3.9]
main()
circleArea() main() [4]
[4]
main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea; // [4.4]
pictureWidth = 8.0;
pictureHeight = 4.5;
circleRadius = 5.0; // [4.7]
pictureSurfaceArea = pictureWidth * pictureHeight;
BECOME AN XCODER 3
15
//
circleSurfaceArea = circleArea(circleRadius); // [4.11]
}
[3]
float [4.4] initialize circleRadius
[4.7] [4.11] circleArea()
circleRadius circleArea() argument
circleRadius circleArea() circleArea()
return [3] circleArea()
[5]
circleArea()
[5]
circleArea(float theRadius) // [5.1]
{
float theArea;
// pi r
theArea = 3.1416 * theRadius * theRadius; //[5.5]
return theArea;
}
[5.1] circleArea()
theRadius theArea [5.5]
[5.3] main [4.4]
theRadius [5.1] [5.6]
[4.11] circleSurfaceArea
[5]
float [6.1]
[6]
float circleArea(float theRadius)
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
BECOME AN XCODER 3
16
[6.1] theArea
float main() circleSurfaceArea
[4.11]
[7]
int throwDice()
{
int noOfEyes;
// 1 6
return noOfEyes;
}
“void”
“return” return
[8]
void beepXTimes(int x);
{
// x
return;
}
pictureSurfaceArea()
[9]
float pictureSurfaceArea(float theWidth, float theHeight)
{
//
}
main() return
0 [10.10] main()
main() "int" [10.1]
[10]
BECOME AN XCODER 3
17
int main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea,
circleRadius, circleSurfaceArea;
pictureWidth = 8;
pictureHeight = 4.5;
circleRadius = 5.0;
pictureSurfaceArea = pictureWidth * pictureHeight;
circleSurfaceArea = circleArea(circleRadius);
return 0; // [10.10]
}
float circleArea(float theRadius) // [10.13]
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
[10] [10.1] main() [10.13]
[10.9]
circleArea() main()
warning int main() function
declaration [11.1] [10.13]
[11]
float circleArea(float theRadius); //
int main()
{
[10]
rectangleArea() [12] main()
BECOME AN XCODER 3
18
main()
[12]
float rectangleArea(float length, float width)
{
return (length * width);
}
[12.3]
[10.15] theArea
[12]
rectangleArea
float
float [12.1]
Objective-C
Xcode
[11]
BECOME AN XCODER 3
19
4
Cocoa
NSLog()
" "
NSLog()
Cocoa
NSLog()
[1]
int main()
{
NSLog(@"Julia is a pretty actress.");
return 0;
}
[1] "Julia is a pretty actress." @" "
string
NSLog()
[1]
2005-12-22 17:39:23.084 test[399] Julia is a pretty actress.
character
main()
[2]
NSLog(@"");
NSLog(@" ");
[2.1] empty string
[2.2] space
1
BECOME AN XCODER 4
20
[3.1] \n
[3]
NSLog(@"Julia is a pretty \nactress.");
Julia is a pretty
actress.
[3.1] escape NSLog()
"n" " "
NSLog()
[4]
NSLog(@"Julia is a pretty actress.\\n");
[4.1]
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 deci-
mal number %d
integerToDisplay [5]
The value of the integer is 6.
%f %d
[6]
BECOME AN XCODER 4
21
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@"The value of the float is %f.", floatToDisplay);
.2 % f
[7]
float x, floatToDisplay;
x = 12345.09876;
floatToDisplay = x/3.1416;
NSLog(@"The value of the float is %.2f.", floatToDisplay);
% f % d
[8]
int x = 123456;
NSLog(@"%2d", x);
NSLog(@"%4d", x);
NSLog(@"%6d", x);
NSLog(@"%8d", x);
[8]
123456
123456
123456
123456
[8.1, 8.2]
[8.4]
[9]
float x = 1234.5678
NSLog(@"Reserve a space of 10, and show 2 significant digits.");
NSLog(@"%10.2d", x);
BECOME AN XCODER 4
22
[10.3]
%d %f int float
[10]
int x = 8;
float pi = 3.1416;
NSLog(@"The integer value is %d, whereas the float value is %f.", x, pi);
[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()
import 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);
BECOME AN XCODER 4
23
NSLog(@"Area of circle: %10.2f.", circleSurfaceArea);
NSLog(@"Area of picture: %f. ", pictureSurfaceArea);
return 0;
}
float circleArea(float theRadius) //
{
float theArea;
theArea = 3.1416 * theRadius * theRadius;
return theArea;
}
float rectangleArea(float width, float height) //
{
return width * height;
}
BECOME AN XCODER 4
24
5
Mac Mac
compiler Mac
Apple Xcode Mac OS X
X c o d e
http://developer.apple.com
Xcode Developer Applications
Preferences File New Pro-
ject
Xcode
Objective-C GUI Graphical User
Interface Command Line Utility Foundation Tool
BECOME AN XCODER 5
25
justatry
Finish
Terminal
"Groups & Files"
GUI GUI
Xcode "Groups"
Groups & Files justatry Source
justatry.m [1] main()
main()
justatry.m Apple
main()
BECOME AN XCODER 5
26
Xcode main()
[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()
BECOME AN XCODER 5
27
NSlog
return 0;
main() [1.3]
NSAutoreleasePool [1.5]
pool release [1.9]
" "
main()
[1]
[1.3 1.5 1.9] Objective-
C
main
"pool"
Apple [1] Build and Go
build compile
Build and Go
Run Log
exited stopped 0 main()
[7.9]
[1]
NSLog " "
BECOME AN XCODER 5
28
[2]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Julia is a pretty actress") //
[pool release]; //[2.9]
return 0;
}
Build [2.9]
Xcode
error: parse error before "release".
parse
import
[2.1] # [2.8]
[2.9]
Apple [1] [3]
[3]
#import <Foundation/Foundation.h>
float circleArea(float theRadius); // [3.3]
BECOME AN XCODER 5
29
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;
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;
}
main() cir-
cleArea() [3.22] rectangleArea() [3.29]
main() [3.5] main() Apple
Area of picture: 36.000000. Area of circle: 78.54.
justatry has exited with status 0.
BECOME AN XCODER 5
30
Xcode
Xcode " " breakpoint
Build and Go popup menu
Build and Debug
BECOME AN XCODER 5
31
Xcode debugger
Continue debugger
Mac OS X
Objective-C
GUI
BECOME AN XCODER 5
32
6
[1.2]
[1]
int age = 42;
if (age > 30) // > " "
{
NSLog(@"age is older than thirty."); //[1.4]
}
NSLog(@"Finished."); //[1.6]
[1.2] if...
true
age > 30 [1.4]
[1.6] if
if...else [2]
[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]
==
>
BECOME AN XCODER 6
33
<
>=
<=
!=
-
if
true false
Objective-C true false 1 0 BOOL
"true" 1 YES "false"
0 NO
[3]
int x = 3;
BOOL y;
y = (x == 4); // y 0
AND & &&
OR | ||
[4]
if ( (age >= 18) && (age < 65) )
{
NSLog(@"Probably has to work for a living.");
}
[5]
if (age >= 18)
{
BECOME AN XCODER 6
34
if (age < 65)
{
NSLog(@"Probably has to work for a living.");
}
}
BECOME AN XCODER 6
35
7 while
[1]
[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] [1.4] 10 x 1
x <= 10 x 1
x x++
x 2 10 10
x 11 x <= 10
[2.6] loop x 11 10
x++
[3]
[3]
float celsius, tempInFahrenheit;
for (tempInFahrenheit = 0; tempInFahrenheit <= 200; tempInFahrenheit =
tempInFahrenheit + 20)
BECOME AN XCODER 7 while
36
{
celsius = (tempInFahrenheit - 32.0) * 5.0 / 9.0;
NSLog(@"%10.2f -> %10.2f", tempInFahrenheit, celsius);
}
0.00 -> -17.78
20.00 -> -6.67
40.00 -> 4.44
60.00 -> 15.56
80.00 -> 26.67
100.00 -> 37.78
120.00 -> 48.89
140.00 -> 60.00
160.00 -> 71.11
180.00 -> 82.22
200.00 -> 93.33
Objective-C
while () { }
do {} while ()
for-loop
false loop
[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);
counter 11
do {} while ()
[5]
BECOME AN XCODER 7 while
37
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);
counter 11
GUI
BECOME AN XCODER 7 while
38
8 GUI
Objective-C
GUI Objective-C C
C Objective-C C
"Objective" Objective-C object
Objective-C
Objective-C
Objective-C
Safari Objective-C
Mac Safari
message
BECOME AN XCODER 8 (GUI)
39
close
Safari
Safari Apple
a.
b. template Safari
b class
behave
instance 76
BECOME AN XCODER 8 (GUI)
40
Mac minimize
Dock
access
close " "
close methods
RAM
button text field
close
BECOME AN XCODER 8 (GUI)
41
Mac
message sending expression
Objective-C [1.1] [1.2]
[1]
[receiver message];
[receiver message:argument];
[1.2]
Xcode Application
Cocoa Application GUI
Xcode Groups & Files Resources
MainMenu.nib Double-click
BECOME AN XCODER 8 (GUI)
42
Xcode MainMenu.nib
Interface Builder File
Hide Others "Window"
"Window"
"Cocoa-" GUI
" " palettes window
GUI "Window"
"System Font Text" GUI
GUI
BECOME AN XCODER 8 (GUI)
43
NSButton
NSTextView Apple
"Window"
p r o p e r t i e s c o m m a n d - s h i f t -
i "Window" In-
stances command-shift-i
pop-up menu Attributes Textured Window
brushed-steel look
Interface Builder inspector
behavior
inherit Objective-C C
BECOME AN XCODER 8 (GUI)
44
NSWindow
"close"
" " superclass
hierarchy of inheritance
Objective-C sig-
nal error
implement
override
Apple
Apple
Apple close
invoke close
close recursive
[2]
//
[super close]; // close
"NSObject"
NSObject subclass NSWindow NSResponder
NSObject NSObject
MainMenu.nib Classes NSObject
Classes Subclass NSObject MainMenu.nib
"MAFoo"
BECOME AN XCODER 8 (GUI)
45
MAFoo
MAFoo My Application
NS NS Apple NextStep
Apple Next, Inc. NextStep Mac OS X Steve
Jobs
CocoaDev wiki
http://www.cocoadev.com/index.pl?ChooseYourOwnPrefix
Cocoa NSWindow
NSColor MAFoo
Interface Builder Classes Instantiate MAFoo In-
stances MAFoo
MAFoo
BECOME AN XCODER 8 (GUI)
46
MAFoo
MAFoo
reference
MAFoo MAFoo
MAFoo
MAFoo
MAFoo MAFoo MAFoo
MAFoo MAFoo trig-
ger FAFoo
outlet
MainFile.nib Classes MAFoo
command-shift-i inspector inspector
Action Add action MAFoo
"setTo5:"
5 Interface Builder
"reset:" 0
":"
BECOME AN XCODER 8 (GUI)
47
MAFoo
inspector Outlet outlet "text-
Field"
outlet MAFoo
MAFoo 5 "Set to 5"
"Reset"
BECOME AN XCODER 8 (GUI)
48
a) "Reset" MAFoo
b) "Set to 5" MAFoo
c) MAFoo
MainFile.nib Instances Control
Reset MaFoo
MAFoo
MAFoo
inspector MAFoo
"reset:" Connect
BECOME AN XCODER 8 (GUI)
49
inspector
MAFoo reset:
Set to 5 MAFoo
MAFoo MAFoo control
Connect
MainMenu.nib MAFoo Classes
MAFoo Classes
Create Files for MAFoo Interface Builder
BECOME AN XCODER 8 (GUI)
50
MAFoo
Xcode Other
Sources
Classes
Xcode
BECOME AN XCODER 8 (GUI)
51
4 [11.1]
MAFoo.h header file
[3.5] NSObject
NSObject
[3]
/* MAFoo */
#import <Cocoa/Cocoa.h> // [3.3]
@interface MAFoo : NSObject
{
IBOutlet id textField; // [3.7]
}
- (IBAction)reset:(id)sender;
- (IBAction)setTo5:(id)sender;
@end
[3.7] outlet "id" "IB" Interface Buil-
der
IBAction [3.9 3.10] void
MAFoo
Interface Builder Actions
#import <Foundation/Foundation.h> [3.3]
MAFoo.m
[4]
#import "MAFoo.h"
@implementation MAFoo
- (IBAction)reset:(id)sender // [4.5]
{
}
BECOME AN XCODER 8 (GUI)
52
- (IBAction)setTo5:(id)sender
{
}
@end
MAFoo.h
reset: setTo5:
MAFoo
Interface Builder 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 outlet
Interface Builder outlet
setIntValue: setIntValue:
Xcode Build
and Go
BECOME AN XCODER 8 (GUI)
53
BECOME AN XCODER 8 (GUI)
54
9
Apple setIntValue:
Apple
bug-free
Interface Builder
"NSButton"
''System Font Text" "NSTextField"
NSTextField
Xcode Help->Documentation Cocoa
"NSTextField" API-Search
NSText-
Field
NSTextField Class NSTextField
BECOME AN XCODER 9
55
Xcode Cocoa
NSObject
Method Types
NSTextField
NSControl NSView
HTML Inherits from
NSControl NSControl
NSControl
Inherits from NSView : NSResponder : NSObject
Setting the control s value
– setIntValue:
BECOME AN XCODER 9
56
setIntValue:
setIntValue:
- (void)setIntValue:(int)anInt
Sets the value of the receiver’s cell (or selected cell) to
the integer anInt. If the cell is being edited, it aborts all
editing before setting the value; if the cell doesn’t inherit
from NSActionCell, it marks the cell’s interior as needing to
be redisplayed (NSActionCell performs its own updating of
cells).
NSTextField
signature
Objective-C
void setInt-
Value: textField MAFood
(int) anInt
5 0
textField
" " Accessors
setIntValue:
[1]
- (int) intValue
textfield
[2]
resultReceived = [textField intValue];
Objective-C
BECOME AN XCODER 9
57
"
" polymorphism object-oriented programming
run-time
GUI setIntValue:
BECOME AN XCODER 9
58
10 awakeFromNib
Apple
framework [12] Foundation Kit
Application Kit
GUI
nib nib Next Interface Buil-
der Application Kit
Xcode Help Documentation
Full-Text Search
Full-Text Search Ap-
plication Kit Return Xcode Applica-
tion Kit Reference for Objective-C
Protocols NSNibAwakening
NSNibAwaking
Declared in AppKit/NSNibLoading.h
Programming Topic Loading Resources
Contents:Protocol DescriptionMethod TypesInstance Methods
Protocol Description
This informal protocol consists of a single method, awakeFrom-
Nib. Classes can implement this method to perform final ini-
tialization of state after objects have been loaded from an
Interface Builder archive.
BECOME AN XCODER 10 awakeFromNib
59
nib
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
awakeFromNib
BECOME AN XCODER 10 awakeFromNib
60
11
C
- pointer
- Objective-C
Mac
[1]
int x = 4;
Mac
x
[1] int
x long long double
"x = 4" 4 x
x address
x
x
pointer
& x
&x
expression x evaluate x
4 &x x
[2]
int *y;
y int y
x x y
BECOME AN XCODER 11
61
[3]
y = &x;
*y
4 "x"
*y = 5
"x = 5"
1
[4]
void increment(int *y)
{
*y = *y + 1;
}
[5]
int x = 4;
increment(&x);
// x 5
BECOME AN XCODER 11
62
12
integer long float
double BOOL pointer
string 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 NSMutableString NSString
[2]
NSString *favoriteComputer;
favoriteComputer = @"Mac!";
NSLog(favoriteComputer);
[2.1]
11
[2]
[3]
int *y;
y [2.1]
favoriteComputer NSString
Objective-C
@ Objective-C C
C Objective-C
Objective-C @
BECOME AN XCODER 12
63
Objective-C C Objective-C Uni-
code ASCII Unicode
[4]
[4]
NSString *favoriteActress = @"Julia";
favoriteActress "Julia"
favoriteComputer
[5] NSString
[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"; //
NSLog(x);
[pool release];
return 0;
}
MacBook Pro Intel
NSString immutable
Objective-C
BECOME AN XCODER 12
64
NSMutableString
length [6]
[6]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int theLength;
NSString * foo;
foo = @"Julia!";
theLength = [foo length]; // [6.10]
NSLog(@"The length is %d.", theLength);
[pool release];
return 0;
}
The length is 6.
foo bar
x
[6.10] foo length length NSString
- (unsigned int)length
Returns the number of Unicode characters in the receiver.
[7]
uppercaseString
NSString
[7]
BECOME AN XCODER 12
65
#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!
NSMutableString NSMutableString
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;
}
BECOME AN XCODER 12
66
Here is the result: Julia! I am happy.
[8.8] mutableCopy NSString
mutable [8.8] foo
"Julia!"
Objective-C
[8.7] Objective-
C " " " "
" "
[8.7] foo "Julia!"
foo bar
bar = foo;
foo bar
foo [foo dosomething];
bar [bar dosomething];
[9]
#import <Foundation/Foundation.h>
"foo"
"foo"
"bar"
BECOME AN XCODER 12
67
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
8
MAFoo
BECOME AN XCODER 12
68
13
array
index
element
1
1
NSArray NSMutableArray
[NSMutableArray array]
… …
NSMutableArray
Objective-C
meta-classes
Cocoa
"+" "-" 8 [4.5]
array
array
+ (id)array
BECOME AN XCODER 13
69
Creates and returns an empty array. This method is used by mu-
table subclasses of NSArray.
See Also: + arrayWithObject:, + arrayWithObjects:
[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
objectAtIndex: [2.13]
[2]
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
BECOME AN XCODER 13
70
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;
}
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;
BECOME AN XCODER 13
71
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
NSArray NSMutableArray
replace
replaceObjectAtIndex:withObject:
Objective-C
[4]
[myArray replaceObjectAtIndex:1 withObject:@"Hello"];
1 @"Hello"
valid
Objective-C
" " Smalltalk
Objective-C expressive
" "
Objective-C
BECOME AN XCODER 13
72
14
Mac RAM
free
destroy
Cocoa
" " retain count
1
2
3
=
3
BECOME AN XCODER 14
73
retain
release Cocoa "autore-
lease pool" release -
autorelease pool
autorelease autorelease pool release
autorelease pool
autorelease pool
Cocoa reference count-
ing 15 Cocoa
Apple Apple Cocoa
automatic garbage collection
Apple
Apple 2006 WWDC World Wide Developer Conference
2007 Xcode 3.0 automatic garbage collection
Objective-C 2.0
BECOME AN XCODER 14
74
15
Xcode Objective-C
Apple
Apple
http://www.apple.com/developer
http://osx.hyperjeff.net/reference/CocoaArticles.php
http://www.cocoadev.com
http://www.cocoabuilder.com
http://www.stepwise.com
http://lists.apple.com/mailman/listinfo/cocoa-dev cocoa-dev
mailing list
http://www.cocoabuilder.com
http://www.catb.org/~esr/faqs/smart-questions.html "How To Ask Questions The
Smart Way" " " http://mis.ndhu.edu.tw/docu/
question.htm
Cocoa Stephen Kochan Programming in Objective-C
Big Nerd Ranch Aaron Hillegass Cocoa Programming for Mac OS X
Xcode James Duncan Davidson Apple O'Reilly Cocoa
with Objective-C
Mac
A p p l e
http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuid
elines/index.html
BECOME AN XCODER 15
75
Xcode 0
Bert, Alex, Philippe.
BECOME AN XCODER 15
76