Error compiling with ARC when runtime programming dynamic method

后端 未结 2 1520
遥遥无期
遥遥无期 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

    In your call

    NSLog(@"How are you , %@", [Test dynamic]);
    

    the ARC compiler does not know the return type of the method. But ARC needs to know if the method returns an object to add the appropriate retain/release calls for managing the lifetime.

    Even without ARC you get a compiler warning

    class method '+dynamic' not found (return type defaults to 'id')

    but the ARC compiler is more strict.

    You can call

    NSLog(@"How are you , %@", [[Test class] performSelector:@selector(dynamic)]);
    

    because performSelector returns an id. For functions returning anything other than an object you can use NSInvocation.

    Alternatively, you can declare the dynamic method using a Class Extension:

    @interface Test (DynamicMethods)
    + (NSString *)dynamic;
    @end
    

提交回复
热议问题