0

I create an array in my app delegate. I want part of the array name to be variable. I have tried componentsSeparatedByString:.

My final code that works:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
testAnimation.animationImages = appDelegate.A_SL1_Deal;

I want the “A_SL1” to be variable. I tried this:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *app_del = @"appDelegate.";
testAnimation.animationImages = [app_del componentsSeparatedByString: @"A_SL1_Deal"];

I also tried just converting everything to a string:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
testAnimation.animationImages = [NSString stringWithFormat:@"appDelegate.%@_Deal", display_string];
jscs
  • 63,694
  • 13
  • 151
  • 195
  • You're on the wrong track; this isn't generally something you want to do with ObjC. Where is the name `A_SL1_Deal` coming from? What are the other options? – jscs Oct 24 '13 at 19:09
  • For some reason, my comment can't recognize code..............A_SL1_Deal is coming from my Appdelegate.h file int i1; self.A_SL1_Deal = [[NSMutableArray alloc] init]; for (i1 = 1; i1<14;) { NSString *imageName = [NSString stringWithFormat:@"deal%i.bmp.png", i1]; UIImage *image = [UIImage imageNamed:imageName]; NSLog(@"A_SL1_Deal: %d", i1); [A_SL1_Deal addObject:image]; i1 = i1 + 1; } – user1467534 Oct 24 '13 at 19:31
  • Basically, I create all my animation arrays in my appdelegate.h then depending on which button it clicked, it will display the correct animation. Instead of adding 50 IF statements, I'm trying to make part of the array name variable. – user1467534 Oct 24 '13 at 19:38
  • possible duplicate of [ObjC equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Oct 24 '13 at 19:46

0 Answers0