-1

Possible Duplicate:
Objective C Equivalent of PHP's “Variable Variables”

I'm studding Objective-C and have a doubt about this operation:

I know that fast enumeration is better, but I would like to know do that with FOR approach.

In Actionscript 3 I code:

/*
label0.text = [array objectAtIndex:0];
label1.text = [array objectAtIndex:1];
label2.text = [array objectAtIndex:2];
label3.text = [array objectAtIndex:3];
*/

for (int i = 0; i<4; i++ ) {
    this["label" + i].text = array[ i ];
}
Community
  • 1
  • 1

2 Answers2

0

Study this:

NSArray *labelsArray = [NSArray arrayWithObjects: label0, label1, label2, label3, nil];
// this assumes that "array" has already been initialized with the text content  
UILabel *aLabel
for (int i = 0; i<[labelsArray count]; i++ ) {
     aLabel = [labelArray objectAtIndex:i];         
     aLabel.text = [array objectAtIndex:i];
}

That's all, if I understand your question. Read the class reference for NSArray for more details on the methods used.

There is some practicality to this, but this example is very simple, and rather academic.

Jim
  • 5,940
  • 9
  • 44
  • 91
  • I understood your code, but I don't like the idea to do an array for my objects in situations like this. But thank you very much for your answer. – user994102 Oct 14 '11 at 16:53
0

You need to tag them (in the code or in the builder) then just call them wherever you want using:

UILabel*myLabel = (UILabel*)[self.view viewWithTag:i];
myLabel.text = [array objectAtIndex:i];

inside a for of course.. goodluck :)

Dor Bashan
  • 136
  • 2