Error compiling with ARC when runtime programming dynamic method

后端 未结 2 1514
遥遥无期
遥遥无期 2020-12-16 08:41

I am trying to do some runtime programmation on Objective-C. In order to do this I override the resolveClassMethod method.

Unfortunately I come up with some

2条回答
  •  温柔的废话
    2020-12-16 09:01

    ARC has definitely thrown a wrench into some of the fun runtime method resolution machinery. There are still a few options, however. Equally as ugly as the performSelector: technique you mentioned is an explicit objc_msgSend() function call. The function needs to be cast with its return and argument types, like so:

    (void (*)(id, SEL)objc_msgSend)([Test class], @selector(dynamic)));
    

    (You'll now get a warning about implicit declaration; just declare extern id objc_msgSend(id, SEL, ...); somewhere.)

    A better option is to cast the object to id when you make the message send (or store it in an id variable to begin with). The compiler never knows anything about the messages that ids respond to, so it can't and doesn't complain about sending arbitrary messages. You can cast a class object to id just as you can an instance.

    [(id)Test dynamic];
    

    or

    [(id)testInstance anotherDynamicName];
    

提交回复
热议问题