Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString, matchig name of an NSArray

I am new to Objective C coming from C# .NET, I have this scenario :

Assume I have 5 NSArrays corresponding to 5 UIButtons. the UIButtons have the exact same name as the NSArray, so for example one UIButton is called mainScreen, and there is an NSArray called mainScreen.

Those Five buttons are linked to one IBAction where I do the following :

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
   //Here I need to call the NSArray which has the same name as category
}

Now I can get the actual name of the UIButton, but how can I get the NSArray same as that title? without getting into a lot of if else or switch statements?

like image 732
funkycoldmedia Avatar asked Dec 08 '25 09:12

funkycoldmedia


1 Answers

What I would do is store the arrays inside an NSDictionary. You can then set the 'key' as the name of your array and then the value would be the array itself.

That way you could say:

- (IBAction)btnClick:(id)sender {
    NSString *category = [(UIButton *)sender currentTitle];
    NSLog(category);
    //Here I need to call the NSArray which has the same name as category
    NSArray *theArray = (NSArray*)[self.myDictionary valueForKey:category];
}

Hope this helps!

like image 91
LuisCien Avatar answered Dec 10 '25 00:12

LuisCien