Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 compilation error with clang when ARC is active :

error: no known class method for selector 'dynamic'

Everything works fine if I use gcc or clang without ARC (-fno-objc-arc option passed), except a warning instead of the error.

I am aware that ARC need to know the name of the method called to figure out how to manage the memory with returned value (following method name convention). But how to this issue without an ugly performSelector call instead of a direct method call ?

Here is my code :

Test.m

#import "Test.h"
#import <objc/runtime.h>

NSString* dynamicImp(id slef, SEL _cmd)
{
    NSLog(@"Dynamic method called");
    return @"dynamicImp";
}

@implementation Test

- (NSString*)name
{
    return @"John";
}

+ (BOOL)resolveClassMethod:(SEL)name
{
    if (name == @selector(dynamic))
    {
        Class metaClass = objc_getMetaClass([NSStringFromClass([self class]) UTF8String]);
        class_addMethod(metaClass, name, (IMP) dynamicImp, "@@:");
        return YES;
    }
    return NO;
}

+ (IMP)methodForSelector:(SEL)aSelector
{
    if (aSelector == @selector(dynamic))
    {
        return (IMP) dynamicImp;
    }
    else
    {
        return [super methodForSelector:aSelector];
    }
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
    if (aSelector == @selector(dynamic))
    {
        return YES;
    }
    else
    {
        return [NSObject respondsToSelector:aSelector];
    }
}

@end

Test.h

#import <Cocoa/Cocoa.h>

@interface Test : NSObject <NSObject> {
    NSString *_name;
}

- (NSString*)name;

@end

main.m

#import <Cocoa/Cocoa.h>
#import <stdio.h>
#import "Test.h"

int main(int argc, char* argv[])
{
    @autoreleasepool {
        Test *test = [[Test alloc] init];
        NSLog(@"Hello, %@", [test name]);
        NSLog(@"How are you , %@", [Test dynamic]);
    }
    return 0;
}

Gcc or clang without ARC

Compilation result

main.m:13:36: warning: class method '+dynamic' not found (return type defaults to 'id')

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

Output

2012-10-22 10:33:15.563 test-clang[957:707] Hello, John 2012-10-22

2012-10-22 10:33:15.565 test-clang[957:707] Dynamic method called 2012-10-22

2012-10-22 10:33:15.565 test-clang[957:707] How are you , dynamicImp

Clang with ARC

Compilation result

main.m:13:36: error: no known class method for selector 'dynamic'

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

PS : I didn't care for the moment on memory management as my goal is to compile this code with ARC activated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
458 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...