没有合适的资源?快使用搜索试试~ 我知道了~
From C++ to Objective-C
需积分: 4 25 下载量 142 浏览量
2009-04-29
13:12:24
上传
评论
收藏 628KB PDF 举报
温馨提示
试读
55页
通过与C++的对比,结合代码来讲述Objective-C,初学者必备的Objective-C资料
资源推荐
资源详情
资源评论
From C
++
to Objective-C
version 1.11-en
Pierre Chatelier
e-mail: pierre.chatelier@club-internet.fr
Copyright
c
! 2005, 2006, 2007 Pierre Chatelier
English adaptation : Aaron Vegh
Document revisions available at :
http://ktd.club.fr/programmation/objective-c.php
This document is also available in french
Ce document est aussi disponible en français
With special thanks to: For their attentive reading and many helpful comments, I would like
to thank Pascal Bleuyard, Jérôme Cornet, François Delobel and Jean-Daniel Dupas, whose
help was important in making this work the best possible. Jack Nutting, Be n Rimmington and
Mattias Arrelid have also provided many feedback. Jonathon Mah has been particularly implied
in bringing a lot of very judicious corrections.
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 . . . . . . . . . . . . . . . . . 13
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 Virtual 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 . . . . . . . . . . . . . . . . . . . . . . 22
2
6.5 Class categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.6 Joint use of protocols, categories, subclassing: ..................... 23
7 Instantiation 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 NSCopyObject() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
7.3.3 Dummy-cloning, mutability, mutableCopy and mutableCopyWithZone: .. 31
8 Memory management 33
8.1 new and delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.2 Reference counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.3 alloc, copy, mutableCopy, retain, release . . . . . . . . . . . . . . . . . . . . . 33
8.4 autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.4.1 autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
8.4.2 The autorelease pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.4.3 Using several autorelease pools . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.4.4 Caution with autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.4.5 autorelease and retain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
8.4.6 Convenience constructor, virtual constructor ................. 36
8.4.7 Setter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8.4.8 Getter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.5 Retain cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
8.6 Garbage collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
9 Exceptions 42
10 Strings in Objective-C 44
10.1 The only static objects in Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . 44
10.2 NSString and encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
10.3 Description of an object, %@ format extension, NSString to C string ......... 44
11 STL and Cocoa 45
11.1 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
11.2 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
11.2.1 Classical enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
11.2.2 Fast enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
11.3 Functors (function objects) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
11.3.1 Using selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
11.3.2 IMP caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
11.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3
12 C
++
specific features 47
12.1 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.2 Inlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.4 Operators overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.5 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.6 const methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
12.7 List of initialization in the constructor . . . . . . . . . . . . . . . . . . . . . . . . . 47
13 RTTI (Run-Time Type Information) 48
13.1 class, superclass, isMemberOfClass, isKindOfClass ............... 48
13.2 conformsToProtocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13.3 respondsToSelector, instancesRespondToSelector . . . . . . . . . . . . . . . . 48
13.4 Strong typing or weak typing with id . . . . . . . . . . . . . . . . . . . . . . . . . 49
14 Objective-C
++
49
15 Objective-C 2.0 50
Conclusion 51
References 51
Document revisions 52
Index 53
4
Introduction
This document is designed to act as a bridge between C
++
and Objective-C. Many texts exist
to teach the object model through Objective-C, but to my knowledge, none target advanced C
++
developers wanting to compare these concepts with their own knowledge. At first, the Objective-C
language seems to be an obstacle rather than a boost for Cocoa programming (cf. section 1 on the
following page): it was so different that I couldn’t get into it. It took me some time to appreciate its
challenges, and understand the many helpful concepts it supplies. This document is not a tutorial,
but a quick reference, to these concepts. I hope that it will be useful in preventing a developer
from either abandoning Objective-C or misusing its features, 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, be sure to read a specialized Objective-C manual [1].
5
剩余54页未读,继续阅读
资源评论
lostime
- 粉丝: 5
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
- SpringBoot整合mongodb学习MongoTemplate和MongoRepository两种方式CRUD使用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功