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
    objects.

    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
    1. Advertisements

  2. Poster

    Bo Lindbergh Guest

    CFStringConvertIANACharSetNameToEncoding followed by
    CFStringConvertEncodingToNSStringEncoding.

    Remember to check for unrecognised values (kCFStringEncodingInvalidId).


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

  3. Poster

    Poster Guest

    In article <iskp0g$gtm$>,
    Bo Lindbergh <> wrote:

    > CFStringConvertIANACharSetNameToEncoding followed by
    > CFStringConvertEncodingToNSStringEncoding.
    >
    > Remember to check for unrecognised values (kCFStringEncodingInvalidId).
    >
    >
    > /Bo Lindbergh


    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
    #3
    1. Advertisements

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.
Similar Threads
  1. CornboyMayse
    Replies:
    1
    Views:
    1,229
    Egidio
    Feb 10, 2008
  2. CornboyMayse
    Replies:
    6
    Views:
    1,838
    Spawn_Dooley
    Feb 20, 2008
  3. Newsbot
    Replies:
    0
    Views:
    839
    Newsbot
    Sep 18, 2008
  4. Eric P. Peterson

    Modem init string help OS 9.2.1

    Eric P. Peterson, Mar 22, 2006, in forum: Communication
    Replies:
    3
    Views:
    273
    Neill Massello
    Mar 23, 2006
  5. William Maslin
    Replies:
    0
    Views:
    426
    William Maslin
    Jan 25, 2008
  6. Florian Zschocke

    NSString from NSData with win-1252 encoding -> nil ?

    Florian Zschocke, Jan 1, 2005, in forum: Programmer Help
    Replies:
    4
    Views:
    522
    Florian Zschocke
    Jan 2, 2005
  7. jim

    Quick NSString question

    jim, Mar 11, 2008, in forum: Programmer Help
    Replies:
    12
    Views:
    451
  8. Poster
    Replies:
    2
    Views:
    995
    Poster
    Jun 9, 2011
Loading...