12

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.

eharo2
  • 2,553
  • 1
  • 29
  • 39
lmirosevic
  • 15,787
  • 13
  • 70
  • 116
  • 1
    (There are three basic mechanisms: 1) actually add methods to the class, 2) intercept the "unrecognized message" error and supply an implementation, 3) for properties there is a specific way to intercept setters and getters. But I don't have any of the details at hand.) – Hot Licks Nov 30 '12 at 13:03

3 Answers3

34

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.

Jano
  • 62,815
  • 21
  • 164
  • 192
  • 1
    The code is from "iOS 5 Programming Pushing the Limits" by Rob Napier and Mugunth Kumar. Chapter 20: Deep Objective-C explains how to implement @dynamic properties. The book has a few advanced techniques like that one. I made the graphic with Omnigraffle with info from the book and several places. – Jano Dec 01 '12 at 02:12
  • 2
    +1… [as noted by Mike Ash](http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html), it's often better to look up the type encodings rather than hard code them. – wbyoung Feb 18 '13 at 19:05
  • Holy crap, I've been tweaking an implementation of a dynamic "property bucket" where my code was 90% of this and you finished it off. @Jano, one big question, what do you do if the property is not an object? In other words, "id aValue" will give you an EXC_BAD_ACCESS? – Erik Kerber Apr 02 '14 at 03:02
2

You're asking different things. If you want to be able to use the bracket syntax mySpecialClass[@"anyProperty"] on instances of your class, it is very easy. Just implement the methods:

 - (id)objectForKeyedSubscript:(id)key
 {
      return ###something based on the key argument###
 }

 - (void)setObject:(id)object forKeyedSubscript:(id <NSCopying>)key
 {
      ###set something with object based on key####
 }

It will be called everytime you use the bracket syntax in your source code.

Otherwise if you want to create properties at runtime, there are different ways to proceed, take a look at NSObject's forwardInvocation: method, or look at the Objective-C Runtime Reference for functions to dynamically alter a class...

Guillaume
  • 4,331
  • 2
  • 28
  • 31
  • This isn't correct, those methods are [only defined on NSDictionary](https://duckduckgo.com/?q=objectForKeyedSubscript&t=osx&ia=web) and in some other frameworks, you can't add subscripts to a arbitrary NSObject just by defining them. – alfwatt Aug 05 '23 at 02:34
  • Of course you can, that is why these two methods exist. Have you even tried to implement them? – Guillaume Aug 24 '23 at 16:01
1

Guillaume is right. forwardInvocation: is the way to go. This answer gives some more details: method_missing-like functionality in objective-c (i.e. dynamic delegation at run time)

This has even more details: Equivalent of Ruby method_missing in Objective C / iOS

And these are some other lesser known Obj-C features that might help you: Hidden features of Objective-C

Enjoy!

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165