Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString to class instance variable

I am looking for a way to convert from NSString to a class instance variable. For sample code below, say filter is "colorFilter". I want filternameclassinstancegohere to be replaced with colorFilter.

- (void)filterSelected:(NSString *)filter
{
    self.filternameclassinstancegohere = ….;
}
like image 706
user523234 Avatar asked Dec 16 '22 06:12

user523234


2 Answers

While there were good suggested solutions given for this question, I discovered what I needed is the NSClassFromString method. Here is a final implementation:

- (void)filterSelected:(NSString *)filter
{
    //self.filternameclassinstancegohere = ….;
    self.myViewController = [[NSClassFromString(filter) alloc] initWithNibName:filter bundle:nil];

}
like image 98
user523234 Avatar answered Dec 28 '22 11:12

user523234


Consider using one NSMutableDictionary instance variable with string keys rather than 40 instance variables.

like image 34
Jakob Egger Avatar answered Dec 28 '22 13:12

Jakob Egger