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

[译]Objective-C Runtime Programming Guide -Dynamic Method Resolution ( 三)

2014-11-24 10:36 585 查看


Dynamic Method Resolution

This chapter describes how you can provide an implementation of a method dynamically.
动态方法解析


Dynamic Method Resolution

There are situations where you might want to provide an implementation of a method dynamically. For example, the Objective-C declared properties feature (see Declared
Propertiesin The Objective-C Programming Language) includes the
@dynamic
directive:

@dynamic propertyName;

which tells the compiler that the methods associated with the property will be provided dynamically.

You can implement the methods
resolveInstanceMethod:
and
resolveClassMethod:
to
dynamically provide an implementation for a given selector for an instance and class method respectively.
有时候你可能想要提供实现一个动态方法。举例来说,Objective-C声明属性功能(参见声明属性的Objective-C编程语言)包括 @dynamic 指令:

@dynamic propertyName;
这个指令告诉编译器 属性相关联的方法将被动态的提供。 你可以实现方法 resolveInstanceMethod 和 resolveClassMethod 分别以动态提供给指定了实例与方法的选择器去实现。

An Objective-C method is simply a C function that take at least two arguments—
self
and
_cmd
.
You can add a function to a class as a method using the function
class_addMethod
.
Therefore, given the following function:

void dynamicMethodIMP(id self, SEL _cmd) {

// implementation ....

}

一个Objective-C 方法是 提供至少两个参数 self和_cmd的 简单C函数。 你可以添加一个函数到一个类 就像一个方法使用函数 class_addMethod 因此。给出以下功能示范:

void dynamicMethodIMP(id self, SEL _cmd) {

// implementation ....

}

you can dynamically add it to a class as a method (called
resolveThisMethodDynamically
) using
resolveInstanceMethod:
like
this:

@implementation MyClass

+ (BOOL)resolveInstanceMethod:(SEL)aSEL

{

if (aSEL == @selector(resolveThisMethodDynamically)) {

class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");

return YES;

}

return [super resolveInstanceMethod:aSEL];

}

@end

Forwarding methods (as described in Message
Forwarding) and dynamic method resolution are, largely, orthogonal. A class has the opportunity to dynamically resolve a method before the forwarding mechanism kicks in. If
respondsToSelector:
or
instancesRespondToSelector:
is
invoked, the dynamic method resolver is given the opportunity to provide an
IMP
for the selector first. If you implement
resolveInstanceMethod:
but
want particular selectors to actually be forwarded via the forwarding mechanism, you return
NO
for
those selectors.

你可以动态的添加到一个类 作为一个方法 (叫做 resolveThisMethodDynamically)使用 resolveInstanceMethod 像这样:

@implementation MyClass

+ (BOOL)resolveInstanceMethod:(SEL)aSEL

{

if (aSEL == @selector(resolveThisMethodDynamically)) {

class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");

return YES;

}

return [super resolveInstanceMethod:aSEL];

}

@end

转发方法(如消息转发机智)和动态解析的方法是 主要的,相互垂直的?一个类有机会去动态的解决一个方法在它转发机制开始生效之前。如果respondsToSelector:或者 instancesRespondToSelector: 被调用,这个动态方法解析有机会去首先提供一个IMP机制给选择器。如果你实现 resoveInstanceMethod: 但是想要特别的选择器去通过转发机制转发,那么你就对这些选择器返回NO。


Dynamic Loading

An Objective-C program can load and link new classes and categories while
it’s running. The new code is incorporated into the program and treated identically to classes and categories loaded at the start.

Dynamic loading can be used to do a lot of different things. For example, the various modules in the System Preferences application are dynamically loaded.

动态加载

一个Objective-C 程序可以在运行时加载一个新的类和种类。新的代码被纳入程序,相同对待,以在开始的时候加载类和种类。
动态加载能够被使用在很多不同的事情上,比如在系统预置的应用程序的各个模块的动态加载。

In the Cocoa environment, dynamic loading is commonly used to allow applications to be customized. Others can write modules that your program loads at runtime—much as Interface Builder loads custom palettes and the OS X System Preferences application loads
custom preference modules. The loadable modules extend what your application can do. They contribute to it in ways that you permit but could not have anticipated or defined yourself. You provide the framework, but others provide the code.

在Cocoa环境下,动态加载通常允许应用程序进行自定义。其他人可以写模块在你程序加载在运行时,就像Interface Builder 加载自定义的调色板和OS X 系统的系统预置应用程序自定义的喜好模块。可加载模块可以扩展你应用程序可以做的事情。他们有助于它在你允许但是不能预期或者你自己不能定义的时候。你提供框架但是其他人提供代码。

Although there is a runtime function that performs dynamic loading of Objective-C modules in Mach-O files (
objc_loadModules
,
defined in
objc/objc-load.h
), Cocoa’s
NSBundle
class
provides a significantly more convenient interface for dynamic loading—one that’s object-oriented and integrated with related services. See the
NSBundle
class
specification in the Foundation framework reference for information on the
NSBundle
class and its use. See OS X ABI Mach-O File Format Reference for
information on Mach-O files.

虽然有一个运行时函数执行Objective-C模块在Mach-O的文件的动态加载(objc_loadModuiles 定义在 objc/objc-load.h中),Cocoa的NSBundle 类提供了一个明显更方便的界面的动态加载-这是面向对象和集成相关服务的。 看NSBundle 类明确说明在Foundation 框架中的NSBundle类中的信息介绍。 看OS X ABI Mach-O 的文件参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: