Objective-C-对象是否可以直接执行方法IMP,就好像它是自己的一样? [英] Objective-C - Is there a way for an Object to execute a method IMP directly as if it were its own?

查看:75
本文介绍了Objective-C-对象是否可以直接执行方法IMP,就好像它是自己的一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定我有一个对象,即MyClass的实例.在Objective-C中,可以通过向对象发送消息或使用NSObject的执行"来要求对象执行"选择器.

Presume I have an Object, an instance of MyClass. In Objective-C one can ask the Object to "perform" a selector by either sending it a message or using NSObject's "perform".

此选择器必须在编译时作为类定义的一部分进行定义,更准确地说,作为该类的实例方法,或者在Obj-C Runtime的帮助下,将该方法添加到(整个)MyClass中使用class_addMethod运行时.

This selector has to be defined at compile time as part of the Class definition, more precisely as an Instance method of that class OR with the help of the Obj-C Runtime, have the method added to the (entire) MyClass at runtime with class_addMethod.

我的问题如下:

是否可以向对象发送IMP并要求其自行执行?本质上,我希望对象,MyClass的不同实例能够在整个MyClass都不了解的情况下自行执行操作.本质上,我将这些称为每个对象方法",一个Object1自己执行此IMP,然后另一个Object2获取不同的IMP,依此类推.这些IMPs存储在其他地方,而Object则知道并决定将内容发送到哪里.

Would it be possible to send an object the IMP and ask it to execute it on itself? Essentially I want Objects, different instances of MyClass to execute things on themselves without the entire MyClass knowing about it. Essentially I would call these "per Object methods", an Object1 gets this IMP executed on itself then another Object2 gets a different IMP, and so on. These IMPs are stored somewhere else and it's that Object that knows and decides where to send things.

推荐答案

是可行的

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface DonorTemplateClass : NSObject
- (void)testMethod:(NSString*)aStringAsParameter;
@end
@implementation DonorTemplateClass
- (void) testMethod:(NSString*)aStringAsParameter {
    NSLog(@"%@ :: %@", self.class, aStringAsParameter);
}
@end

@interface AClass : NSObject
@end
@implementation AClass
@end

@interface AnotherClass : NSObject
@end
@implementation AnotherClass
@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        SEL justTheMethodSelector = @selector(testMethod:);
        IMP justTheMethodImplementation = class_getMethodImplementation([DonorTemplateClass class], justTheMethodSelector);

        AClass *anAClassInstance = [[AClass alloc] init];
        AnotherClass *anotherClassInstance = [[AnotherClass alloc] init];

        NSString *aString = @"Test1";

        typedef void (*MyTypeName)(id,SEL, NSString*); // more info about the block syntax on http://goshdarnblocksyntax.com scroll down to "typedef"
        MyTypeName blockName = (MyTypeName)justTheMethodImplementation;

        blockName(anAClassInstance, justTheMethodSelector, aString);
        blockName(anotherClassInstance, justTheMethodSelector, aString);
    }
}

请注意,我将我的IMP转换为类型定义的指针类型.当我仅致电给我时它编译良好,但objc_retain对我崩溃了……所以我说你需要typedef您的IMP可以在使用它们之前使用,但是您可以在任何合适的类的上下文中执行它们

NOTE that I cast my IMP to a typedef'd pointer type. it compiles fine when I just call I but objc_retain crashes then for me ... so I'd say you need to typedef your IMPs before using them but then you can execute them in the context of any suitable class

这篇关于Objective-C-对象是否可以直接执行方法IMP,就好像它是自己的一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆