Johannes Ruof
Johannes Ruof

Reputation: 63

Create Mutable Array with name from NSString Variable


I have searched the internet quite a while concerning this problem but didn't find anything related. So I have a simple question: Is it by any means possible to create a Mutable Array in Objective C by using a NSString Variable to name it?

To be more clear a code example (which doesn't work this way):

NSString* someString = @"RandomWord";

NSMutableArray* (someString) = [[NSMutableArray alloc] init];

Any help would be appreciated. If this does not work the way I intend to do it, it would be nice to know another way to dynamically create Arrays with reasonable, retrievable names.

Best Regards,
J


UPDATE:
I have an NSMutableDictionary in which I intend to save multiple NSMutableArray with URLs. The user can name the keys in the Dictionary himself.
For example: The user creates a category named "Animals" and stores multiple URLs in that category. So what I want to achieve is to save the NSMutableArray filled with the URLs for the key "Animals" in the NSMutableDictionary.
I originally thought about just using one NSMutableArray but as I learned the copies of the Array in the NSMutableDictionary are only shallow, so each time I filled the array with something new and stored it the old values were lost in my NSMutableDictionary. But since the User creates as many categories as he wants and also names them how he wants to I need to somehow create arrays that I can reasonably retrieve and add as content to the NSMutableDictionary.
I know this sounds kind of confusing but I hope I made my goal somehow clear.
If there are any questions please ask!

Upvotes: 0

Views: 297

Answers (1)

jrtc27
jrtc27

Reputation: 8526

No you can't. You can, however, use an NSMutableDictionary, to store the instances.

An example:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *someString = @"RandomWord";
[dict setObject:[NSMutableArray array] forKey:someString];

Then to use later:

[[dict objectForKey:someString] doSomethingToTheArray];

Upvotes: 1

Related Questions