Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:00:41+00:00 2026-06-13T07:00:41+00:00

I am trying to do some runtime programmation on Objective-C. In order to do

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:00:42+00:00Added an answer on June 13, 2026 at 7:00 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some overflow runtime error happens when my C++ program is trying to write some
Recently I was trying some practice programs in python and I came across this
I have been trying some programs in the C Language and come across to
After following this MVC 4 tutorial series I was trying some of the stuff
I'm trying to add some items to a NSPopUpButton during runtime, but I cannot
I'm trying to load NiB files from subfolders based on some condition at runtime.
I'm trying to create a new annotation with which I'll do some runtime wiring,
Im trying to compile some java files using the Runtime.getRuntime.exec(command) and im generating the
I'm trying to understand some code in the D language runtime. It seems like
i am trying to compile some code to memory at runtime using a CodeDomProvider.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.