Search code examples
objective-cfor-loopnaming-conventionsvalue-of

variable name with value of int i


i try made something like this:

for (int i=0; i<8; i++) {
    UIButton *btn_i = [[UIButton alloc] initWithFrame:CGRectMake(0 * numberRowsOfMenu, 0 * heightOfRow, btnWidth, btnHeight)];
}

where UIButton *btn_i is btn_ + value of i.

it is possible in Obj-C?


Solution

  • Do you mean you want to wind up with variables with names like btn_0, btn_1, etc?

    No, you cannot do this. You might be better off with something like this:

    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:8];
    for(int i=0; i<8; i++) {
        UIButton *b  = [[UIButton alloc] initWithFrame:...];
        [butttons addObject:b];
        [b release];
    }
    
    // Now you can access the buttons array with indices, e.g:
    UIButton *b = [buttons objectAtIndex:4];