没有合适的资源?快使用搜索试试~ 我知道了~
From C++ to Objective-C version 1.9
需积分: 3 19 下载量 85 浏览量
2009-03-20
13:18:21
上传
评论
收藏 627KB PDF 举报
温馨提示
试读
53页
法国人写的C++到Objective-C教程。v1.9-English。 推荐下载另外一个链接的最新版v1.11。
资源推荐
资源详情
资源评论
From C
++
to Objective-C
version 1.9-en
Pierre Chatelier
e-mail: pierre.chatelier@club-internet.fr
Copyright
c
2005, 2006, 2007 Pierre Chatelier
This document is also available in french
Ce document est aussi disponible en français
With special thanks to: For their attentive reading and multiples remarks, I would like to
thank Pascal Bleuyard, Jérôme Cornet, François Delobel and Jean-Daniel Dupas, whom
help was very precious in order to achieve the best possible quality.
1
Contents
Table of contents 2
Introduction 5
1 Objective-C and Cocoa 6
2 A short history of Objective-C 6
3 Syntax overview 6
3.1 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Mixing up code and declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 New types and values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4.1 BOOL, YES, NO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4.2 nil, Nil and id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4.3 SEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Class names: why NS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.6 Differencing functions and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Organization of source code: .h and .m files, inclusion 8
5 Classes and objects 9
5.1 Root class, type id, nil and Nil values . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2 Class declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2.1 Attributes and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2.2 Forward declarations: @class, @protocol . . . . . . . . . . . . . . . . . . . 10
5.2.3 public, private, protected . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.2.4 static attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.3.1 Prototype and call, instance methods, class methods . . . . . . . . . . . . . 11
5.3.2 this, self and super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.3.3 Accessing instance variables inside a method . . . . . . . . . . . . . . . . . 12
5.3.4 Prototype id and signature, overloading . . . . . . . . . . . . . . . . . . . . 13
5.3.5 Pointer to member function: Selector . . . . . . . . . . . . . . . . . . . . . . 14
5.3.6 Default values of parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3.7 Variable number of arguments . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3.8 Anonymous arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3.9 Prototype modifiers (const, static, virtual, ‘‘= 0’’, friend, throw) . . 17
5.4 Messages and transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.4.1 Sending a message to nil . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.4.2 Delegating a message to an unknown object . . . . . . . . . . . . . . . . . . 17
5.4.3 Forwarding: handling an unknown message . . . . . . . . . . . . . . . . . . 17
5.4.4 Downcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 Inheritance 19
6.1 Simple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2 Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3 Virtuality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3.1 Virtual methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3.2 Silent redefinition of virtual methods . . . . . . . . . . . . . . . . . . . . . . 19
6.3.3 Virtuel inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.4 Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.4.1 Formal protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.4.2 Informal protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.4.3 Message qualifiers for distant objects . . . . . . . . . . . . . . . . . . . . . . 21
2
6.5 Class categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.6 Joint use of protocols, categories, subclassing: . . . . . . . . . . . . . . . . . . . . . 23
7 Instanciation 24
7.1 Constructors, initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.1.1 Distinction between allocation and initialization . . . . . . . . . . . . . . . 24
7.1.2 Using alloc and init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.1.3 Example of a correct initializer . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.1.4 Initialization failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.1.5 Default constructor : designated initializer . . . . . . . . . . . . . . . . . . . 26
7.1.6 List of initialization and default value of instance data . . . . . . . . . . . . 28
7.1.7 Virtual constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
7.1.8 Class constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
7.2 Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
7.3 Copy operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.3.1 Classical cloning, copy and copyWithZone: . . . . . . . . . . . . . . . . . . 29
7.3.2 Dummy-cloning, mutability, mutableCopy and mutableCopyWithZone: . . 29
8 Memory management 31
8.1 new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.2 Reference counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.3 alloc, copy, mutableCopy, retain, release . . . . . . . . . . . . . . . . . . . . . 31
8.4 autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.4.1 Precious autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.4.2 The autorelease pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.4.3 Using several autorelease pools . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.4.4 Caution with autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.4.5 autorelease and retain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
8.4.6 Convenience constructor, virtual constructor . . . . . . . . . . . . . . . . . 34
8.4.7 Setter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.4.8 Getter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8.5 Retain cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
8.6 Garbage collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
9 Exceptions 40
10 Strings in Objective-C 42
10.1 The only static objects in Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . 42
10.2 NSString and encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
10.3 Description of an object, %@ format extension, NSString to C string . . . . . . . . . 42
11 STL and Cocoa 43
11.1 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
11.2 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
11.2.1 Classical enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
11.2.2 Fast enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
11.3 Functors (function objects) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
11.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12 C
++
specific features 45
12.1 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.2 Inlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.4 Operators overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.5 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.6 const methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.7 List of initialization in the constructor . . . . . . . . . . . . . . . . . . . . . . . . . 45
3
13 RTTI (Run-Time Type Information) 46
13.1 class, superClass, isMemberOfClass, isKindOfClass . . . . . . . . . . . . . . . 46
13.2 conformsToProtocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
13.3 respondsToSelector, instancesRespondToSelector . . . . . . . . . . . . . . . . 46
13.4 Strong typing or weak typing with id . . . . . . . . . . . . . . . . . . . . . . . . . 47
14 Objective-C
++
47
15 Objective-C 2.0 47
Conclusion 49
References 49
Document revisions 50
Index 51
4
Introduction
This document is designed to act as a bridge between C
++
and Objective-C. Many documen-
tations exist to teach the model object through Objective-C, but none targets, as far as I know, to
advanced C
++
developers willing to learn more on the language concepts, in order to compare these
concepts with their actual knowledge. At first, the Objective-C language seemed to be an obstacle
to me rather than a boost for Cocoa programming (cf. section 1 on the following page): it was so
confidential that I did not realized any interest against a powerful, efficient and well-known C
++
.
It took me some time to understand that it was a real challenger, thanks to the many original
concepts it supplies. This document does not stand for a tutorial but a quick reference to these
concepts. I hope that it will be useful to prevent a developer from either evicting Objective-C or
misusing its usual tools, because of a misunderstanding of the language. This document does not
claim to be a full reference, but a quick one. For detailed explanations of a concept, it is better to
read a specialized Objective-C documentation [1].
5
剩余52页未读,继续阅读
资源评论
Lbebo
- 粉丝: 3
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功