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 8919923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:08:40+00:00 2026-06-15T06:08:40+00:00

Is it possible to create an Objective-C class that can have an arbitrary number

  • 0

Is it possible to create an Objective-C class that can have an arbitrary number of dynamic properties at runtime?

I want to be able to call mySpecialClass.anyProperty and intercept this inside my class to be able to provide my own custom implementation that can then return an NSString (for instance) at runtime with raising an exception. Obviously this all has to compile.

Ideal would be if I could refer to my properties using something similar to the new literal syntax, e.g. mySpecialClass["anyProperty"].

I guess in a way I want to create something like a dynamic NSDictionary with no CFDictionary backing store, that executes 2 custom methods on property getting and setting respectively, with the property name passed in to these accessor methods so they can decide what to do.

  • 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-15T06:08:42+00:00Added an answer on June 15, 2026 at 6:08 am

    There are at least two ways to do this.

    Subscripting

    Use objectForKeyedSubscript: and setObject:forKeyedSubscript:

     @property (nonatomic,strong) NSMutableDictionary *properties;
    
     - (id)objectForKeyedSubscript:(id)key {
          return [[self properties] valueForKey:[NSString stringWithFormat:@"%@",key]];
     }
    
     - (void)setObject:(id)object forKeyedSubscript:(id <NSCopying>)key {
          [[self properties] setValue:object forKey:[NSString stringWithFormat:@"%@",key]];
     }
    
     Person *p = [Person new];
     p[@"name"] = @"Jon";
     NSLog(@"%@",p[@"name"]);
    

    resolveInstanceMethod:

    This is the objc_sendMsg executed by the runtime for all methods:

    objc_sendMsg

    If you look at the bottom, you have the opportunity to resolveInstanceMethod:, which lets you redirect the method call to one of your choosing. To answer your question, you need to write a generic getter and setter that looks-up a value on a dictionary ivar:

    // generic getter
    static id propertyIMP(id self, SEL _cmd) {
        return [[self properties] valueForKey:NSStringFromSelector(_cmd)];
    }
    
    
    // generic setter
    static void setPropertyIMP(id self, SEL _cmd, id aValue) {
    
        id value = [aValue copy];
        NSMutableString *key = [NSStringFromSelector(_cmd) mutableCopy];
    
        // delete "set" and ":" and lowercase first letter
        [key deleteCharactersInRange:NSMakeRange(0, 3)];
        [key deleteCharactersInRange:NSMakeRange([key length] - 1, 1)];
        NSString *firstChar = [key substringToIndex:1];
        [key replaceCharactersInRange:NSMakeRange(0, 1) withString:[firstChar lowercaseString]];
    
        [[self properties] setValue:value forKey:key];
    }
    

    And then implement resolveInstanceMethod: to add the requested method to the class.

    + (BOOL)resolveInstanceMethod:(SEL)aSEL {
        if ([NSStringFromSelector(aSEL) hasPrefix:@"set"]) {
            class_addMethod([self class], aSEL, (IMP)setPropertyIMP, "v@:@");
        } else {
            class_addMethod([self class], aSEL,(IMP)propertyIMP, "@@:");
        }
        return YES;
    }
    

    You could also do it returning a NSMethodSignature for the method, which is then wrapped in a NSInvocation and passed to forwardInvocation:, but adding the method is faster.

    Here is a gist that runs in CodeRunner. It doesn’t handle myClass["anyProperty"] calls.

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

Sidebar

Related Questions

In some languages like C# and Objective C, you can create class extensions. That
Is it possible to create a simple objective C object in xcode that has
In Objective-C I can create categories to extend a class. I can also add
Is it possible to create classes dynamically in Objective-C? I want to do something
Possible Duplicate: How does @synchronized lock/unlock in Objective-C? I have an application that creates
I want to create an Objective-C application which lets you specify a class implementation
Possible Duplicate: Creating an abstract class in Objective C Hi Can any one please
Is it possible to create private property in Objective-C? I do know that a
I want to know that is it possible to create a fully portable virtual
Possible Duplicate: create multiple variables based on an int count Objective C Equivalent of

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.