Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fstigre

macrumors regular
Original poster
Aug 12, 2008
158
1
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]);
 
Last edited:
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.
 
Last edited:
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.
 
Thank you all for your help, I will work on what you have sugested to make this work.

Thanks a lot!
 
...but when I try to out put their values I only get the value from the last object in the array..

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 I’m 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
 
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...
self.display.numberOfLines=0;

Final code.
NSString*string = @"";

for(int i = 0; i < [numbers count]; i++)
{
int nums = [[numbers objectAtIndex:i] intValue];

self.display.numberOfLines=2;
string = [string stringByAppendingFormat:mad:"%i \n", nums];
}

self.display.text = string;
Thank you all very much.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.