-2

I have name of class in string Format. And i want to make Object of that class. As well as i have the name of data member in string.

How to make object of that class ? How to access property of that class ?

Salman Ghumsani
  • 3,647
  • 2
  • 21
  • 34

1 Answers1

1

To create an instance of a class based on the class name:

Class MyClass = NSClassFromString(@"Person");
id someObj = [[MyClass alloc] init];

To access a property of an object by name (key):

NSString *firstName = [someObj valueForKey:@"firstName"];

You can also modify a property of an object by name (key):

[someObj setValue:@"Freddy" forKey:@"firstName];
jlehr
  • 15,557
  • 5
  • 43
  • 45