- 浏览: 13655309 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
前言
这篇教学假设你已经有一些基本的 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 中,唿叫物件上的一个讯息并不一定表示物件真的会实作这个讯息,而是物件知道如何以某种方式去实作它,或是转送给知道如何实作的物件。
编译 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
@interface
基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing 一书中的范例,并经过允许而刊载。
Fraction.h
#import @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; @end
1. NSObject:NeXTStep Object 的缩写。因为它已经改名为 OpenStep,所以这在今天已经不是那么有意义了。
2. 继承(inheritance)以 Class: Parent 表示,就像上面的 Fraction: NSObject。
3. 夹在 @interface Class: Parent { .... } 中的称为 instance variables。
4. 没有设定存取权限(protected, public, private)时,预设的存取权限为 protected。设定权限的方式将在稍后说明。
5. Instance methods 跟在成员变数(即 instance variables)后。格式为:
scope (returnType) methodName: (parameter1Type) parameter1Name;
scope 有class 或 instance 两种。instance methods 以 - 开头,class level methods 以 + 开头。
6. Interface 以一个 @end 作为结束。
@implementation
基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing 一书中的范例,并经过允许而刊载。
Fraction.m
#import "Fraction.h" #import @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
1. Implementation 以 @implementation ClassName 开始,以 @end 结束。
2. Implement 定义好的 methods 的方式,跟在 interface 中宣告时很近似。
把它们凑在一起
基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing 一书中的范例,并经过允许而刊载。
main.m
#import #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];
这行程式码中有很多重要的东西。
1. 在 Objective-C 中呼叫 methods 的方法是 [object method],就像 C++ 的 object->method()。
2. Objective-C 没有 value 型别。所以没有像 C++ 的 Fraction frac; frac.print(); 这类的东西。在 Objective-C 中完全使用指标来处理物件。
3. 这行程式码实际上做了两件事: [Fraction alloc] 呼叫了 Fraction class 的 alloc method。这就像 malloc 记忆体,这个动作也做了一样的事情。
4. [object init] 是一个建构子(constructor)呼叫,负责初始化物件中的所有变数。它呼叫了 [Fraction alloc] 传回的 instance 上的 init method。这个动作非常普遍,所以通常以一行程式完成:Object *var = [[Object alloc] init];
6. [frac setNumerator: 1] 非常简单。它呼叫了 frac 上的 setNumerator method 并传入 1 为参数。
7. 如同每个 C 的变体,Objective-C 也有一个用以释放记忆体的方式: release。它继承自 NSObject,这个 method 在之后会有详尽的解说。
详细说明...
多重参数
目前为止我还没展示如何传递多个参数。这个语法乍看之下不是很直觉,不过它却是来自一个十分受欢迎的 Smalltalk 版本。
基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
Fraction.h
... -(void) setNumerator: (int) n andDenominator: (int) d; ...
Fraction.m
... -(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d; } ...
main.m
#import #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; Fraction *frac2 = [[Fraction alloc] init]; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // combined set [frac2 setNumerator: 1 andDenominator: 5]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // print it printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); // free memory [frac release]; [frac2 release]; return 0; }
output
The fraction is: 1/3
Fraction 2 is: 1/5
1. 这个 method 实际上叫做 setNumerator:andDenominator:
2. 加入其他参数的方法就跟加入第二个时一样,即 method:label1:label2:label3: ,而呼叫的方法是 [obj method: param1 label1: param2 label2: param3 label3: param4]
3. Labels 是非必要的,所以可以有一个像这样的 method:method:::,简单的省略 label 名称,但以 : 区隔参数。并不建议这样使用。
建构子(Constructors)
基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
Fraction.h
... -(Fraction*) initWithNumerator: (int) n denominator: (int) d; ...
Fraction.m
... -(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; if ( self ) { [self setNumerator: n andDenominator: d]; } return self; } ...
main.m
#import #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; Fraction *frac2 = [[Fraction alloc] init]; Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // combined set [frac2 setNumerator: 1 andDenominator: 5]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); printf("Fraction 2 is: " ); [frac2 print]; printf( "\n" ); printf( "Fraction 3 is: " ); [frac3 print]; printf( "\n" ); // free memory [frac release]; [frac2 release]; [frac3 release]; return 0; }
output
The fraction is: 1/3
Fraction 2 is: 1/5
Fraction 3 is: 3/10
1. @interface 里的宣告就如同正常的函式。
2. @implementation 使用了一个新的关键字:super
3. 如同 Java,Objective-C 只有一个 parent class(父类别)。
4. 使用 [super init] 来存取 Super constructor,这个动作需要适当的继承设计。
5. 你将这个动作回传的 instance 指派给另一新个关键字:self。Self 很像 C++ 与 Java 的 this 指标。
6. if ( self ) 跟 ( self != nil ) 一样,是为了确定 super constructor 成功传回了一个新物件。nil 是 Objective-C 用来表达 C/C++ 中 NULL 的方式,可以引入 NSObject 来取得。
7. 当你初始化变数以后,你用传回 self 的方式来传回自己的位址。
8. 预设的建构子是 -(id) init。
9. 技术上来说,Objective-C 中的建构子就是一个 "init" method,而不像 C++ 与 Java 有特殊的结构。
存取权限
预设的权限是 @protected
Java 实作的方式是在 methods 与变数前面加上 public/private/protected 修饰语,而 Objective-C 的作法则更像 C++ 对于 instance variable(译注:C++ 术语一般称为 data members)的方式。
Access.h
#import
@interface Access: NSObject {
@public
int publicVar;
@private
int privateVar;
int privateVar2;
@protected
int protectedVar;
}
@end
Access.m
#import \"Access.h\"
@implementation Access
@end
main.m
#import \"Access.h\"
#import
int main( int argc, const char *argv[] ) {
Access *a = [[Access alloc] init];
// works
a->publicVar = 5;
printf( \"public var: %i\\n\", a->publicVar );
// doesn\'t compile
//a->privateVar = 10;
//printf( \"private var: %i\\n\", a->privateVar );
[a release];
return 0;
}
output
public var: 5
如同你所看到的,就像 C++ 中 private: [list of vars] public: [list of vars] 的格式,它只是改成了@private, @protected, 等等。
Class level access
当你想计算一个物件被 instance 几次时,通常有 class level variables 以及 class level functions 是件方便的事。
ClassA.h
#import
static int count;
@interface ClassA: NSObject
+(int) initCount;
+(void) initialize;
@end
ClassA.m
#import \"ClassA.h\"
@implementation ClassA
-(id) init {
self = [super init];
count++;
return self;
}
+(int) initCount {
return count;
}
+(void) initialize {
count = 0;
}
@end
main.m
#import \"ClassA.h\"
#import
int main( int argc, const char *argv[] ) {
ClassA *c1 = [[ClassA alloc] init];
ClassA *c2 = [[ClassA alloc] init];
// print count
printf( \"ClassA count: %i\\n\", [ClassA initCount] );
ClassA *c3 = [[ClassA alloc] init];
// print count again
printf( \"ClassA count: %i\\n\", [ClassA initCount] );
[c1 release];
[c2 release];
[c3 release];
return 0;
}
output
ClassA count: 2
ClassA count: 3
static int count = 0; 这是 class variable 宣告的方式。其实这种变数摆在这里并不理想,比较好的解法是像 Java 实作 static class variables 的方法。然而,它确实能用。
+(int) initCount; 这是回传 count 值的实际 method。请注意这细微的差别!这里在 type 前面不用减号 - 而改用加号 +。加号 + 表示这是一个 class level function。(译注:许多文件中,class level functions 被称为 class functions 或 class method)
存取这个变数跟存取一般成员变数没有两样,就像 ClassA 中的 count++ 用法。
+(void) initialize method is 在 Objective-C 开始执行你的程式时被呼叫,而且它也被每个 class 呼叫。这是初始化像我们的 count 这类 class level variables 的好地方。
异常情况(Exceptions)
注意:异常处理只有 Mac OS X 10.3 以上才支援。
基于 \"Programming in Objective-C,\" Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。
CupWarningException.h
#import
@interface CupWarningException: NSException
@end
CupWarningException.m
#import \"CupWarningException.h\"
@implementation CupWarningException
@end
CupOverflowException.h
#import
@interface CupOverflowException: NSException
@end
CupOverflowException.m
#import \"CupOverflowException.h\"
@implementation CupOverflowException
@end
Cup.h
#import
@interface Cup: NSObject {
int level;
}
-(int) level;
-(void) setLevel: (int) l;
-(void) fill;
-(void) empty;
-(void) print;
@end
Cup.m
#import \"Cup.h\"
#import \"CupOverflowException.h\"
#import \"CupWarningException.h\"
#import
#import
@implementation Cup
-(id) init {
self = [super init];
if ( self ) {
[self setLevel: 0];
}
return self;
}
-(int) level {
return level;
}
-(void) setLevel: (int) l {
level = l;
if ( level > 100 ) {
// throw overflow
NSException *e = [CupOverflowException
exceptionWithName: @\"CupOverflowException\"
reason: @\"The level is above 100\"
userInfo: nil];
@throw e;
} else if ( level >= 50 ) {
// throw warning
NSException *e = [CupWarningException
exceptionWithName: @\"CupWarningException\"
reason: @\"The level is above or at 50\"
userInfo: nil];
@throw e;
} else if ( level < 0 ) {
// throw exception
NSException *e = [NSException
exceptionWithName: @\"CupUnderflowException\"
reason: @\"The level is below 0\"
userInfo: nil];
@throw e;
}
}
-(void) fill {
[self setLevel: level + 10];
}
-(void) empty {
[self setLevel: level - 10];
}
-(void) print {
printf( \"Cup level is: %i\\n\", level );
}
@end
main.m
#import \"Cup.h\"
#import \"CupOverflowException.h\"
#import \"CupWarningException.h\"
#import
#import
#import
#import
int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Cup *cup = [[Cup alloc] init];
int i;
// this will work
for ( i = 0; i < 4; i++ ) {
[cup fill];
[cup print];
}
// this will throw exceptions
for ( i = 0; i < 7; i++ ) {
@try {
[cup fill];
} @catch ( CupWarningException *e ) {
printf( \"%s: \", [[e name] cString] );
} @catch ( CupOverflowException *e ) {
printf( \"%s: \", [[e name] cString] );
} @finally {
[cup print];
}
}
// throw a generic exception
@try {
[cup setLevel: -1];
} @catch ( NSException *e ) {
printf( \"%s: %s\\n\", [[e name] cString], [[e reason] cString] );
}
// free memory
[cup release];
[pool release];
}
output
Cup level is: 10
Cup level is: 20
Cup level is: 30
Cup level is: 40
CupWarningException: Cup level is: 50
CupWarningException: Cup level is: 60
CupWarningException: Cup level is: 70
CupWarningException: Cup level is: 80
CupWarningException: Cup level is: 90
CupWarningException: Cup level is: 100
CupOverflowException: Cup level is: 110
CupUnderflowException: The level is below 0
NSAutoreleasePool 是一个记忆体管理类别。现在先别管它是干嘛的。
Exceptions(异常情况)的丢出不需要扩充(extend)NSException 物件,你可简单的用 id 来代表它: @catch ( id e ) { ... }
还有一个 finally 区块,它的行为就像 Java 的异常处理方式,finally 区块的内容保证会被呼叫。
Cup.m 里的 @\"CupOverflowException\" 是一个 NSString 常数物件。在 Objective-C 中,@ 符号通常用来代表这是语言的衍生部分。C 语言形式的字串(C string)就像 C/C++ 一样是 \"String constant\" 的形式,型别为 char *。
发表评论
-
手机firebug查看网页代码 How to View Website Source Codes on iPad / iPhone
2016-04-26 02:41 1132You’re using your iPad to br ... -
Android模拟器genymotion的安装和使用
2016-03-18 01:16 2486Genymotion概述 Genymotion是一套完整的 ... -
编码之道:取个好名字很重要
2015-10-14 05:19 1135代码就是程序员的孩子,给“孩子”取个好听的名字很重 ... -
Genymotion - 强大好用高性能的 Android 模拟器 (在电脑流畅运行APK安卓软件游戏的利器)
2015-06-05 13:34 7343随着 Android 系统的应用和游戏越来越丰富,甚至有 ... -
收集几个移动平台浏览器的User-Agent
2014-07-23 02:33 2990List of all Mobile Browsers ... -
常用浏览器修改User-Agent的方法
2014-06-28 02:58 10926之前有介绍收集的几个移动平台浏览器的User-Agent,以 ... -
更简洁的方式修改Chrome的User Agent,轻松体验移动版网络
2014-06-28 02:53 6889国庆节在家宅着,使用3g上网卡上网,和在公司上网不一样的是 ... -
3个检测浏览器User-Agent信息的网站
2014-06-28 02:52 2515刚刚在收集各个移动平台浏览器的User-Agent(参见这 ... -
根据移动设备屏幕像素密度,给予不同分辨率的图片
2014-06-26 00:52 2916在出现iphone4之前的相当长的时间内,网站开发人员 ... -
视网膜New iPad与普通分辨率iPad页面的兼容处理
2014-06-26 00:44 1287一、这是篇经验分享 ... -
iOS8开发-Swift编程
2014-06-25 00:45 1844课时列表 章节1:第一个Swift应用 ... -
Swift中文教程(二十三) 高级运算符
2014-06-25 00:43 793除了基本操作符中所讲的运算符,Swift还有许多复杂的高级 ... -
Swift中文教程(二十二) 泛型
2014-06-25 00:35 1022泛型代码可以让你写出根据自我需求定义、适用于任何类型的,灵 ... -
Swift中文教程(二十一) 协议
2014-06-24 05:25 1203Protocol(协议)用于统一方法和属性的名称,而不实现任 ... -
Swift中文教程(二十) 扩展
2014-06-24 00:47 699扩展就是向一个已有的类、结构体或枚举类型添加新功能(fun ... -
Swift中文教程(十九) 类型嵌套
2014-06-24 00:44 897枚举类型常被用于实现特定类或结构体的功能。也能够在有多种变量 ... -
Swift中文教程(十八) 类型检查
2014-06-24 00:41 743类型检查是一种检查类实例的方式,并且或者也是让实例作为它的 ... -
Swift中文教程(十七) 可选链
2014-06-17 05:11 877可选链(Optional Chaining)是一种可以请求 ... -
Swift中文教程(十六) 自动引用计数
2014-06-17 05:05 1258Swift使用自动引用计数(ARC)来管理应用程序的内存使 ... -
Swift中文教程(十五) 析构
2014-06-17 04:57 1006在一个类的实例被释放之前,析构函数会被调用。用关键字dei ...
相关推荐
Objective-C基础教程:1天玩转Objective-C语法
启动Ultraedit,依次打开:高级(A) --> 配置(C) --> 编辑器显示 --> 语法着色。找到文档的完整目录名称所指示的目录, 然后把objective-c.uew文件放在对应的目录下即可。
Objective-C语法总结(第一版)
XCode、Objective-C、Cocoa
iOS开发之Objective-C语法快速参考(PDF文档).
Objective-C 基础语法 Objective-C 基础语法
很好的语法讲座,叫你快速入门,如何了解objc的语法知识!
iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门 iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门
语法基础——Objective-C语法基础.pdf
Objective-C语法快速参考( 很有用的文档 )
Objective-C语法之异常处理
通过学习Objective-C的基本语法、类和对象、控制流程和方法等内容,你将能够编写简单的Objective-C程序并逐渐掌握更复杂的概念。继续学习和实践,你将能够开发各种类型的应用程序,并为用户提供出色的体验。祝你在...
Objective-C language development
Objective-C 是一种通用、面向对象的编程语言,广泛应用于 macOS 和 iOS 等苹果平台...本教程提供了 Objective-C 的基本语法和一些示例,希望能够帮助你快速入门 Objective-C 编程,并开始构建 macOS 和 iOS 应用程序。
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
使UltraEdit支持Objective-C语法高亮
教程名称:苹果Objective-C语法学习集萃 课程目录: 【】Beginning.iPhone.SDK.Programming.with.Objective-C 【】iphone开发比较实用,object-c注意事项,能提高不少 【】Object-c...
Objective-C语法之走进iphone的开发世界那些事儿 简单的ios开发界面,欢迎大家下载阅读。