init an NSString with data from a URL load - encoding question.

Discussion in 'Programmer Misc' started by Poster, Jun 7, 2011.

  1. Poster

    Poster Guest

    I'm guessing that I'm probably looking right through what I need - It
    seems like something that should be a common enough need that it would
    be built into Cocoa...

    Due to constraints beyond my control, I'm stuck doing this under XCode
    2.5 and OSX 10.4.11. That said, here's my situation:

    I'm working on a "GetPage" routine, prototyped like so:
    NSString *GetPage(NSString *URLString,
    NSString *RefererString,
    NSURLResponse **Response);

    It does some setup work to build an NSMutableURLRequest, then calls out
    to [NSURLConnection sendSynchronousRequest:...] to get a page from the
    web. That part is going well, and I'm getting back exactly what I
    expect, properly parked in NSData and corresponding NSURLResponse

    Now that I've got the resulting data, I need to turn it into an
    NSString. So I figure I need to do something like this:

    NSString *TheString = [[NSString string] initWithData:TheData
    encoding:<uh-oh... having trouble with what I should use here>];

    As noted above, I'm having problems with what I should use as the
    encoding - Doing something like "TheEncoding = [MyResponse
    textEncodingName]" (Where "MyResponse" is, of course, the NSURLResponse
    that was filled in by the NSURLConnection call) successfully gets me
    back the encoding used on TheData, but where I hit a wall is trying to
    turn that (NSString object) into something that [aString
    initwithData:encoding:] can swallow. The only thing I can come up with
    is a string of half a dozen or so preceding "if"s like the following:
    if ([TheEncoding compare:mad:"utf-8"] == NSOrderedSame)
    UseEncoding = NSUTF8StringEncoding;
    then plug "UseEncoding" into the "initWithData" call - This seems to me
    like a pretty ugly way of doing it. There's gotta be something better!

    Can somebody be so kind as to point me at it?
    Poster, Jun 7, 2011
    1. Advertisements

  2. Poster

    Bo Lindbergh Guest

    CFStringConvertIANACharSetNameToEncoding followed by

    Remember to check for unrecognised values (kCFStringEncodingInvalidId).

    /Bo Lindbergh
    Bo Lindbergh, Jun 7, 2011
    1. Advertisements

  3. Poster

    Poster Guest

    Thanks Bo, your fix is dead in the black.

    Even so, I still kept banging into a problem - Trying to do things as

    NSString *MyString = [[NSString string] initWithData:yadda-yadda-yadda];

    kept giving me complaints about an unrecognized selector. I was banging
    my head against the wall trying to figure it out when it finally dawned
    on me that I wasn't doing it quite right - My problems went away when I
    went to doing it as

    NSString *MyString = [[NSString alloc] initWithData:yadda-yadda-yadda];

    with that variant, I got EXACTLY what I was hoping for. Hooray!

    Apparently, grabbing an empty string, then using initWithData on it
    isn't operationally the same as doing an alloc/init combination.

    Live 'n learn...

    Thanks again!
    Poster, Jun 9, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.