increment variable name in a loop?

Discussion in 'iOS Programming' started by dantastic, Feb 4, 2011.

  1. dantastic
    Expand Collapse
    macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    Um, I haven't been able to figure out how to use variables to name variables.

    For example the piece below here, would be so much cleaner inside a for loop.

    I want to transform this piece:
    Code:
    cell.textLabel0.text = [localValues objectAtIndex:0];
    cell.textLabel1.text = [localValues objectAtIndex:1];
    cell.textLabel2.text = [localValues objectAtIndex:2];
    cell.textLabel3.text = [localValues objectAtIndex:3];
    cell.textLabel4.text = [localValues objectAtIndex:4];
    cell.textLabel5.text = [localValues objectAtIndex:5];
    
    into
    Code:
    for (int i = 0; i < 6; i+=){
       cell.textLabel+i<?HowDoIIncrement?>.text = [localValues objectAtIndex:i];
    }
    
     
  2. mheidt
    Expand Collapse
    macrumors member

    Joined:
    Jun 7, 2005
    #2
    you can't.
    the textLabels need to be in an array.
     
  3. PhoneyDeveloper
    Expand Collapse
    macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  4. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    So, you have a single table cell with six labels in it, is that correct?
     
  5. dantastic
    Expand Collapse
    thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #5
    In this case yes.

    Phoney, can you give me a bit more than valueForKey - you don't mean a Dictionary?

    I don't need to stick the variables in a dictionary or anything, I just want to be able to do like the example. No biggie really, just one of those things in Java that are really handy.
     
  6. PhoneyDeveloper
    Expand Collapse
    macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I said look up the method valueForKey: in NSObject.
     
  7. ulbador
    Expand Collapse
    macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #7
    That's quite a neat implementation of reflection. Never had a reason to use this yet, but it's nice. Thanks!
     
  8. PhoneyDeveloper
    Expand Collapse
    macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    The usual reason to use it is to get around Apple's private apis to get/set private ivars ;-) which is illegal of course and you should never do it.

    Actually I don't think I've ever used it in a shipping app. It's a little too weird for me.

    The kind of thing OP wants to do can be done this way but it's a little too weird for me.
     
  9. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    You might want to consider tagging each of the labels and then using UIView's viewWithTag: instance method.
     
  10. dantastic
    Expand Collapse
    thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #10
    Ok, I see where you are going with valueForKey. It will actually do the job.

    To be honest though the code is a fair bit convoluted so I'm probably better off not looping through. It's just one of those things I suppose, quirks of different languages.

    Thanks for entertaining me anyway!
     
  11. PhoneyDeveloper
    Expand Collapse
    macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008

Share This Page