Here is some code:
File Name: NSObject + dictionary.h
#import <Foundation/Foundation.h> #import <objc/runtime.h> @interface NSObject (dictionary) - (NSMutableDictionary*) getDictionary; @end
File Name: NSObject + dictionary.m
#import "NSObject+dictionary.h" @implementation NSObject (dictionary) - (NSMutableDictionary*) getDictionary { if (objc_getAssociatedObject(self, @"dictionary")==nil) { objc_setAssociatedObject(self,@"dictionary",[[NSMutableDictionary alloc] init],OBJC_ASSOCIATION_RETAIN); } return (NSMutableDictionary *)objc_getAssociatedObject(self, @"dictionary"); }
Now each instance (of each class) has a dictionary in which you can save your custom attributes. Using keyword encoding , you can set this value:
[myObject setValue:attributeValue forKeyPath:@"dictionary.attributeName"]
And you can get the value as follows:
[myObject valueForKeyPath:@"dictionary.attributeName"]
This works great with the Builder interface and custom runtime attributes.
Key Path Type Value dictionary.attributeName String(or other Type) attributeValue
source share