for loop problem

Discussion in 'iPhone/iPad Programming' started by moomy, Jul 8, 2010.

  1. macrumors newbie

    Joined:
    Apr 28, 2010
    #1
    hi there,

    I am struggling to place my 100 items from my plist into a label, one string at a time, at the push of a button. Here is the relevant code I am trying. Any help would be super :)

    Code:
    
    	int x;
    	for (x=1;x<=100;x=x+1);	
    		
    
    		
    NSString *arrayData1 = [array1 objectAtIndex:x];
    
    		
    		
    		myLabel.text = arrayData1;
    	

    have I put the for loop in the wrong place?? when I run it it just repeatedly pulls the same one string.
    thanks :eek::eek::
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You have a semi-colon in the wrong place in your for loop. The for loop is doing nothing because of that. However even if you fix that you're not going to see anything but the last item in the array. Can you be more specific for what you want to see exactly?
     
  3. thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #3
    hi, thank you for your reply. :)

    I want to display each string in the array, in order, ideally starting with item 0.

    When the button is pressed the next string from the array should appear in the label, replacing the last and so on for up to 100 times.
     
  4. macrumors regular

    Joined:
    Sep 9, 2007
    #4
    Code:
    - (void)readPlist
    {
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *finalPath = [path stringByAppendingPathComponent:@"myStrings.plist"];
    	NSArray *array1 = [NSArray arrayWithContentsOfFile:finalPath];
    	
    	
    	for (int i=0;i<100;i++)
    	{
    		[myLabel setText:[array1 objectAtIndex:i]];
    	}
            // or a fast enumerator
            for (NSString* str  in array1)
           {
                   [myLabel setText:str];
           }
    }
    
    You need to go back and look at the Objective-C user guide & basic C programming.
    Some things wrong with your code
    1. You are retaining an array that does not need to be retained as long as its only getting accessed from within the scope its being called.
    2. You used a ";" instead of a bracket "{",and didn't properly open and close the code that was to be contained in the for loop
    3. You started your for loop at index 1, but normal C style arrays start at place 0 which is the 1st, and to go up to the 100th item you just need to go up to i<100 as the index will be just 99. Also you should use the "x++" instead of "x=x+1".
    4. In some cases using a fast enumerator is a better option then a regular for loop.
    Now for some coding "rules"
    4. You seemed to have created the x variable outside of the for loop, in straightup ANSI C this would be applicable but in this case its just extra, just create it inside of the for loop.

    But your code doesn't seem to do what you want it to do.
    One way of doing this is creating a enumerator in the @properties of the class

    Code:
    @property (nonatomic, retain) NSEnumerator *enum;
    
    then synthesize the property
    Code:
     @synthesize enum; 
    then somewhere create the enum object
    Code:
     self.enum = [array1 objectEnumerator]; 
    then on your button click method

    Code:
    
    - (IBAction) clickedButton: (id) sender
    {
    	[myLabel setText:[enum nextObject]];
    }
    
    
     
  5. thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #5
    Thanks for all the help. It's quite a steep learning curve for me as this is the first language I'm learning. I'm sure with daily practice I can crack it though.

    I had a look at the enumerator and it does look very interesting... however I will be aiming to enable to user to add and delete strings later on and, looking at the documentation, I wasn't sure if enum supported that?

    For now I have declared an NSInteger x in the header file and then put
    x = x+1; within the buttonClicked method. It seems to be doing the trick for now. Will have to look into this further though. And will need a loop to activate different code for when the array comes to an end.


    thanks again :)
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I'm assuming that the reason for this code is to allow the user to see the different strings, perhaps for a little while. Your code won't do that.

    What is the high-level goal? What do you expect the user to see?

    You will likely need to use a timer to iterate through all the strings.
     
  7. thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #7
    the code I have used most recently works to go forward and backwards though the array....but quits when the array gets to the end. May have to use a while loop for that??



    myInt = myInt+1;



    NSString *arrayData1 = [array1 objectAtIndex:myInt];
    quoteLabel.text = arrayData1;

    [/code]

    myInt is declared in the header file as..

    NSInteger *myInt;


    My code for the back button is the same but with
    myInt = myInt-1;
     
  8. macrumors newbie

    DemonJim

    Joined:
    Jun 19, 2010
    Location:
    UK
    #8
    You don't want a loop in the function at all. You just need to add a check after the increment that it hasn't gone too far (or decrement in the 'back' function) and set it back to the first. Something like:

    Code:
    - (void)readPlist
    {
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSString *finalPath = [path stringByAppendingPathComponent:@"myStrings.plist"];
        NSArray *array1 = [NSArray arrayWithContentsOfFile:finalPath];
    	
        if (++myInt >= [array1 count])
        {
            myInt = 0;
        }
         	
        NSString *arrayData1 = [array1 objectAtIndex:myInt];
        quoteLabel.text = arrayData1;
    }
    
    In the back function, do this:

    Code:
        ...
        if (--myInt < 0)
        {
            myInt = [array1 count] - 1;
        }
        ...
    
    Note: "++myInt" increments myInt by 1 before the condition is tested (pre-increment).
    Note2: I removed the 'retain' from the temporary array, otherwise you would have to release it.
     
  9. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    Code:
    NSInteger *myInt;
    needs to be

    Code:
    NSInteger myInt;
    or better

    Code:
    NSInteger myCurrentQuoteIndex;
     
  10. thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #10
    thanks guys,

    I'm off to bed now as it is late here. Will have a look at putting these in tomorrow. :):)
     
  11. thread starter macrumors newbie

    Joined:
    Apr 28, 2010
    #11
    Hi there,
    just tried the revised code, based on your suggestions, and it works a treat.
    really pleased.


    Will be working on the mutalbility next week, should be fun.

    thanks again
    :):):):):):)
     

Share This Page