Objective-C有3个动态特性
1,动态类型
Dynamic typing—determining the class of an object at runtime
运行时决定对象类型
2,动态绑定
Dynamic binding—determining the method to invoke at runtime
运行时决定方法调用
3,动态加载
Dynamic loading—adding new modules to a program at runtime
运行时加载新模块
如何理解Objective-C的3这个动态特性呢?
首先,动态类型,简单点说就是 id 类型,可以理解为通用对象类型,一旦被赋值,可以被强制转化为其它类型。可以通过[obj isKindOfClass:aClass],来判断其具体类型,作相应操作。这一点在委托(delegate)中体现得比较充分
动态绑定是基于动态类型的,某个实例被确定后,其类型也是确定的,其对应的属性和方法将会因为类型的确定而确定,这就是动态绑定。
动态加载是程序启动时动态加载可执行代码和资源。例如:多国语言的程序,会在程序启动时只加载设定为某一种语言的资源,而不是全部加载。基于Utility Application的程序,分别在iPhone和iPad上运行的时候,只会加载对应的代码和资源。当然兼容视网膜技术的@2x 图片加载也是这样的
The Dynamism of Objective-C
Objective-C is a very dynamic language. Its dynamism frees a program from compile-time and link-time constraints and shifts much of the
responsibility for symbol resolution to runtime, when the user is in control. Objective-C is more dynamic than other programming languages because its dynamism springs from three sources:
-
Dynamic typing—determining the class of an object at runtime
-
Dynamic binding—determining the method to invoke at runtime
-
Dynamic loading—adding new modules to a program at runtime
For dynamic typing, Objective-C introduces theid
data
type, which can represent any Cocoa object. A typical use of this generic object type is shown in this part of the code example fromListing
2-2:
id word;
|
while (word = [enm nextObject]) {
|
// do something with 'word' variable....
|
}
|
Theid
data type makes it possible to substitute any type of object at runtime. You can thereby let runtime factors dictate what kind of object is to be used
in your code. Dynamic typing permits associations between objects to be determined at runtime rather than forcing them to be encoded in a static design.Static
type checking at compile time may ensure stricter data integrity, but in exchange for that stricter integrity, dynamic typing gives your program much greater flexibility. And throughobject
introspection (for example, asking a dynamically typed, anonymous object what its class is) you can still verify the type of an object at runtime and thus validate its suitability for a particular operation. (Of course, you can always statically check the
types of objects when you need to.)
Dynamic typing gives substance todynamic binding, the second kind of dynamism in Objective-C. Just as dynamic typing defers the resolution
of an object’s class membership until runtime, dynamic binding defers the decision of which method to invoke until runtime. Method invocations are not bound to code during compilation; they are bound only when a message is actually delivered. With both dynamic
typing and dynamic binding, you can obtain different results in your code each time you execute it. Runtime factors determine which receiver is chosen and which method is invoked.
The runtime’s message-dispatch machinery enables dynamic binding. When you send a message to a dynamically typed object, the runtime system uses the receiver’s isa pointer to locate the object’s class, and from there the method implementation to invoke. The
method is dynamically bound to the message. And you don’t have to do anything special in your Objective-C code to reap the benefits of dynamic binding. It happens routinely and transparently every time you send a message, especially one to a dynamically typed
object.
Dynamic loading, the final type of dynamism, is a feature of Cocoa that depends on Objective-C for runtime support. With dynamic loading,
a Cocoa program can load executable code and resources as they’re needed instead of having to load all program components at launch time. The executable code (which is linked prior to loading) often contains new classes that become integrated into the runtime
image of the program. Both code and localized resources (including nib files) are packaged in bundles and are explicitly loaded with methods defined in Foundation’sNSBundle
class.
This “lazy-loading” of program code and resources improves overall performance by placing lower memory demands on the system. Even more importantly, dynamic loading makes applicationsextensible.
You can devise a plug-in architecture for your application that allows you and other developers to customize it with additional modules that the application can dynamically load months or even years after the application is released. If the design is right,
the classes in these modules will not clash with the classes already in place because each class encapsulates its implementation and has its own namespace.
Dynamic typing—determining the class of an object at runtime
Dynamic binding—determining the method to invoke at runtime
Dynamic loading—adding new modules to a program at runtime
Dynamic typing—determining the class of an object at runtime
Dynamic binding—determining the method to invoke at runtime
Dynamic loading—adding new modules to a program at runtime
Dynamic typing—determining the class of an object at runtime
Dynamic binding—determining the method to invoke at runtime
Dynamic loading—adding new modules to a program at runtime
分享到:
相关推荐
iPhone+SDK编程入门经典:使用Objective-C
苹果iphone开发系列:Objective-C初學者指南 该种语言的最大特色是中括号【】
iPhone开发资料之Objective-C语言简介,网上收集的.
iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门 iPhone开发Objective-C语法入门 iphone 入门 iphone 开发入门
只要有可能,Objective-C总是使用动态的方式来解决问题。这意味着Objective-C语言不仅需要一个编译器,同时也需要一个运行时系统来执行编译好的代码。这儿的运行时系统扮演的角色类似于Objective-C语言的操作系统,...
中文介绍(china-pub):Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。. 本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和...
Objective-C是Mac软件开发领域最主要的开发语言。假如我们对面向对象的思维已经C语言都很熟悉的话,对于我们学习Objective-C将会非常有用。
iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics by Matt Neuburg (Author) Publisher: O’Reilly Media (October 2013) Language: English ISBN-10: 1491945575 ISBN-13: 978-...
人民邮电出版社《Objective-C基础教程》(含源代码) 影印版的PDF文件+原书附带的代码,特别适合学习iPhone,iPad入门开发学习~
读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/ipad和mac应用开发。《objective-c程序设计》共分成11章。前6章讲述objective-c语言,包括数据类型、运算符、表达式...
Objective-C是创建Mac OS应用和iPhone应用的主要语言,优雅的面向对象编程环境与C语言特性珠联璧合,造就了它的不俗表现。 本书全面而系统地讲述Objective-C语言的基础知识和面向对象编程的重要概念,结合实例...
在windows系统下搭建Objective-C开发环境,由于公司要做iphone开发,个人学习objc(Objective-C)语法,所以在windows7下搭建了一个objc开发环境,现把自己整理的帮助文档拿来与大家分享,希望能对大家有所帮助。...
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中...
Title: More iPhone Development with Objective-C, 3rd Edition Author: Alex Horovitz, David Mark, Jayant Varma, Jeff LaMarche, Kevin Kim Length: 512 pages Edition: 3 Language: English Publisher: Apress ...
More iPhone Development with Objective-C is an independent companion to Beginning iPhone Development with Objective-C. That is, it is a perfect second book, but it is also a great book for those ...
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程的相关内容,包括Objective-C在C的基础上引入的特性和Cocoa工具包的功能及其中...
Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition).epub (epub 格式) Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is ...
Google Objective-C Sytle Guide 中文 iPhone开发人员的代码规范,中文版本。