开发者

Xcode: How to address dynamic Variables?

I want to do something like that:

         for (v=1;v=150;v++) {
          for (h=1; h=250;v++)   {
            tile_0%i_0%i.image = [UIImage imageWithData:tmp_content_of_tile]; 开发者_JAVA百科 //1st %i = v; 2nd %i = h
}
}

In the %i should be inserted the current value of "v" or "h"? Is it possible? How is it called?


It is called an array, which in basic C/C++ would look like this:

Tile tile[150][250];
for (int v=0;v<150;v++) {
   for (int h=0; h<250;v++)   {
       tile[v][h].image = [UIImage imageWithData:tmp_content_of_tile];
   }
}

Also take a look at the syntax of the for loop.


I think what you want there is an array or a dictionary. See NSMutableArray and NSMutableDictionary. Even better, though, just use a plain old 2D array, as in the following:

// Allocate 2D array and fill with contents
UIImage*** imgs = (UIImage***) calloc(sizeof(UIImage**),150);
for (int v = 0; v < 150; v++){
   imgs[v] = (UIImage**) calloc(sizeof(UIImage*),250);
   for ( int u = 0; u < 250; u++ ){
      imgs[v][u] = // initialize entry
   }
}

// Using elements
UIImage* img = imgs[dim1_idx][dim2_idx];

// Freeing the array
for ( int v = 0; v < 150; v++ ){
    for (int u = 0; u < 250; u++ ){
       [ imgs[v][u] release ];
    }
    free(imgs[v]);
}
free(imgs);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜