Type Method 型メソッド

resolveInstanceMethod(_:)

Dynamically provides an implementation for a given selector for an instance method. ある指定された、インスタンスメソッドに対するセレクタに、実装を動的に提供します。

Declaration 宣言

class func resolveInstanceMethod(_ sel: Selector!) -> Bool

Parameters パラメータ

name

The name of a selector to resolve. この名前のセレクタを解決します。

Return Value 戻り値

true if the method was found and added to the receiver, otherwise false.

Discussion 解説

This method and resolveClassMethod(_:) allow you to dynamically provide an implementation for a given selector.

An Objective-C method is simply a C function that take at least two arguments—self and _cmd. Using the class_addMethod(_:_:_:_:) function, you can add a function to a class as a method. Given the following function: 以下の関数を与えられて:


void dynamicMethodIMP(id self, SEL _cmd)
{
    // implementation ....
}

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


+ (BOOL) resolveInstanceMethod:(SEL)aSEL
{
    if (aSEL == @selector(resolveThisMethodDynamically))
    {
          class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");
          return YES;
    }
    return [super resolveInstanceMethod:aSel];
}

Special Considerations 特別な注意事項

This method is called before the Objective-C forwarding mechanism is invoked. このメソッドは、Objective-Cの転送メカニズムが発動される前に呼び出されます。 If responds(to:) or instancesRespond(to:) is invoked, the dynamic method resolver is given the opportunity to provide an IMP for the given selector first.

See Also 参照

Dynamically Resolving Methods 動的に決定するメソッド