PDA

View Full Version : for loop problem




moomy
Jul 8, 2010, 04:43 PM
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 :)



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 :o:o:



kainjow
Jul 8, 2010, 04:53 PM
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?

moomy
Jul 8, 2010, 05:09 PM
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.

skunkworker
Jul 8, 2010, 06:36 PM
- (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


@property (nonatomic, retain) NSEnumerator *enum;


then synthesize the property @synthesize enum;
then somewhere create the enum object self.enum = [array1 objectEnumerator];

then on your button click method



- (IBAction) clickedButton: (id) sender
{
[myLabel setText:[enum nextObject]];
}

moomy
Jul 9, 2010, 07:31 AM
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 :)

PhoneyDeveloper
Jul 9, 2010, 09:19 AM
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.

moomy
Jul 9, 2010, 11:11 AM
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;

DemonJim
Jul 9, 2010, 11:32 AM
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??

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:


- (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:


...
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.

PhoneyDeveloper
Jul 9, 2010, 11:39 AM
NSInteger *myInt;

needs to be

NSInteger myInt;

or better

NSInteger myCurrentQuoteIndex;

moomy
Jul 9, 2010, 05:27 PM
thanks guys,

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

moomy
Jul 10, 2010, 01:27 AM
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
:):):):):):)