Translations: English | Chinese
大纲
开始吧
o 下载这篇教学
o 设定环境
o 前言
o 编译 hello world
创建 Classes
o @interface
o @implementation
o 把它们凑在一起
详细说明...
o 多重参数
o 建构子(Constructors)
o 访问权限
o Class level access
o 异常情况(Exceptions)处理
继承、多型(Inheritance, Polymorphism)以及其他面向对象功能
o id 型别
o 继承(Inheritance)
o 动态识别(Dynamic types)
o Categories
o Posing
o Protocols
内存管理
o Retain and Release(保留与释放)
o Dealloc
o Autorelease Pool
Foundation Framework Classes
o NSArray
o NSDictionary
优点与缺点
更多信息
开始吧
o 下载这篇教学
所有这篇初学者指南的原始码都可以由 objc.tar.gz 下载。
这篇教学中的许多范例都是由 Steve Kochan 在
Programming in Objective-C. 一书中撰写。如果你想得到
更多详细信息及范例,请直接参考该书。这个网站上登载的
所有范例皆经过他的允许,所以请勿复制转载。
o 设定环境
Linux/FreeBSD: 安装 GNUStep
为了编译 GNUstep 应用程序,必须先执行位于
/usr/GNUstep/System/Makefiles/GNUstep.sh 的
GNUstep.sh 这个档案。这个路径取决于你的系统环
境,有些是在 /usr, some /usr/lib,有些是
/usr/local。如果你的 shell 是以 csh/tcsh 为基
础的 shell,则应该改用 GNUStep.csh。建议把这个
指令放在 .bashrc 或 .cshrc 中。
Mac OS X: 安装 XCode
Windows NT 5.X: 安装 cygwin 或 mingw,然后安装
GNUStep
o 前言
这篇教学假设你已经有一些基本的 C 语言知识,包括 C 数
据型别、什么是函式、什么是回传值、关于指针的知识以及
基本的 C 语言内存管理。如果您没有这些背景知识,我非
常建议你读一读 K&R 的书:The C Programming Language
(译注:台湾出版书名为 C 程序语言第二版)这是 C 语言
的设计者所写的书。
Objective-C,是 C 的衍生语言,继承了所有 C 语言的特
性。是有一些例外,但是它们不是继承于 C 的语言特性本
身。
nil:在 C/C++ 你或许曾使用过 NULL,而在 Objective-C
中则是 nil。不同之处是你可以传递讯息给 nil(例如 [nil
message];),这是完全合法的,然而你却不能对 NULL 如
法炮制。
BOOL:C 没有正式的布尔型别,而在 Objective-C 中也不
是「真的」有。它是包含在 Foundation classes(基本类
别库)中(即 import NSObject.h;nil 也是包括在这个头
文件内)。BOOL 在 Objective-C 中有两种型态:YES 或 NO,
而不是 TRUE 或 FALSE。
#import vs #include:就如同你在 hello world 范例中看
到的,我们使用了 #import。#import 由 gcc 编译程序支
援。我并不建议使用 #include,#import 基本上跟 .h 档
头尾的 #ifndef #define #endif 相同。许多程序员们都同
意,使用这些东西这是十分愚蠢的。无论如何,使用 #import
就对了。这样不但可以避免麻烦,而且万一有一天 gcc 把
它拿掉了,将会有足够的 Objective-C 程序员可以坚持保
留它或是将它放回来。偷偷告诉你,Apple 在它们官方的程
序代码中也使用了 #import。所以万一有一天这种事真的发
生,不难预料 Apple 将会提供一个支持 #import 的 gcc
分支版本。
在 Objective-C 中, method 及 message 这两个字是可以
互换的。不过 messages 拥有特别的特性,一个 message 可
以动态的转送给另一个对象。在 Objective-C 中,呼叫对
象上的一个讯息并不一定表示对象真的会实作这个讯息,而
是对象知道如何以某种方式去实作它,或是转送给知道如何
实作的对象。
o 编译 hello world
hello.m
#import <stdio.h>
int main( int argc, const char *argv[] ) {
printf( "hello world\n" );
return 0;
}
输出
hello world
在 Objective-C 中使用 #import 代替 #include
Objective-C 的预设扩展名是 .m
创建 classes
o @interface
基于 "Programming in Objective-C," Copyright © 2004 by
Sams Publishing 一书中的范例,并经过允许而刊载。
Fraction.h
#import <Foundation/NSObject.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
NSObject:NeXTStep Object 的缩写。因为它已经改名为
OpenStep,所以这在今天已经不是那么有意义了。
继承(inheritance)以 Class: Parent 表示,就像上面的
Fraction: NSObject。
夹在 @interface Class: Parent { .... } 中的称为
instance variables。
没有设定访问权限(protected, public, private)时,预
设的访问权限为 protected。设定权限的方式将在稍后说明。
Instance methods 跟在成员变数(即 instance variables)
后。格式为:scope (returnType) methodName:
(parameter1Type) parameter1Name;
scope 有 class 或 instance 两种。instance
methods 以 - 开头,class level methods 以 + 开
头。
Interface 以一个 @end 作为结束。
o @implementation
基于 "Programming in Objective-C," Copyright © 2004 by
Sams Publishing 一书中的范例,并经过允许而刊载。
Fraction.m
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end
Implementation 以 @implementation ClassName 开始,以
@end 结束。
Implement 定义好的 methods 的方式,跟在 interface 中
宣告时很近似。
o 把它们凑在一起
基于 "Programming in Objective-C," Copyright © 2004 by
Sams Publishing 一书中的范例,并经过允许而刊载。
main.m
#import <stdio.h>
#import "Fraction.h"
int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] init];
// set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
printf( "The fraction is: " );
[frac print];
printf( "\n" );
// free memory
[frac release];
return 0;
}
output
The fraction is: 1/3
Fraction *frac = [[Fraction alloc] init];
这行程序代码中有很多重要的东西。
在 Objective-C 中呼叫 methods 的方法是
[object method],就像 C++ 的 object->method()。
Objective-C 没有 value 型别。所以没有像 C++ 的
Fraction frac; frac.print(); 这类的东西。在
Objective-C 中完全使用指针来处理对象。
这行程序代码实际上做了两件事: [Fraction alloc]
呼叫了 Fraction class 的 alloc method。这就像
malloc 内存,这个动作也做了一样的事情。