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.

1
  • 1
    What are you really trying to do? Commented Aug 20, 2012 at 22:50

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

lol you are 4 secs faster than me..
It is very possible with a hashmap, otherwise known as an associative array.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.