0

I'm looking for a way to dynamically create NSString objects in objective C based on how many of them I need (between 1 and 5). I then want to use those strings as names of objects which also are dynamically created;

Pseudo Code:

for (i=1, i <= number_of_characters, i++)
{
NSMutableString* theString = [NSMutableString character];
[theString appendString:[NSString stringWithFormat:@"%i ",i]];
UILabel *theString;
[theString release];
}

and I am hoping to get several UILabel objects named:

character1

character2

character3

and so on...

Thanks!

1 Answer 1

4

You can create UILabel objects on the fly, but you can't create variables at runtime. If you want to set the text of the label to theString, that's no problem:

NSMutableArray *labels = [NSMutableArray array];
for (i=1, i <= number_of_characters, i++)
{
    NSMutableString* theString = [NSString stringWithFormat:@"%i ",i];
    UILabel *label = [[UILabel alloc] initWithFrame:someCGRect];
    label.text = theString;
    [labels addObject:label];
    [theString release];
}

Now you've got an array full of labels, each of which has a number as its text. The labels haven't been added to any view yet, so you'll want to take care of that.

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

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.