Print result outside of the while loop

Discussion in 'Mac Programming' started by cea, Dec 12, 2009.

  1. cea macrumors newbie

    Joined:
    Oct 26, 2009
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Create an NSMutableString and append the values to it via the appendString: or appendFormat: methods, then just NSLog the mutable string.
     
  3. cea thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #3
    // 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 appendStringthisObject];
        }
        
    NSLog(@"thisObject: %@"str);
    }
    @
    end
    Result:
    thisObject: OneTwoThree
     

Share This Page