become an tchinese
Post on 05-Apr-2018
225 Views
Preview:
TRANSCRIPT
-
7/31/2019 Become an TChinese
1/76
BECOME AN XCODER
1
mailto:shivahuang@gmail.com -
7/31/2019 Become an TChinese
2/76
-
7/31/2019 Become an TChinese
3/76
__________________________________________________ 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
-
7/31/2019 Become an TChinese
4/76
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
-
7/31/2019 Become an TChinese
5/76
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
-
7/31/2019 Become an TChinese
6/76
-
7/31/2019 Become an TChinese
7/76
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]
compilerMac 0 1
Mac
BECOME AN XCODER 1
7
-
7/31/2019 Become an TChinese
8/76
b u g
debugging
x
pictureWidth [2]
[2]
pictureWidth = 8;
pictureWidth pictureWIDTH PictureWidth
[2]
Objective-C Objective-C
pictureWidth
"_"
door8kdo8or do_or
door 88door
Door8
[3]
[3]
pictureWidth=8;
pictureHeight=6;
pictureSurfaceArea=pictureWidth*pictureHeight;
BECOME AN XCODER 1
8
-
7/31/2019 Become an TChinese
9/76
[4]
pictureWidth = 8;
pictureHeight = 6;
pictureSurfaceArea = pictureWidth * pictureHeight;
[5]
[5]
pictureWidth = 8;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
integers [5.1] [5.2]
f loating-
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/31/2019 Become an TChinese
10/76
[7]
unsigned int chocolateBarsInStock;
unsigned intunsigned 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
-
7/31/2019 Become an TChinese
11/76
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
-
7/31/2019 Become an TChinese
12/76
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
-
7/31/2019 Become an TChinese
13/76
2
[1]
[1]
float pictureWidth, pictureHeight, pictureSurfaceArea;
pictureWidth = 8.0;
pictureHeight = 4.5;
pictureSurfaceArea = pictureWidth * pictureHeight;
//
Xcode /* */
/*
*/
Xcode
outcomment /* */
BECOME AN XCODER 2
13
-
7/31/2019 Become an TChinese
14/76
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
-
7/31/2019 Become an TChinese
15/76
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
-
7/31/2019 Become an TChinese
16/76
// 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
-
7/31/2019 Become an TChinese
17/76
[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
-
7/31/2019 Become an TChinese
18/76
int main()
{
float pictureWidth, pictureHeight, pictureSurfaceArea, circleRadius, circleSurfaceArea; pictureWidth = 8; pictureHeight = 4.5; circleRadius = 5.0; pictureSurfaceArea = pictureWidth * pictureHeight; circleSurfaceArea = circleArea(circleRadius); return0; // [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
-
7/31/2019 Become an TChinese
19/76
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
-
7/31/2019 Become an TChinese
20/76
4
Cocoa
NSLog()
""
NSLog()
Cocoa
NSLog()
[1]
int main()
{
NSLog(@"Julia is a pretty actress."); return0;}
[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
-
7/31/2019 Become an TChinese
21/76
[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
-
7/31/2019 Become an TChinese
22/76
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
-
7/31/2019 Become an TChinese
23/76
[10.3]
%d%fintfloat
[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()
importNSLog()
#import
[11]
#import
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
-
7/31/2019 Become an TChinese
24/76
NSLog(@"Area of circle: %10.2f.", circleSurfaceArea);
NSLog(@"Area of picture: %f. ", pictureSurfaceArea);
return0;
}
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
-
7/31/2019 Become an TChinese
25/76
5
Mac Mac
compiler Mac
Apple Xcode Mac OS X
X c o d e
http://developer.apple.com
Xcode DeveloperApplications
Preferences File New Pro-
ject
Xcode
Objective-C GUIGraphical User
InterfaceCommand Line UtilityFoundation Tool
BECOME AN XCODER 5
25
-
7/31/2019 Become an TChinese
26/76
justatry
Finish
Terminal
"Groups & Files"
GUI GUI
Xcode"Groups"
Groups & FilesjustatrySource
justatry.m [1] main()
main()
justatry.mApple
main()
BECOME AN XCODER 5
26
-
7/31/2019 Become an TChinese
27/76
Xcode main()
[1]#import
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] return0;}
NSLog() import
main()
BECOME AN XCODER 5
27
-
7/31/2019 Become an TChinese
28/76
NSlog
return 0;
main() [1.3]
NSAutoreleasePool [1.5]
pool release [1.9]
""
main()
[1]
[1.31.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
-
7/31/2019 Become an TChinese
29/76
[2]
#import
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] return0;}
Build [2.9]
Xcode
error: parse error before "release".
parse
import
[2.1]# [2.8]
[2.9]
Apple [1] [3]
[3]
#import
float circleArea(float theRadius); // [3.3]
BECOME AN XCODER 5
29
-
7/31/2019 Become an TChinese
30/76
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]; return0;}
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
-
7/31/2019 Become an TChinese
31/76
Xcode
Xcode ""breakpoint
Build and Gopopup menu
Build and Debug
BECOME AN XCODER 5
31
-
7/31/2019 Become an TChinese
32/76
Xcode debugger
Continue debugger
Mac OS X
Objective-C
GUI
BECOME AN XCODER 5
32
-
7/31/2019 Become an TChinese
33/76
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
-
7/31/2019 Become an TChinese
34/76
<
>=
= 18) && (age < 65) )
{
NSLog(@"Probably has to work for a living.");}
[5]
if (age >= 18){
BECOME AN XCODER 6
34
-
7/31/2019 Become an TChinese
35/76
if (age < 65) { NSLog(@"Probably has to work for a living."); }}
BECOME AN XCODER 6
35
-
7/31/2019 Become an TChinese
36/76
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
-
7/31/2019 Become an TChinese
37/76
{
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.33Objective-C
while () { }
do {} while ()
for-loop
false loop
[4]
int counter = 1;
while (counter
-
7/31/2019 Become an TChinese
38/76
int counter = 1;
do
{
NSLog(@"Julia is a pretty actress.\n"); counter = counter + 1;}
while (counter
-
7/31/2019 Become an TChinese
39/76
8
GUI
Objective-C
GUIObjective-C C
C Objective-C C
"Objective"Objective-Cobject
Objective-C
Objective-C
Objective-C
Safari Objective-C
Mac Safari
message
BECOME AN XCODER 8 (GUI)
39
-
7/31/2019 Become an TChinese
40/76
close
Safari
SafariApple
a.
b. template Safari
bclass
behave
instance 76
BECOME AN XCODER 8 (GUI)
40
-
7/31/2019 Become an TChinese
41/76
Mac minimize
Dock
access
close ""
closemethods
RAM
buttontext field
close
BECOME AN XCODER 8 (GUI)
41
-
7/31/2019 Become an TChinese
42/76
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.nibDouble-click
BECOME AN XCODER 8 (GUI)
42
-
7/31/2019 Become an TChinese
43/76
XcodeMainMenu.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
-
7/31/2019 Become an TChinese
44/76
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 menuAttributesTextured Window
brushed-steel look
Interface Builderinspector
behavior
inherit Objective-C C
BECOME AN XCODER 8 (GUI)
44
-
7/31/2019 Become an TChinese
45/76
NSWindow
"close"
""superclass
hierarchy of inheritance
Objective-C sig-
nalerror
implement
override
Apple
Apple
Apple close
invokeclose
closerecursive
[2]
//
[super close]; // close
"NSObject"
NSObject subclassNSWindow NSResponder
NSObject NSObject
MainMenu.nibClasses NSObject
Classes Subclass NSObjectMainMenu.nib
"MAFoo"
BECOME AN XCODER 8 (GUI)
45
-
7/31/2019 Become an TChinese
46/76
MAFoo
MAFoo My Application
NSNS 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 ClassesInstantiate MAFoo In-
stances MAFoo
MAFoo
BECOME AN XCODER 8 (GUI)
46
-
7/31/2019 Become an TChinese
47/76
MAFoo
MAFoo
reference
MAFoo MAFoo
MAFoo
MAFoo
MAFoo MAFoo MAFoo
MAFoo MAFoo trig-
ger FAFoo
outlet
MainFile.nibClassesMAFoo
command-shift-i inspector inspector
ActionAddaction MAFoo "setTo5:"
5 Interface Builder
"reset:" 0
":"
BECOME AN XCODER 8 (GUI)
47
-
7/31/2019 Become an TChinese
48/76
MAFoo
inspectorOutlet outlet "text-
Field"
outletMAFoo
MAFoo 5 "Set to 5"
"Reset"
BECOME AN XCODER 8 (GUI)
48
-
7/31/2019 Become an TChinese
49/76
a) "Reset"MAFoo
b) "Set to 5"MAFoo
c) MAFoo
MainFile.nibInstances Control
ResetMaFoo
MAFoo
MAFoo
inspector MAFoo
"reset:"Connect
BECOME AN XCODER 8 (GUI)
49
-
7/31/2019 Become an TChinese
50/76
inspector
MAFoo reset:
Set to 5MAFoo
MAFoo MAFoo control
Connect
MainMenu.nib MAFoo Classes
MAFoo Classes
Create Files for MAFoo Interface Builder
BECOME AN XCODER 8 (GUI)
50
-
7/31/2019 Become an TChinese
51/76
MAFoo
Xcode Other
Sources
Classes
Xcode
BECOME AN XCODER 8 (GUI)
51
-
7/31/2019 Become an TChinese
52/76
4 [11.1]
MAFoo.hheader file
[3.5] NSObject
NSObject
[3]
/* MAFoo */
#import // [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.93.10] void
MAFoo
Interface Builder Actions
#import [3.3]
MAFoo.m
[4]
#import "MAFoo.h"
@implementation MAFoo
- (IBAction)reset:(id)sender // [4.5]
{
}
BECOME AN XCODER 8 (GUI)
52
-
7/31/2019 Become an TChinese
53/76
- (IBAction)setTo5:(id)sender
{
}
@end
MAFoo.h
reset:setTo5:
MAFoo
Interface Builder MAFoo
MAFoo [5.75.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
-
7/31/2019 Become an TChinese
54/76
BECOME AN XCODER 8 (GUI)
54
-
7/31/2019 Become an TChinese
55/76
9
Apple setIntValue:
Apple
bug-free
Interface Builder "NSButton"
''System Font Text" "NSTextField"
NSTextField
XcodeHelp->DocumentationCocoa
"NSTextField" API-Search
NSText-
Field
NSTextField Class NSTextField
BECOME AN XCODER 9
55
-
7/31/2019 Become an TChinese
56/76
Xcode Cocoa
NSObject
Method Types
NSTextField
NSControl NSView
HTMLInherits from
NSControl NSControl
NSControl
Inherits from NSView : NSResponder : NSObject
Setting the controls value
setIntValue:
BECOME AN XCODER 9
56
-
7/31/2019 Become an TChinese
57/76
setIntValue:
setIntValue:
- (void)setIntValue:(int)anInt
Sets the value of the receivers 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 doesnt inherit
from NSActionCell, it marks the cells interior as needing to
be redisplayed (NSActionCell performs its own updating of
cells).
NSTextField
signature
Objective-C
voidsetInt-
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
-
7/31/2019 Become an TChinese
58/76
"
" polymorphismobject-oriented programming
run-time
GUI setIntValue:
BECOME AN XCODER 9
58
-
7/31/2019 Become an TChinese
59/76
10
awakeFromNib
Apple
framework [12] Foundation Kit
Application Kit
GUI
nib nib Next Interface Buil-
der Application Kit
XcodeHelpDocumentation
Full-Text Search
Full-Text SearchAp-
plication Kit ReturnXcodeApplica-
tion Kit Reference for Objective-C
ProtocolsNSNibAwakening
NSNibAwaking
Declared in AppKit/NSNibLoading.hProgramming Topic Loading Resources
Contents:
Protocol DescriptionMethod Types
Instance 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
-
7/31/2019 Become an TChinese
60/76
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
-
7/31/2019 Become an TChinese
61/76
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
expressionx evaluatex 4&xx
[2]
int *y;
y inty
xxy
BECOME AN XCODER 11
61
-
7/31/2019 Become an TChinese
62/76
[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
-
7/31/2019 Become an TChinese
63/76
12
integerlongfloat
doubleBOOL 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
-
7/31/2019 Become an TChinese
64/76
Objective-C C Objective-C Uni-
code ASCII Unicode
[4]
[4]
NSString *favoriteActress = @"Julia";
favoriteActress "Julia"
favoriteComputer
[5] NSString
[5]
#import
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
NSStringimmutable
Objective-C
BECOME AN XCODER 12
64
-
7/31/2019 Become an TChinese
65/76
NSMutableString
length [6]
[6]
#import
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.
foobar
x
[6.10]foolengthlength NSString
- (unsigned int)length
Returns the number of Unicode characters in the receiver.
[7]
uppercaseString
NSString
[7]
BECOME AN XCODER 12
65
-
7/31/2019 Become an TChinese
66/76
#import
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
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
-
7/31/2019 Become an TChinese
67/76
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
"foo"
"foo"
"bar"
BECOME AN XCODER 12
67
-
7/31/2019 Become an TChinese
68/76
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
-
7/31/2019 Become an TChinese
69/76
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
-
7/31/2019 Become an TChinese
70/76
Creates and returns an empty array. This method is used by mu-
table subclasses of NSArray.
See Also: + arrayWithObject:, + arrayWithObjects:
[1]
#import
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]; return0;}
There are 3 elements in my array
0
objectAtIndex: [2.13]
[2]
#import
int main (int argc, const char * argv[])
{
BECOME AN XCODER 13
70
-
7/31/2019 Become an TChinese
71/76
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]; return0;}
The element at index 0 in the array is: first string
[3]
#import
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
-
7/31/2019 Become an TChinese
72/76
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-Cexpressive
""
Objective-C
BECOME AN XCODER 13
72
-
7/31/2019 Become an TChinese
73/76
14
Mac RAM
free
destroy
Cocoa
""retain count
1
2
3
=
3
BECOME AN XCODER 14
73
-
7/31/2019 Become an TChinese
74/76
retain
releaseCocoa "autore-
lease pool" release -
autorelease pool
autoreleaseautorelease poolrelease 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
-
7/31/2019 Become an TChinese
75/76
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
-
7/31/2019 Become an TChinese
76/76
Xcode 0
Bert, Alex, Philippe.
BECOME AN XCODER 15
top related