转载自:IOS开发工程师--周玉的博客 iOS 开发 深入浅出Runtime运行时之官方翻译--动态方法处理
iOS开发 Rumtime运行时之消息发送机制(一)
iOS开发 Runtime运行时之官方翻译--动态方法解析(二)
iOS开发 Rumtime运行时之消息转发机制(三)
动态方法处理
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 Properties in The Objective-C Programming Language) includes the @dynamic directive:
@dynamic propertyName;
在有些情况下,您可能需要提供一个动态的实现方法。例如,objective - c声明属性特征(见objective - c编程语言)声明属性包括@dynamic指令:
@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.
使得我们可以在运行时动态地为一个 selector 供 实现。我们只要实现 +resolveInstanceMethod: 和/或 +resolveClassMethod: 方法,并在其中为指 定的 selector 供实现即可(通过调用运行时函数 class_addMethod 来添加)。这两个方法都是 NSObject 中的类方法
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方法仅仅是一个C函数,至少需要两个arguments -self和_cmd。您可以添加一个函数来使用函数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:
可以动态地将其添加到一个类作为一个方法(称为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
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.
转发方法(如消息转发所述)和动态方法处理,主要是正交的(动态方法处理是先于消息转发调用的,如果连个方法都实现了,先调用动态方法处理,不能处理时才会调用消息转发)。一个类有机会在消息转发之前使用动态方法处理。如果respondstoselector或者instancesRespondToSelector:被调用,动态方法处理功能是有机会为选择器提供一个IMP。如果你实现resolveInstanceMethod:但要特定的选择器是通过消息转发机制时间,你就为这些选择器返回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.
一个objective - c程序可以在运行的时候加载和链接新类和分类。新代码在类和分类第一次加载的时候就合并到程序中。
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.
动态加载可以用来做很多不同的事情。例如,各种动态加载的模块在系统设置应用程序。
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环境中,动态加载通常被用来允许应用程序定制。其他人可以编写程序的模块加载在runtime-much界面构建器加载自定义调色板和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.
虽然是一个运行时函数执行在Mach-O objective - c的动态加载模块文件(objc_loadModules objc / objc-load.h中定义),可可NSBundle类提供了一个更方便的接口的动态装载一个面向对象的集成与相关服务。看到NSBundle类规范的基础框架参考信息NSBundle类和它的使用。看到OS X ABI Mach-O Mach-O文件格式参考信息文件。
网友评论