`
janedoneway
  • 浏览: 580592 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

iPhone开发:浅析Objective-C的动态特性

 
阅读更多

From:http://www.2cto.com/kf/201110/107291.html

 

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 图片加载也是这样的

From:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html%23//apple_ref/doc/uid/TP40002974-CH4-SW32

 

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 the id 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....

}

The id 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 through object 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 to dynamic 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’s NSBundle 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 applications extensible. 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

分享到:
评论

相关推荐

    Mac编程入门:使用Objective-C和Cocoa开发

    - **动态性**:Objective-C的动态特性使得它能够在运行时向对象发送消息,从而实现灵活的功能扩展。 - **简洁性**:尽管功能强大,但Objective-C的语法相对简单,易于学习和使用。 #### Cocoa框架概述 Cocoa是苹果...

    iPhone开发:Objective-C语法入门.pdf

    ### iPhone开发:Objective-C语法入门 #### 一、Objective-C简介 Objective-C是一种面向对象的编程语言,它是在C语言的基础上扩展而成的。Objective-C主要应用于苹果公司的操作系统上,包括macOS、iOS、watchOS以及...

    iPhone开发:Objective-C语法入门

    Objective-C的语法在很多方面与C语言相似,但增加了类、消息传递等面向对象的特性。 1. **方法调用**:在Objective-C中,方法调用通过`[对象 方法]`的形式进行。例如,`[object method];`或`[object ...

    Objective-C 基础教程(Amazon超级畅销书)英文版:Learn Objective-C on the Mac (Learn Series)

    - **特点**:Objective-C保留了C语言的所有特性,并在此基础上增加了面向对象的特性,使得它成为一种强大的语言。 #### 二、Objective-C编程基础知识 - **变量与数据类型**:Objective-C支持多种数据类型,包括基本...

    苹果iphone开发系列:Objective-C初學者指南

    苹果iphone开发系列:Objective-C初學者指南 该种语言的最大特色是中括号【】

    windows 下搭建 Objective-C 开发环境

    接下来,您可以开始探索Objective-C的各种特性和功能,逐步深入学习和开发iOS应用程序了。希望这份指南能够帮助您顺利入门,祝您学习进步! 总结来说,在Windows系统下搭建Objective-C开发环境虽然不像在Mac OS X上...

    iPhone开发资料之Objective-C语言简介

    Objective-C是一种面向对象的编程语言,它是C语言的超集,特别为苹果的iOS和macOS平台的软件开发而设计。Objective-C通过引入Smalltalk的影响,支持类、方法和属性等面向对象特性,使得程序员能够构建复杂的软件系统...

    Objective-C 2.0 运行时系统编程指南

    1. **通过Objective-C源代码**:这是最常见的交互方式,当编写和编译Objective-C类和方法时,编译器会自动生成一些数据结构和函数,用于支持语言的动态特性。这些数据结构包含了类定义和协议定义中的信息,如方法...

    iPhone开发基础教程&Objective-C.2.0程序设计

    Objective-C.2.0是Apple公司开发的面向对象的编程语言,它是基于C语言的,同时也融合了Smalltalk的特性。Objective-C在苹果的iOS和macOS平台上被广泛用于开发原生应用程序。以下是一些Objective-C.2.0的关键知识点:...

    iPhone+SDK编程入门经典:使用Objective-C

    ### iPhone SDK编程入门:使用Objective-C详解 #### 获取与理解iPhone SDK 对于任何希望进入iPhone应用开发领域的开发者而言,掌握如何获取与使用iPhone SDK(Software Development Kit)是至关重要的第一步。SDK...

    Beginning iPhone SDK Programming with Objective-C.pdf

    本书《Beginning iPhone SDK Programming with Objective-C》是一本专为初学者设计的指南,旨在帮助读者快速掌握如何使用Objective-C语言进行iPhone应用程序开发。书中通过浅显易懂的语言和丰富的示例代码,为读者...

    Objective-c语言学习-快速入门完整版完整版

    本教程将通过一系列的PPT课件,引导你快速入门Objective-C,这些课件覆盖了从基础到高级的重要概念,是《iPhone与iPad开发实战》一书中关于语言学习的前半部分内容。 首先,我们从"第一讲 Objective-C概述"开始,这...

    iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics

    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-...

    iPhone开发人员的Objective-C代码规范

    Objective-C作为一种动态的面向对象语言,是C语言的一种扩展。它以其易读性和强大的面向对象设计能力著称,是MacOS X以及iPhone操作系统的主要开发语言。Cocoa作为MacOS X的核心应用程序框架,包含了大量用于快速...

    这是一个基于Objective-C语言的基础案例集 旨在用于给初学者快速了解Objective-C语言的语法 .zip

    8. Foundation框架:Objective-C的开发离不开Foundation框架,它提供了一系列基础数据类型、集合类(如NSArray、NSDictionary)、线程管理等工具。 9. iOS SDK:对于iOS开发,还需要了解UIKit框架,它包含了构建...

    [Objective-C编程(第6版)]Programming in Objective-C

    - **特点**:Objective-C支持类、消息传递、继承等面向对象编程特性,并与C语言兼容,允许开发者直接在Objective-C代码中使用C代码。 ##### 2. 基础语法 - **变量和数据类型**:Objective-C支持多种内置数据类型,...

    Objective-C编程全解第三版

    1. Objective-C简介:Objective-C是一种通用、面向对象的编程语言,主要用于iOS和OS X应用的开发。它是由Smalltalk语言的面向对象特性和C语言的灵活性相结合而成的,由Next公司开发,并由苹果公司在Mac OS X和iOS...

    深入浅出讲objective-c

    1. **语法特性**:Objective-C是在C语言的基础上扩展的,保留了C的语法特性,同时引入了消息传递机制,使得它具有面向对象的能力。 2. **类和对象**:Objective-C中的所有数据结构都是基于类的,类是对象的模板,而...

    Objective-C Runtime Programming Guide

    尽管了解Objective-C运行时系统的细节不是编写Cocoa应用程序所必需的,但掌握这些知识可以让开发者更好地理解Objective-C语言的底层机制,并在必要时利用这些强大的特性来构建更加复杂和灵活的应用程序。通过深入...

Global site tag (gtag.js) - Google Analytics