0

Possible Duplicate:
Syntax help - Variable as object name

I have a very basic beginner's question in Objective C:

I'd like to declare a variable that is named after the content/value/literal of another variable. How can I do that?

I.e.

NSString *s = [NSString stringWithFormat:@"variableName"];

// now create a second variable that is named after the literal in s

int *s = 42; // This is what doesn't work. The integer-type variable should be named variableName

Does anyone know how to do this?

Thanks for the answers so far. The reason why I am asking this questions is the following:

I have an array containing values I load from an xml file structured as follows:

<string>name</string><integer>23</integer><real>3.232</real><real>4.556</real> ... (44 times another real number)<string>nextName</string>...(and so on).

The file contains the names for MKPolygons, the number of points for each polygon and the latitude and logitude values for each point of the polygon. I managed to load the file and have its content in an array. Now I want to create MKPolygons from this array which are named as the strings in the array.

Community
  • 1
  • 1
user1612877
  • 391
  • 1
  • 5
  • 19

3 Answers3

5

What you are looking for is a hash table. You can use NSDictionary or NSMutableDictionary or NSHashTable.

Here is an example:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:anObj forKey:@"foo"];
[dict objectForKey:@"foo"];
[dict removeObjectForKey:@"foo"];
[dict release];
Bryan Wolfford
  • 2,102
  • 3
  • 20
  • 26
0

This is not possible in Objective-C, nor in most mainstream languages (save for PHP). What are you trying to accomplish with this?

Marc W
  • 19,083
  • 4
  • 59
  • 71
0

I don't think you can do it. However, you can create an NSDictionary and create a key called variableName and a value called 42. (an NSNumber). Then whenever you want to retrieve this value, you can just do valueForKey.

danqing
  • 3,348
  • 2
  • 27
  • 43