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

cea

macrumors newbie
Original poster
Oct 26, 2009
22
0
Hello,

The following example prints out the result line by line in the loop. How can i append the result of each line and print it outside the while loop? Thank you

PHP:
NSArray * myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
NSEnumerator * myArrayEnumerator = [myArray objectEnumerator];
NSString *thisObject;
while (thisObject = [myArrayEnumerator nextObject])
{
  NSLog(@"thisObject: %@", thisObject);
}
// NSLog(@"Using while loop and I would like the result 
// outside the loop so: thisObject: One Two Three");

Produces:
thisObject: One
thisObject: Two
thisObject: Three
 
Create an NSMutableString and append the values to it via the appendString: or appendFormat: methods, then just NSLog the mutable string.
 
// EDIT:

I got it. Thank you for your help!

PHP:
#import "untitled.h"


@implementation untitled
- (IBAction)test:(id)sender{
	NSMutableString *str = [NSMutableString string];
	NSArray * myArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
	NSEnumerator * myArrayEnumerator = [myArray objectEnumerator];
	NSString *thisObject;
	while (thisObject = [myArrayEnumerator nextObject]) {
		[str appendString: thisObject];
	}
	NSLog(@"thisObject: %@", str);
}
@end

Result:
thisObject: OneTwoThree
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.