1

In objective-c is there a way to dynamically create a variable name. Like I pass a string into a method and the method creates an NSString whos name is the string that was passed in. Something like

-(void)create:(NSString *)str{

NSString *[NSString stringWithFormat:@"%@", str];
}

Or maybe there is a way to change an already existing variables name.

NSString *password;
password.name = entryPassword;
4
  • 2
    objC is not an interpreted language Commented Aug 15, 2011 at 22:27
  • 4
    I'm having a hard time understanding what you're trying to do… Commented Aug 15, 2011 at 22:28
  • 3
    Why do you want to do this? I'd be willing to say that if you think you need to do this, then your approach is totally wrong. Commented Aug 15, 2011 at 22:31
  • 2
    Isn't answering "no" better than a downvote here? IMO it's not a poorly asked question, just one that's basic. Commented Dec 1, 2011 at 20:47

4 Answers 4

6

I'm sure that someone will point out some obscure exception, but in general, this is not possible in any compiled language.

Sign up to request clarification or add additional context in comments.

Comments

2

I would look into NSMutableArray.

An NSMutableArray's size can change and all objects are retained in it.

So, you can store locally created strings inside the NSMutableArray and access the string later.

So you can have a ivar mutableArray of type NSMutableArray and then you can store locally created strings:

//In your init method
mutableArray = [[NSMutableArray alloc] init];
//In your create method:
[mutableArray addObject: [NSString initWithString:str]];

This doesn't do exactly what you are saying, but it probably is the closest you are going to get. (Or I am misunderstanding your question).

1 Comment

I'd say an NSMutableDictionary is closer to what he wants. (But that does not change the fact that he should really rethink what he is trying to do and how he is attempting to do it)
2

In C# (also compiled language) it's called reflection : http://msdn.microsoft.com/en-us/library/ms173183.aspx and as per definition you can "dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.".

I don't get why ppl ask "why would one want to do what they want to do" instead of simply answering the question.

Comments

2

I came to this question a few weeks ago and treated the answer with the biggest number of points (by mmc) as a fact, but in at least 1 context you can dynamically “create” variable name in Objective C.

My teacher, Jacek Lampart, showed me how and I thought I’ll share it with other newbies.

The meat:

// Sample for loop

for(int i=1; i<=3; i++)
{
    // Here you define the format of variable.
    // In this example it’s gonna be blablabla1, blablabla2, blablabla3.

    NSString *targetVariableName = [NSString stringWithFormat:@"blablabla%d", i];

    // Sample UIImageView variable.
    // You assign self.blablabla1 (self.blablabla2 and so on) 
    // to currentVariable in a way, which enables you to work
    // with this object (for example setImage to it etc.).

    UIImageView *currentVariable = [self valueForKey:targetVariableName];

    // […]

    // Profit & DRY power! 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.