iOS 开发 深入浅出Runtime运行时之官方指南翻译--与运行时交互

    xiaoxiao2021-12-14  20

    与运行时交互

    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源代码;通过Foundation框架中NSObject类定义的方法;通过直接调用运行时函数。

    1. objective - c源代码

    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.

    在大多数情况下,运行时系统自动和幕后工作。你使用它只是通过编写和编译objective - c源代码。

    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 in Defining 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(Messaging连接). It’s invoked by source-code message expressions.

    当你编译代码包含objective - c的类和方法,编译器创建数据结构和函数调用,实现语言的动态特性。数据结构中捕捉到的信息可以再类和分类定义和协议声明中找到,其中包括objective - c中定义的类和协议讨论的对象,以及选择器方法,实例变量属性、从源代码提取的其他信息。运行时函数的核心是发送消息(Messaging连接). 。包括调用源代码的消息表达式。

    2. NSObject方法

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

    在cocoa中大多数对象都是NSObject类的子类,所以大多数对象继承它定义的方法。(值得注意的例外是NSProxy类;有关更多信息,请参见消息转发(Message Forwarding连接) )。因此NSObject的方法每一个实例和类对象都可以继承。然而,在少数情况下,NSObject类只定义了一个模板应该怎么做,它不提供所有必要的代码本身。

    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.

    例如,NSObject类定义了一个描述实例方法返回一个字符串描述类的内容。这主要是用于调试GDB打印对象命令打印该方法返回的字符串。NSObject的实现该方法的不知道类包含的信息,所以它返回一个字符串对象的名称和地址。NSObject的子类可以实现此方法返回更多的细节。例如,基础类NSArray返回一个列表,它包含的对象的描述。

    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.

    NSObject一些简单的方法可以在运行时系统查询这些方法的信息。这些方法允许对象进行自检。

    有些方法是类方法,通过对象来确定它的类;isKindOfClass:和isMemberOfClass:测试对象在继承层次结构的位置;respondstoselecor: 检测一个对象是否实现了指定的消息conformsToProtocol:检测对象是否实现了指定协议类的方法;methodForSelector:返回指定方法返回的地址。performSelector: withObject 执行SEL所指代的方法 这些方法给对象的自我检测能力。

    3. 运行时函数

    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(Objective-C Runtime Reference连接).

    运行时系统是一个包含由一系列函数和数据结构组成公共接口的动态共享库,在头文件位于/usr/include/objc.的目录 当你编写objective - C代码的时候,这些函数允许您使用纯C代码去复制,并在编译时执行这些函数。 其他形式的基础函数可以通过NSObject类的方法导出。 这些函数可以用于在运行时系统和生产工具中开发其他接口,丰富开发环境;他们不需要使用objective - c编程。 然而,一些运行时函数有时可能需要编写一个objective - c程序时才有用。 所有这些函数都是记录在objective - c运行时参考。(Objective-C Runtime Reference连接).

    转载请注明原文地址: https://ju.6miu.com/read-971191.html

    最新回复(0)