2

For my iOS app, I created a class named Tile which is a subclass of UIImageView.

The tiles are displayed in a kind of an array of 6 rows and 5 column. I previously created 30 instances of my Tile class. These instances are all named this way: RiCj where i is the row number and j is the column number.

I would like to create a for loop where I would apply a specific treatment to each of my tiles (basically, I want to display the tiles where displayTile is an instance method of the class Tile).

I would love to do something like (I know the code below is incorrect):

for (int i = 1; i <= numberOfRows ; j++) {
  for (int j = 1; j <= numberOfColumns ; j++) {
     [self.RiCj displayTile];
  }
}

I don't know how to do a call to my tiles based on their dynamic string title.

Krizz
  • 11,362
  • 1
  • 30
  • 43
Olivier_d
  • 46
  • 4
  • 2
    You can do that for *properties* (as described below). You can't do it for ordinary variables, though. And using an array is a much better approach than using properties for this purpose. – Hot Licks Aug 26 '14 at 19:24
  • See also [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Aug 26 '14 at 20:40

3 Answers3

0

Yes, you can actually access a property of a class dynamically by creating a string naming the property then using KVC like so:

NSString *propertyName = [NSString stringWithFormat:@"R%dC%d", i, j]; 
tile = [self valueForKey:propertyName];

But should you? No, not in this case. It's a horrible hack when the perfectly nice alternative of making an array (or array of arrays) is available.

Here's what array of array creation and access might look like (by using handy Objective C literals for arrays):

NSArray *tiles = @[
                    @[ tile0C0, tile0C1, tile0C2 ],
                    @[ tile1C0, tile1C1, tile1C2 ],
                    @[ tile2C0, tile2C1, tile2C2 ],
                 ];

for (int i = 0; i < numberOfRows ; j++) {
    for (int j = 0; j < numberOfColumns ; j++) {
        tile = tiles[j][i];
        // do stuff with tile
    }
}
occulus
  • 16,959
  • 6
  • 53
  • 76
0

Yes, technically, it is possible - you may use Key-Value Coding like this:

for (int i = 1; i <= numberOfRows; i++) {
  for (int j = 1; j <= numberOfColumns; j++) {
    NSString* tileName = [NSString stringWithFormat:@"R%dC%d", i, j]; 
    [[self valueForKey:tileName] displayTile];
  }
} 

But you should not. It won't be a clean solution. Array is a more natural choice here.

Krizz
  • 11,362
  • 1
  • 30
  • 43
-2

If I understand correctly, you're trying to access the instances by their variable names dynamically. You can't do that, as your variable name is designed for you, the programmer, and is not available at runtime.

What you can do, however, is to keep a list of your created instances in an array somewhere, and simply iterate over that array when you need to access them.

Alternatively, if you created the 30 tiles as 30 different properties, you could use some dynamic code to get them. At that point, however, I would strongly recommend to use the array technique.

Jonathan M
  • 1,891
  • 13
  • 21
  • ok, I get it. So I should create an array of tiles. Thank you – Olivier_d Aug 26 '14 at 19:13
  • Yes, he can access his propery dynamically, using Key Value Coding. Not that he should, as there are better ways; but he can if he wishes. – occulus Aug 26 '14 at 19:17
  • You mean, like I noted in my 3rd paragraph ? The point is, having 30 properties is most likely not a logical design in this case, unless you want to interact with them separately. – Jonathan M Aug 26 '14 at 19:19
  • In your first para you say "you're trying to access the instances by their variable names dynamically. You can't do that" – occulus Aug 26 '14 at 19:20
  • Keyword : variable name. Not Property name. – Jonathan M Aug 26 '14 at 19:21
  • In his example he write "self.RiCj". That's a property. Where does he refer to them as variable? He didn't. You're the one that used that word. – occulus Aug 26 '14 at 19:23
  • Actually, KVC *can* access instance variables directly. – jlehr Aug 26 '14 at 20:10
  • KVC can access instance variables, but it cannot access local variables, and I thought I understood (as noted at the start of my answer) that he used local variables. Since he's using properties, my 3rd paragraph applies, not the first. – Jonathan M Aug 26 '14 at 20:19