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.