您的位置:首页 > 移动开发 > Objective-C

[译]Objective-C Runtime Programming Guide - Interacting with the Runtime (一)

2014-11-20 16:39 351 查看
这篇翻译只是自己的学习过程的记录。觉得有误谢谢大家指正。


Interacting with the Runtime

Objective-C programs interact with the runtime system at three distinct levels: through Objective-C source code; through methods defined in the
NSObject
class
of the Foundation framework; and through direct calls to runtime functions.

Objective-C程序的运行时系统在三个不同的层面进行交互:通过Objective-C的源代码;通过NSObject类Foundation框架的确定方法;并通过直接调用运行时函数。




Objective-C Source Code

For the most part, the runtime system works automatically and behind the scenes. You use it just by writing and compiling Objective-C source code.

When you compile code containing Objective-C classes and methods, the compiler creates the data structures and function calls that implement the dynamic characteristics of the language. The data structures capture information found in class and category definitions
and in protocol declarations; they include the class and protocol objects discussed inDefining a Class and Protocols in The
Objective-C Programming Language, as well as method selectors, instance variable templates, and other information distilled from source code. The principal runtime function is the one that sends messages, as described in Messaging.
It’s invoked by source-code message expressions.
在大多数情况下,运行时系统自动地在幕后工作。您可以使用它只需要通过编写和编译Objective-C的源代码。


当你编译的代码包含Objective-C的类和方法时,编译器会创建数据结构和函数称作实现这个语言的动态特性。数据结构会在类和类别的定义和协议的声明中捕获信息。它们包括在定义一个类和协议的Objective-C的编程语言所讨论的类和对象的协议。以及选择的方法,实例变量的模板,然后从源代码中提炼出信息。主体运行时功能是发送消息,如消息所描述的。它是由源代码的消息表达式调用。


NSObject Methods

Most objects in Cocoa are subclasses of the
NSObject
class, so most objects inherit the methods it defines. (The notable exception is the
NSProxy
class;
see Message
Forwarding for more information.) Its methods therefore establish behaviors that are inherent to every instance and every class
object. However, in a few cases, the
NSObject
class merely defines a template for how something should be done; it doesn’t provide all the necessary
code itself.

For example, the
NSObject
class defines a
description
instance
method that returns a string describing the contents of the class. This is primarily used for debugging—the GDB
print-object
command prints the
string returned from this method.
NSObject
’s implementation of this method doesn’t know what the class contains, so it returns a string with the
name and address of the object. Subclasses of
NSObject
can implement this method to return more details. For example, the Foundation class
NSArray
returns
a list of descriptions of the objects it contains.

Some of the
NSObject
methods simply query the runtime system for information. These methods allow objects to perform introspection. Examples of
such methods are the
class
method,
which asks an object to identify its class;
isKindOfClass:
and
isMemberOfClass:
,
which test an object’s position in the inheritance hierarchy;
respondsToSelector:
,
which indicates whether an object can accept a particular message;
conformsToProtocol:
,
which indicates whether an object claims to implement the methods defined in a specific protocol; and
methodForSelector:
,
which provides the address of a method’s implementation. Methods like these give an object the ability to introspect about itself.

Cocoa的大部分对象都是NSObject类的子类,所以大多数对象继承它定义的方法。
(值得注意的例外是NSProxy类; 看Message Forwarding获取更多信息。)因此,它的方法建立行为所固有的每个实例,每一个类对象。然而,在少数情况下,NSObject类只定义了如何事情应该做的模板;它不提供所有必要的代码本身。


例如,NSObject类定义一个实例方法它会返回描述这个类的字符串。这个主要用于调试,GDB会用这个方法打印这个字符串。 NSObject的实现该方法的并不知道是包含什么类,所以它返回一个字符串的对象的名称和地址。 NSObject的子类可以实现此方法并返回详细信息。例如,Foundation类的NSArray返回它所包含的对象的描述的列表。

一些NSObject的方法简单地查询运行时系统的信息。这些方法允许对象进行自省(反省)。这类方法的实例是类方法,它要求一个对象表明自己的类; isKindOfClass:和isMemberOfClass:,它测试在继承层次结构中的对象的位置; respondsToSelector:,其指示对象是否可以接受的特定信息; conformsToProtocol:,这表明一个对象是否声称实现特定的协议中定义的方法;和methodForSelector:,它提供了一个方法的实现的地址。这样的方法给一个对象内省有关自身的能力。


Runtime Functions

The runtime system is a dynamic shared library with a public interface consisting of a set of functions and data structures in the header files located within the directory
/usr/include/objc
.
Many of these functions allow you to use plain C to replicate what the compiler does when you write Objective-C code. Others form the basis for functionality exported through the methods of the
NSObject
class.
These functions make it possible to develop other interfaces to the runtime system and produce tools that augment the development environment; they’re not needed when programming in Objective-C. However, a few of the runtime functions might on occasion be
useful when writing an Objective-C program. All of these functions are documented in Objective-C
Runtime Reference.
运行时函数

运行时系统是一个公共接口,由位于目录/ usr / include目录/ objc中的头文件的一组函数和数据结构的动态共享库。许多这些功能允许您使用纯C复制,当你写Objective-C代码编译器做什么。其他形式的基础功能,通过NSObject类的方法导出。这些功能可以开发其他接口的运行系统,并产生增强的开发环境工具;在Objective-C编程时,他们并不需要。然而,编写一个Objective-C程序时数的运行时函数可能有时是有用的。所有这些功能都记录在Objective-C运行时参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: