Joe
Joe

Reputation: 45

Displaying the contents of an NSMutableArray in a UITextView

I have this array, NSSMutableArray *myarray, which has five objects in it, and I am using a loop like this:

for( className *myObject in myarray)
{
    myTextview.text = [NSString stringWithFormat:@"the name is %@", myObject];
}

When I build and run, only the last name shows in my UITextView *myTextview. I logged it, and my loop is working fine -- it's showing all five objects.

The problem seems to be that each time an object is sent to the myTextView, the next object replaces it; is there a way I can hold all of them, so the whole array can be shown?

Upvotes: 3

Views: 3147

Answers (1)

PengOne
PengOne

Reputation: 48398

Each time you pass the loop you are replacing myTextview.text. What you want is to add to the string each time. Try this:

NSMutableString *string = [NSMutableString string];
for( className *myObject in myarray) {
    [string appendString:[NSString stringWithFormat:@"the name is %@\n", myObject]];
}
myTextview.text = string;

Upvotes: 9

Related Questions