Getting a NSString from NSMutableData, possible?

Discussion in 'Mac Programming' started by Monkaaay, Jul 16, 2006.

  1. macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #1
    I'm trying to read the contents of a URL and I came across the NSURLRequest and NSURLConnection objects. I have a NSMutableData object filled with the results but I can't find a way to print that data to the screen. Any ideas?
     
  2. thread starter macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #2
    Well, I found this solution but I wish it were more elegant.

    Code:
    unsigned char byteBuffer[[receivedData length]];
    [receivedData getBytes:byteBuffer];
    NSLog(@"Output: %s", (char *)byteBuffer);
    
     
  3. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    How about

    NSString *s = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
    NSLog(@"%@", s);
    [s release];


    Not sure if that's really any better though.
     
  4. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #4
    Or just use stringWithContentsOfURL:encoding: (I think that's the method...)
    Also, NSLog(s) would work as well.
     

Share This Page