0

How I can create a NSMutableArray (name) dynamically by a NSString value?

e.g. NSString *stringName = @"helloArray1";

Then create the NSMutableArray with the "helloArray1" dynamically.

e.g. NSMutableArray * (--here the stringName / helloArray1---) = [NSMutableArray new];

And then the NSLog:

NSLog(@"%@", (--here the stringName / helloArray1---) );

Thanks for your help

Daniela
  • 15
  • 4
  • You mean you'd like to create a _variable_ with a certain name dynamically? That's not possible; but if you want to fill an existing _instance variable_ with a certain name instead, there is a way to do that. – DarkDust Jul 02 '15 at 08:50
  • One possible way would be a dictionary where the array would be the value for `stringName` key... – Alladinian Jul 02 '15 at 08:51
  • @DarkDust: Have you code example to fill an existing instance variable with a certain name? – Daniela Jul 02 '15 at 08:53
  • @Alladinian: Have you code example? – Daniela Jul 02 '15 at 08:54
  • @Daniela: Search for "Key-Value Coding", an important concept in Objective-C: you can usually do `[self setValue:someObject forKey:@"instanceVariableName"]`. – DarkDust Jul 02 '15 at 09:01
  • @DarkDust: Thx but Key-Value Coding doesn't solve my problem. Unfortunately I think it is your first sentence (...not possible...). – Daniela Jul 02 '15 at 09:14
  • Instead of describing _how_ you want to solve your problem, it might help if you actually described _what_ you're trying to do. What's the "big picture" you're trying to do? – DarkDust Jul 02 '15 at 10:02
  • http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables – J Andrew McCormick Jul 02 '15 at 15:21

1 Answers1

0

I think that's not possible.

Better is set dynamically all into object. For example:

NSMutableDictionary *variables = [NSMutableDictionary new];
for (int i = 0; i < 10; i++) {
    NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
    [variables setObject:temp forKey:[NSString stringWithFormat:@"value_%d", i]];
}

NSLog(@"%@", [variables objectForKey:@"value_1"]);
Jiri Zachar
  • 487
  • 3
  • 8