[译]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 DeclaredPropertiesin The Objective-C Programming Language) includes the
@dynamicdirective:
@dynamic propertyName; |
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—
selfand
_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 .... |
} |
void dynamicMethodIMP(id self, SEL _cmd) { |
// implementation .... |
} |
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) 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
IMPfor the selector first. If you implement
resolveInstanceMethod:but
want particular selectors to actually be forwarded via the forwarding mechanism, you return
NOfor
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 |
Dynamic Loading
An Objective-C program can load and link new classes and categories whileit’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
NSBundleclass
provides a significantly more convenient interface for dynamic loading—one that’s object-oriented and integrated with related services. See the
NSBundleclass
specification in the Foundation framework reference for information on the
NSBundleclass 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 的文件参考。
相关文章推荐
- Objective-C研究之Runtime分析(四)-Dynamic Method Resolution
- Objective-C Runtime Programming Guide 中文翻译
- Objective-C 运行时编程指南 之 Dynamic Method Resolution
- Objective-C Runtime Programming Guide 笔记02 objc_msgSend Function
- Objective-C Runtime Programming Guide
- [译]Objective-C Runtime Programming Guide - Interacting with the Runtime (一)
- [译]Objective-C Runtime Programming Guide -Messaging (二)
- Objective-C运行时编程指南(Objective-C Runtime Programming Guide)
- runtime系列1--Objective-C Runtime Programming Guide简介
- Objective-C Runtime Programming Guide
- Runtime--Dynamic Method Resolution
- Objective-C Runtime Programming Guide 笔记01
- Objective-C Runtime Programming Guide 中文翻译
- runtime系列3--Dynamic Method Resolution-方法的动态决议
- iOS 开发 深入浅出Runtime运行时之方法动态处理(Dynamic Method Resolution)详解
- 深入浅出Cocoa之消息(二)-详解动态方法决议(Dynamic Method Resolution) 【转】
- Introduction to WebKit Objective-C Programming Guide
- [Core Javascirpt] Basic Metaprogramming: Dynamic Method
- Objective-C语言--Key-Value Observing Programming Guide
- Objective-C Primer(3)Multiple arguments for one method, synthesize and dynamic