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

  • Home
  • SEARCH
  • 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 7039147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:45:43+00:00 2026-05-28T01:45:43+00:00

I want to add a property implementation method at runtime. Add I use +resolveInstanceMethod,

  • 0

I want to add a property implementation method at runtime. Add I use +resolveInstanceMethod, class_addMethod to do. But below the code, in dynamicN() and dynamicSetN(), they can’t be compiled and I don’t know how to use C-Function to set/get instance variable without synthesize property.

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

float dynamicN(id self, SEL _cmd)
{
    NSString *methodName = NSStringFromSelector(_cmd);
    NSLog(@"%@,%@", methodName, [self description]);
    // return [self n];
}

void dynamicSetN(id self, SEL _cmd, float sname)
{
    printf("setName start;\n");
    // self.n = sname;   
}


@interface bird : NSObject
{
    int height;
    float n;
}

@property float n;
@property int height;

@end

@implementation bird
@synthesize height = height;
@dynamic n;

- (id)init
{
    if (self = [super init]) {
        n = 1.0;
        height = 3;
    }
    return self;
}

- (float) n {
    return n;
}

+ (BOOL) resolveInstanceMethod:(SEL)aSEL
{
    if (aSEL == @selector(n)) {
        //class_addMethod([self class], aSEL, (IMP) dynamicN, "f@:");
        //return YES;
    }
    if (aSEL == @selector(setN:)) {
        class_addMethod([self class], aSEL, (IMP) dynamicSetN, "v@:f");
        return YES;
    }
    return [super resolveInstanceMethod:aSEL];
}
@end

int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    bird *aBird = [[bird alloc] init];
    aBird.n = 3;
    printf("\n%f\n,%d", aBird.n, aBird.height);
    [pool drain];
    return 0;
}
  • 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-05-28T01:45:44+00:00Added an answer on May 28, 2026 at 1:45 am

    @H2CO3 is correct about your setter. The dynamic getter has the same basic problem. You’re trying to call [self n], which doesn’t exist on id. That’s why it won’t compile. (As @H2CO3 notes, this will generate a warning, not an error; you’re all using -Werror, right?) But more importantly, even if it did, this would be an infinite loop. self.n is just a call to [self n], which will dispatch back to this function.

    If you are looking for examples, you can download the code for chapter 20 of iOS 6 Programming Pushing the Limits. Look in the Person project for Person.m. Chapter 28 includes a lot more explanation on how to implement this kind of feature if you’re interested.

    Note that proper naming in ObjC is very important. Your class should be Bird, not bird. Getting naming basics under your belt is critical before attempting advanced features like dynamic dispatch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a property to my User model that returns the number
I have a class which I want to add a property with using formula
Suppose I have a Python class that I want to add an extra property
I have 2 Lists: List<User> List<UserStats> So I want to add a property Count
I'm trying to simply add objects to a mutable array but they WILL NOT
I have dbml with single table users i want add partial class for User
I have a website built with ASP.NET (3.5) and want add some level of
I have a list of string values that I want add to a hashtable
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I used RollingFileAppender. And I want add a blank line to the log when

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.