-2

How to add property in runtime and sampleItem is my item class and I am presenting the .h and .m file. It has two properties but I want add one more property in run time.

sampleItem.h  
@interface sampleItem : NSObject
@property (strong,nonatomic) NSString *name;
@property (nonatomic)  NSString *city;

- (id) initToDefaults;
@end


sampleItem.m
- (id) initToDefaults
{
self. name = @"";
self.city = @" ";
}

Am using above item class with two properties in my project. This is a class using in my project as a entity. Now my question is want to add one more property in runtime.

Can any body please solve my problem.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • 1
    [Does using Google hurt?](https://www.google.hu/search?q=objective-c+add+property+at+run+time) I mean, the **first two** hits are both Stack Overflow answers to this very problem... – The Paramagnetic Croissant Aug 29 '14 at 13:44

1 Answers1

0

You can add more properties in runtime or on the fly but with some limitations.

But as an alternative you can use NSMutableDictionary as a store and keep any number of objects on the fly as an example here.

// sampleItem.h  

@interface sampleItem : NSObject

@property (strong, nonatomic) NSMutableDictionary *store;

@end

and save like:

[store setObject:@"Your name" forKey@"nameKey"];
[store setObject:@"Your city" forKey@"cityKey"];

retrive like:

NSString *name = [store objectForKey:@"nameKey"];
NSString *city = [store objectForKey:@"cityKey"];

but make sure to initiate the store like:

_store = [NSMutableDictionary new];

before you use.

Goppinath
  • 10,569
  • 4
  • 22
  • 45