0

Suppose (for the sake of argument) that i have a class in my iOS project which contains some treatments.

I want to know if it is possible to add dynamically properties to an Objective C object at runtime ?

I need to create dynamically, an object from an NSDictionnary instance wihch have a none fixed pattern of keys&vals. And to create the object with a generic way, i need that the Key from the NSDictionnary goes for the attribute name, and the Val goes for the attribute value.

-(NSMutableArray *) getFromJSON:(NSString *)json
{
JSONDecoder *decoder = [[JSONDecoder alloc] init];
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [decoder objectWithData:jsonData];
NSMutableArray *data = [[NSMutableArray alloc] initWithArray:array];
for (int i = 0; i < [data count]; i++) {
    NSDictionary *modelData = [data objectAtIndex:i];
    // .... Object attributes creation goes here

At this Line,i need to create from this Dic (modelData) an object with attributes names that must correspond to the Keys of our Dic. The attributes of the created Object ( a dummy class) will be :

@property (nonatomic, strong) key0;
@property (nonatomic, strong) key1;
@property (nonatomic, strong) key2;

.. and so On. is that possible ?

i'm creating a ModelManager and different Model subClasses that inherit from the ModelManager class with some specific actions.InThe ModelManager class,i have to do a generic method that creates an NSMuttableArray which contains the list of NSObjects.This objects corresponds to the data pulled from a Json request. Every Json request, returns data specific to one Model. I'm searching how to do this method with a generic manner to inherit it in the subclasses and just cast that dynamic object to the type of the subclass.The only generic way that i have found, is to create a dummy class with attributes created from the json data keys, afterwards cast this NSObject to the Model subclass class.

  • 2
    So what's wrong with just using an `NSMutableDictionary`? – trojanfoe Jul 23 '13 at 11:03
  • Go to this [link](http://stackoverflow.com/questions/7819092/how-can-i-add-properties-to-an-object-at-runtime) this may help you. – Suryakant Sharma Jul 23 '13 at 11:12
  • Thanks for yours comments, i have added some code for more clarification – Marwen Aloui Jul 23 '13 at 11:53
  • 1
    Before we go into constructing a class at runtime, I think it might be better if you explained why you want to do this. The point of a class is that you have data and actions upon that data. Just putting random data into a class is going to be of limited use in which case you are better off just adding the dictionary to your general data class as a property. – Abizern Jul 23 '13 at 11:57
  • i'm creating a ModelManager and different Model subClasses that inherit from the ModelManager class with some specific actions.InThe ModelManager class,i have to do a generic method that creates an NSMuttableArray which contains the list of NSObjects. (i have added the rest of the talk on my main Post ) thx. – Marwen Aloui Jul 23 '13 at 12:45

1 Answers1

0

Just declare it as a property.

@property (nonatomic, strong) NSDictionary * dic;

or

@property (nonatomic, strong) NSMutableDictionary * dic;

And then later at runtime create it the way you want.

self.dic = @{randomKey : randomValue};

or

self.dic = [@{randomKey : randomValue} mutableCopy];
Ecarrion
  • 4,940
  • 1
  • 33
  • 44