Don Wilson
Don Wilson

Reputation: 552

How do I setup a reference variable to another variable/UILabel variable?

Let's say I have several lines of code to clarify specific settings on a given UILabel variable:

numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].text = @"derp";      
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.x = 5;
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.y = 3;

I'd like to setup a reference variable (&$varname in PHP) for this massive array index-specified variable in Obj-C. What is the best way to do this?

Upvotes: 0

Views: 127

Answers (1)

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95549

Just use a pointer:

UILabel* label = numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY];
label.text = @"derp";
label.center.x = 5;
label.center.y = 3;

Since you are not writing into the array, you don't need anything fancier than that. If you were to overwrite the value in the array, then you could use a pointer to a pointer:

UILabel** label_in_array = &numberMarkings[selectedBoxX]/* ... */[selectedNoteY];
// Write to the label
UILabel* label = *label_in_array;
label.text = @"derp";
// Write to the array
[label release];
*label_in_array = [[UILabel alloc] init]; // Now numberMarkings[][][...][]
                                          // holds a new uilabel object.

Upvotes: 2

Related Questions