ObjectiveC最新汉化版全集教程
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
----------------------- Page -----------
Objective-C Beginner's Guide
目录
l 始吧
°下载这篇教学
°设定环境
°前言
°编译hello world
l 创建Classes
° @interface
° @implementation
°把它们凑在一路
l 详细说明...
°多重参数
°建构子(Constructors )
°存取权限
° Class level access
°异样情形(Exceptions )处置
l 继承、多型(Inheritance, Polymorphism)和其它对象导向功能°id 型别
°继承(Inheritance)
°动态识别(Dynamic types)
° Categories
° Posing
° Protocols
l 内存治理
°Retain and Release (保留与释放)
° Dealloc
° Autorelease Pool
l Foundation Framework Classes
° NSArray
° NSDictionary
l 优势与缺点
l 更多信息
开始吧
----------------------- Page 2-----------------------
Objective-C Beginner's Guide 页码,2/43
o 设定环境
n Linux/FreeBSD: 安装GNUStep
n 为了编译GNUstep 应用程序,必需先执行位
于/usr/GNUstep/System/Makefiles/ 的那个档案。
那个途径取决于你的系统环境,有些是在/usr, some /usr/lib ,有些
是/usr/local。
若是你的shell 是以csh/tcsh 为基础的shell,那么应该改用。
建议把那个指令放在.bashrc 或.cshrc 中。
n Mac OS X: 安装XCode
n Windows NT : 安装cygwin 或mingw,然后安装GNUStep o 前言
n 这篇教学假设你已经有一些大体的C 语言知识,包括C 数据型别、什么是
函式、什么是回传值、关于指针的知识和大体的C 语言内存治理。
若是
您没有这些背景知识,我超级建议你读一读K&R 的书:The C Programming
Language (译注:台湾出版书名为C 程序语言第版)这是C 语言的设计者
所写的。
n Objective-C,是C 的衍生语言,继承了所有C 语言的特性。
是有一些例外,
可是它们不是继承于C 的语言特性本身。
n nil:在C/C++ 你或许曾利用过NULL,而在Objective-C 中那么是nil 。
不同之
处是你能够传递讯息给nil (例如[nil message]; ),这是完全合法的,但是你
却不能对NULL 依样画葫芦。
n BOOL:C 没有正式的布尔型别,而在Objective-C 中也不是「真的」有。
它
是包括在Foundation classes (大体类别库)中(即import ;nil 也
是包括在那个标头档内)。
BOOL 在Objective-C 中有两种型态:YES 或
NO,而不是TRUE 或FALSE。
n #import vs #include:就犹如你在hello world 范例中看到的,咱们利用了
#import 。
#import 由gcc 编译器支持。
我并非建议利用#include,#import 大体
上跟.h 档头尾的#ifndef #define #endif 相同。
许多程序员们都同意,利用这
些东西这是十分愚蠢的。
不管如何,利用#import 就对了。
如此不但能够避
gcc
Objective-C
免麻烦,而且万一有一天把它拿掉了,将会有足够的程序员
能够坚持保留它或是将它放回来。
偷偷告知你,Apple 在它们官方的程序代
码中也利用了#import 。
因此万一有一天这种事真的发生,不难预料Apple 将
会提供一个支持#import 的gcc 分支版本。
n 在Objective-C 中,method 及message 这两个字是能够互换的。
只是messages
拥有专门的特性,一个message 能够动态的转送给另一个对象。
在Objective-
C 中,呼对象上的一个讯息并非必然表示对象真的会实作那个讯息,而是
对象明白如何以某种方式去实作它,或是转送给明白如何实作的对象。
o 编译hello world
n
S #import <>
S
S int main( int argc, const char *argv[] ) {
S printf( "hello world\n" );
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 3-----------------------
Objective-C Beginner's Guide 页码,3/43
S return 0;
}
n 输出
hello world
n 在Objective-C 中利用#import 代替#include
n Objective-C 的预设扩展名是.m
创建classes
o @interface
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing 一中
的范例,并通过许诺而刊载。
n
S #import <Foundation/>
S
S @interface Fraction: NSObject {
S int numerator;
S int denominator;
S }
S
S -(void) print;
S -(void) setNumerator: (int) d;
S -(void) setDenominator: (int) d;
S -(int) numerator;
S -(int) denominator;
S @end
n NSObject:NeXTStep Object 的缩写。
因为它已经更名为OpenStep,因此这在
今天已经不是那么成心义了。
n 继承(inheritance )以Class: Parent 表示,就像上面的Fraction: NSObject。
n 夹在@interface Class: Parent { .... } 中的称为instance variables。
n 没有设定存取权限(protected, public, private )时,预设的存取权限为
protected。
设定权限的方式将在稍后说明。
n Instance methods 跟在成员变数(即instance variables)后。
格式为:scope
(returnType) methodName: (parameter1Type)
parameter1Name;
n scope 有class 或instance 两种。
instance methods 以- 头,class level
methods 以+ 头。
n Interface 以一个@end 作为终止。
o @implementation
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing 一中
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 4-----------------------
Objective-C Beginner's Guide 页码,4/43
的范例,并通过许诺而刊载。
n
S #import ""
S #import <>
S
S @implementation Fraction
S -(void) print {
S printf( "%i/%i", numerator, denominator );
S }
S
S -(void) setNumerator: (int) n {
S numerator = n;
S }
S
S -(void) setDenominator: (int) d {
S denominator = d;
S }
S
S -(int) denominator {
S return denominator;
S }
S
S -(int) numerator {
S return numerator;
S }
@end
n Implementation 以@implementation ClassName 开始,以@end 终止。
n Implement 概念好的methods 的方式,跟在interface 中宣告时很近似。
o 把它们凑在一路
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing 一中
的范例,并通过许诺而刊载。
n
S #import <>
S #import ""
S
S int main( int argc, const char *argv[] ) {
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 5-----------------------
Objective-C Beginner's Guide 页码,5/43
S [frac print];
S printf( "\n" );
S
S .
o 多重参数
n 目前为止我还没展现如何传递多个参数。
那个语法乍看之下不是很直觉,不
过它却是来自一个十分受欢迎的Smalltalk 版本。
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S ...
S -(void) setNumerator: (int) n andDenominator: (int) d;
...
n
S ...
S -(void) setNumerator: (int) n andDenominator: (int) d {
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6 PDF created with pdfFactory Pro trial version
----------------------- Page 6-----------------------
Objective-C Beginner's Guide 页码,6/43
S numerator = n;
S denominator = d;
S }
...
n
S #import <>
S #import ""
S
S int main( int argc, const char *argv[] )
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 7-----------------------
Objective-C Beginner's Guide 页码,7/43
略label 名称,但以: 区隔参数。
并非建议如此利用。
o 建构子(Constructors )
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S ...
S -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
...
n
S ...
S -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
S self = [super init];
S
S if ( self ) {
S [self setNumerator: n andDenominator: d];
S }
S
S return self;
S }
...
n
S #import <>
S #import ""
S
S int main( int argc, const char *argv[] ) {
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 8-----------------------
Objective-C Beginner's Guide 页码,8/43
S [frac2 print];
S printf( "\n" );
S
S printf( "Fraction 3 is: " );
S [frac3 print];
S printf( "\n" );
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 9-----------------------
Objective-C Beginner's Guide 页码,9/43
S @private
S int privateVar;
S int privateVar2;
S @protected
S int protectedVar;
S }
@end
n
S #import ""
S
S @implementation Access
@end
n
S #import ""
S #import <>
S
S int main( int argc, const char *argv[] ) {
S Access *a = [[Access alloc] init];
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,10/43
S
S static int count;
S
S @interface ClassA: NSObject
S +(int) initCount;
S +(void) initialize;
@end
n
S #import ""
S
S @implementation ClassA
S -(id) init {
S self = [super init];
S count++;
S return self;
S }
S
S +(int) initCount {
S return count;
S }
S
S +(void) initialize {
S count = 0;
S }
@end
n
S #import ""
S #import <>
S
S int main( int argc, const char *argv[] ) {
S ClassA *c1 = [[ClassA alloc] init];
S ClassA *c2 = [[ClassA alloc] init];
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,11/43
}
n output
S ClassA count: 2
ClassA count: 3
n static int count = 0; 这是class variable 宣告的方式。
其实这种变量摆在那个地址并
不睬想,比较好的解法是像Java 实作static class variables 的方式。
但是,它
确实能用。
n +(int) initCount; 这是回传count 值的实际method。
请注意这细的不同!这
里在type 前面不用减号- 而改用加号+。
加号+ 表示这是一个class level
function。
(译注:许多文件中,class level functions 被称为class functions 或
class method)
n 存取那个变数跟存取一样成员变数没有两样,就像ClassA 中的count++ 用
法。
n +(void) initialize method is 在Objective-C 开始执行你的程序时被呼,而且它
也被每一个class 呼。
这是初始化像咱们的count 这种class level variables 的好
地址。
o 异样情形(Exceptions)
n 注意:异样处置只有Mac OS X 以上才支持。
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S #import <Foundation/>
S
S @interface CupWarningException: NSException
@end
n
S #import ""
S
S @implementation CupWarningException
@end
n
S #import <Foundation/>
S
S @interface CupOverflowException: NSException
@end
n
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6 PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,12/43
S #import ""
S
S @implementation CupOverflowException
@end
n
S #import <Foundation/>
S
S @interface Cup: NSObject {
S int level;
S }
S
S -(int) level;
S -(void) setLevel: (int) l;
S -(void) fill;
S -(void) empty;
S -(void) print;
@end
n
S #import ""
S #import ""
S #import ""
S #import <Foundation/>
S #import <Foundation/>
S
S @implementation Cup
S -(id) init {
S self = [super init];
S
S if ( self ) {
S }
S
S return self;
S }
S
S -(int) level {
S return level;
S }
S
S -(void) setLevel: (int) l {
S level = l;
S
S if ( level > 100 ) {
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,13/43
S reason: @"The level is above 100"
S @throw e;
S } else if ( level >= 50 ) {
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,14/43
S [cup fill];
S [cup print];
S }
S
S . }
n 还有一个finally 区块,它的行为就像Java 的异样处置方式,finally 区块的内
容保证会被呼。
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,15/43
n 里的@"CupOverflowException" 是一个NSString 常数物件。
在
Objective-C 中,@ 符号通经常使用来代表这是语言的衍生部份。
C 语言形式的字
符串(C string )就像C/C++ 一样是"String constant" 的形式,型别为char * 。
继承、多型(Inheritance, Polymorphism)以
及其它对象导向功能
o id 型别
n Objective-C 有种叫做id 的型别,它的运作有时候像是void*,只是它却严格
规定只能用在对象。
Objective-C 与Java 跟C++ 不一样,你在呼一个对象
的method 时,并非需要明白那个对象的型别。
固然那个method 必然要存
在,这称为Objective-C 的讯息传递。
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S #import <Foundation/>
S
S @interface Fraction: NSObject {
S int numerator;
S int denominator;
S }
S
S -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
S -(void) print;
S -(void) setNumerator: (int) d;
S -(void) setDenominator: (int) d;
S -(void) setNumerator: (int) n andDenominator: (int) d;
S -(int) numerator;
S -(int) denominator;
@end
n
S #import ""
S #import <>
S
S @implementation Fraction
S -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
S self = [super init];
S
S if ( self ) {
S [self setNumerator: n andDenominator: d];
S }
S
S return self;
S }
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,16/43
S
S -(void) print {
S printf( "%i / %i", numerator, denominator );
S }
S
S -(void) setNumerator: (int) n {
S numerator = n;
S }
S
S -(void) setDenominator: (int) d {
S denominator = d;
S }
S
S -(void) setNumerator: (int) n andDenominator: (int) d {
S numerator = n;
S denominator = d;
S }
S
S -(int) denominator {
S return denominator;
S }
S
S -(int) numerator {
S return numerator;
S }
@end
n
S #import <Foundation/>
S
S @interface Complex: NSObject {
S double real;
S double imaginary;
S }
S
S -(Complex*) initWithReal: (double) r andImaginary: (double) i;
S -(void) setReal: (double) r;
S -(void) setImaginary: (double) i;
S -(void) setReal: (double) r andImaginary: (double) i;
S -(double) real;
S -(double) imaginary;
S -(void) print;
S
@end
n
S #import ""
S #import <>
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,17/43
S
S @implementation Complex
S -(Complex*) initWithReal: (double) r andImaginary: (double) i {
S self = [super init];
S
S if ( self ) {
S [self setReal: r andImaginary: i];
S }
S
S return self;
S }
S
S -(void) setReal: (double) r {
S real = r;
S }
S
S -(void) setImaginary: (double) i {
S imaginary = i;
S }
S
S -(void) setReal: (double) r andImaginary: (double) i {
S real = r;
S imaginary = i;
S }
S
S -(double) real {
S return real;
S }
S
S -(double) imaginary {
S return imaginary;
S }
S
S -(void) print {
S printf( "%_f + %_fi", real, imaginary );
S }
S
@end
n
S #import <>
S #import ""
S #import ""
S
S int main( int argc, const char *argv[] ) {
S .
2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,18/43
S
S ntValue() 就得先转型,然后才能呼那个method。
o 继承(Inheritance)
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S #import <Foundation/>
S
S @interface Rectangle: NSObject {
S int width;
S int height;
S }
S
S -(Rectangle*) initWithWidth: (int) w height: (int) h;
S -(void) setWidth: (int) w;
S -(void) setHeight: (int) h;
S -(void) setWidth: (int) w height: (int) h;
S -(int) width;
S -(int) height;
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page ------------
Objective-C Beginner's Guide 页码,19/43
S -(void) print;
@end
n
S #import ""
S #import <>
S
S @implementation Rectangle
S -(Rectangle*) initWithWidth: (int) w height: (int) h {
S self = [super init];
S
S if ( self ) {
S [self setWidth: w height: h];
S }
S
S return self;
S }
S
S -(void) setWidth: (int) w {
S width = w;
S }
S
S -(void) setHeight: (int) h {
S height = h;
S }
S
S -(void) setWidth: (int) w height: (int) h {
S width = w;
S height = h;
S }
S
S -(int) width {
S return width;
S }
S
S -(int) height {
S return height;
S }
S
S -(void) print {
S printf( "width = %i, height = %i", width, height );
S }
@end
n
S #import ""
S
S @interface Square: Rectangle
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 20-----------------------
Objective-C Beginner's Guide 页码,20/43
S -(Square*) initWithSize: (int) s;
S -(void) setSize: (int) s;
S -(int) size;
@end
n
S #import ""
S
S @implementation Square
S -(Square*) initWithSize: (int) s {
S self = [super init];
S
S if ( self ) {
S [self setSize: s]; S }
S
S return self;
S }
S
S -(void) setSize: (int) s {
S width = s;
S height = s;
S }
S
S -(int) size {
S return width;
S }
S
S -(void) setWidth: (int) w { S [self setSize: w];
S }
S
S -(void) setHeight: (int) h {
S [self setSize: h];
S }
@end
n
S #import ""
S #import ""
S #import <>
S
S int main( int argc, const char *argv[] ) {
S
S Square *sq = [[Square alloc] initWithSize: 15];
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 2-----------
Objective-C Beginner's Guide
页码,21/43
S [rec print];
S printf( "\n" );
S
S printf( "Square: " );
S [sq print];
S printf( "\n" );
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 22-----------------------
Objective-C Beginner's Guide 页码,22/43
classObj
-(BOOL) isMemberOfClass: classObj 此对象是不是是
classObj 的一员
does the object have a
method named specifiec
-(BOOL) respondsToSelector: selector by the selector
此对象是不是有叫做
selector 的method
does an object created
by this class have the
ability to respond to the
+(BOOL) instancesRespondToSelector: selector specified selector
此对象是不是是由有能
力响应指定selector
的对象所产生
invoke the specified
selector on the object
-(id) performSelector: selector
唤起此对象的指定
selector
n 所有继承自NSObject 都有一个可回传一个class 物件的class method 。
这超级
近似于Java 的getClass() method 。
那个class 对象被利用于前述的methods
中。
n Selectors 在Objective-C 用以表示讯息。
下一个范例会秀出成立selector 的语
法。
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S #import ""
S #import ""
S #import <>
S
S int main( int argc, const char *argv[] ) {
S
S Square *sq = [[Square alloc] initWithSize: 15];
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 23-----------------------
Objective-C Beginner's Guide 页码,23/43
S if ( [sq isMemberOfClass: [NSObject class]] == YES ) {
S printf( "square is a member of object class\n" );
S }
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 24-----------------------
Objective-C Beginner's Guide 页码,24/43
S . 2020-2-6 PDF created with pdfFactory Pro trial version
----------------------- Page 25-----------------------
Objective-C Beginner's Guide 页码,25/43
S
S
S }
S
S -(Fraction*) div: (Fraction*) f { S
S
S }
S
S -(Fraction*) sub: (Fraction*) f { S
S
S
S }
@end
n
S #import <>
S #import ""
S #import ""
S
S int main( int argc, const char *argv[] ) {
S
and Settings\Administrator\桌面\Objective-C Begi... 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 26-----------------------
Objective-C Beginner's Guide 页码,26/43
n (同一个class)只能有一个同名的category,其它的categories 得加上不同
的、唯一无的名字。
n Categories 在成立private methods 时十分有效。
因为Objective-C 并无像Java
这种private/protected/public methods 的概念,因此必需要利用categories 来达
成这种功能。
作法是把private method 从你的class header (.h) 档案移到
implementation (.m) 档案。
以下是此种作法一个简短的范例。
n
S #import <Foundation/>
S
S @interface MyClass: NSObject
S -(void) publicMethod;
@end
n
S #import ""
S #import <>
S
S @implementation MyClass
S -(void) publicMethod {
S printf( "public method\n" );
S }
S @end
S
S . 2020-2-6 PDF created with pdfFactory Pro trial version
----------------------- Page 27-----------------------
Objective-C Beginner's Guide 页码,27/43
S [obj release];
S
S return 0;
}
n output
public method
o Posing
n Posing 有点像categories,可是不太一样。
它许诺你扩充一个class,而且全面
性地的扮演(pose)那个super class。
例如:你有一个扩充NSArray 的
NSArrayChild 物件。
若是你让NSArrayChild 扮演NSArray,那么在你的程序代
码中所有的NSArray 都会自动被替代为NSArrayChild 。
n 基于"Programming in Objective-C," Copyright 2004 by Sams Publishing一中
的范例,并通过许诺而刊载。
n
S #import ""
S
S @interface FractionB: Fraction
S -(void) print;
S @end
n
S #import ""
S #import <>
S
S @implementation FractionB
S -(void) print {
S printf( "(%i/%i)", numerator, denominator );
S }
@end
n
S #import <>
S #import ""
S #import ""
S
S int main( int argc, const char *argv[] ) {
S
S
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 28-----------------------
Objective-C Beginner's Guide 页码,28/43
S . 2020-2-6
PDF created with pdfFactory Pro trial version
----------------------- Page 29-----------------------
Objective-C Beginner's Guide 页码,29/43
S -(Fraction*) initWithNumerator: (int)。