Setting the encoding of a NSString

Discussion in 'iOS Programming' started by Wunk, Feb 9, 2009.

  1. Wunk macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #1
    I'm trying to set the encoding of a string correctly..

    I have a:

    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

    The problem is, that when it retreives a character like ä, it'll stick it into the string as √§ (according to the debugger when I NSLog the string)

    I've tried doing a:
    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL encoding:NSUTF8StringEncoding];

    But all that gives me is a warning when compiling (warning: no -InitwithContentsOfURL:encoding: method found) and gdb complains with: *** -[NSPlaceholderString initWithContentsOfURL:encoding:]: unrecognized selector sent to instance 0x513f50


    Any suggestions on how to correctly populate this NSString with correct encoding ?
     
  2. Wunk thread starter macrumors newbie

    Wunk

    Joined:
    Nov 17, 2008
    Location:
    Netherlands
    #2
    Took a while, but for reference if anyone would ever run into it.., changing the sequence to:

    Code:
    NSError *lookupError = nil;
    NSString *jsonData = [NSString stringWithContentsOfURL:jsonURL
    encoding:NSUTF8StringEncoding error:&lookupError];
    
    Makes the encoding go perfect :rolleyes: :apple:
     
  3. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #3
    Well, I guess that makes sense, considering the actual NSString method is indeed "initWithContentsOfURL:encoding:error:". The dead giveaway should have been the compiler warning.
     

Share This Page