Hi, I'm trying to display the values from an NSArray and for some reason I'm only getting the last value. In this code I have to NSNumbers in an NSArray but when I try to out put their values I only get the value from the last object in the array. Any idea what am I doing wrong? Code: [CODE] NSNumber *myInt; myInt = [NSNumber numberWithInt:5]; NSNumber *myInt2; myInt2 = [NSNumber numberWithInt:10]; NSArray *numbers = [NSArray arrayWithObjects:myInt, myInt2, nil]; for(int i = 0; i < [numbers count]; i++) { int nums = [[numbers objectAtIndex:i] intValue]; self.display.text = [NSString stringWithFormat:@"%i \n", nums]; } [/CODE] Out putting the values to the console is not a problem Code: NSLog(@"%@ \n", [numbers objectAtIndex:i]);
Each time you assign a new string to the text field, you discard the prior string that was in the text field. Have a look at NSMutableString's appendFormat: method. Edit: And as a side note, you might want to change your for loop to a for-each... I recently read something that said that, in addition to being more readable, it's quicker than a normal for loop. Code: for (NSNumber* number in numbers) { int num = [number intValue]; [string appendFormat: ... ]; // <== Your code in the ... } You'll also need to add some code before and after the for-each loop... I had written it in but I decided I was telling you to much and you needed to figure out more of it for yourself, so I took it out.
The screen doesn't update immediately when you set the text property on a label. Drawing in iOS happens when your code returns to the system code. Since that only happens when your function is complete only the last value for the text property is shown on screen. One way to implement this kind of display would be to use an NSTimer. Creating a repeating timer. Each time the timer calls back update the label. When you've reached the last value then invalidate the timer.
What do you want it to do? You've stated what it actually does, but haven't described what you expect to see. This will help us in suggesting approaches to a solution.
What Im trying to do is simply display all of the items in the NSArray on the UILabel. I'm still trying to figure this out . Thanks
you should replace Code: for(int i = 0; i < [numbers count]; i++) { int nums = [[numbers objectAtIndex:i] intValue]; self.display.text = [NSString stringWithFormat:@"%i \n", nums]; } with something like this Code: NSString*string = @""; for(int i = 0; i < [numbers count]; i++) { int nums = [[numbers objectAtIndex:i] intValue]; string = [string stringByAppendingFormat:@"%i \n", nums]; } self.display.text = string; notice: i did not check over my syntax, so check it yourself
Still too vague, really. Do you want the values comma-separated, space-separated, one value per line (assuming your UILabel is multi-line), something else?
Thank you all for your reply. @waterskier2007 Will try what you suggested as soon as I can and let you know. @dejo I want every value in a separate line. Sorry, I didn't provide this detail of information because I think I can make that part work by my own. Thank you all a lot.
@waterskier2007 Your code worked, thanks a lot. The only thing is not working is the new line break (\n), I thought this was going to work but for some reason it didn't, I will figure this out by my own. EDIT: For the line break I used the new line operator and (\n) and set the numberOfLInes to 0... Final code. Thank you all very much.