1

Can we initalize dynamically any Modal class. rather than creating any NSObject class with property values as likes string inside that class.

default we do code as like: in .h file

@interface MyUser : NSObject
@property (nonatomic, strong) NSString *username,*bio,*website;
@end

in .m file

@implementation InstaUser
@synthesize bio;
@end

To use that we do:

MyUser *sendUser = [[MyUser alloc]init];
sendUser.username = @"JHON";
sendUser.bio = @"abcdcskdfhksfjhfkjsdf";

I Don't want to create so many this type of modal class rather then this just any dynamic method to initalize class property and use it by inline code.

Nick
  • 1,417
  • 1
  • 14
  • 21
Esha
  • 1,328
  • 13
  • 34

2 Answers2

1

I think you meant a flexible model object with dynamically declared properties something like this:

MyModel *user = [[MyModel alloc] init];
user.name = @"name";

MyModel *something = [[MyModel alloc] init];
something.dynamicProperty = @"blahblah";

If so, you cannot. Use NSMutableDictionary instead, or consider to generate model classes from a simple config file by some scripts.

uasi
  • 462
  • 6
  • 23
1

You can use run time feature of objective c class. Create a single model class and add property to it dynamically at run time: For more reference :

How can I add properties to an object at runtime?

Community
  • 1
  • 1
Saurav Nagpal
  • 1,247
  • 1
  • 10
  • 27
  • Thanks, using this I can add any number of property to my dynamic class: class_addProperty([SomeClass class], "name", attrs, 3); – Esha Jun 25 '14 at 10:16