NSString writeToFile Crashes on encoding

Discussion in 'iOS Programming' started by StijnSpijker, Jul 3, 2009.

  1. StijnSpijker, Jul 3, 2009
    Last edited: Jan 2, 2013

    StijnSpijker macrumors newbie

    Joined:
    Jan 27, 2009
    Location:
    The Netherlands
    #1
    This is crazy :confused:

    I simply want to write an NSString to file.
    When I do this:

    Code:
    NSString *frameString = @"You're the banana king, Charlie!";
    NSString *path = @"/Users/me/textFile.txt";
    [frameString writeToFile:path atomically:YES];
    All is fine, file get's written, no problems :)
    Buuuut, this method has been deprecated by Apple since 10.4, you now need to specify the encoding, and optionally where you want to return errors.

    So, the last line is replace by this one:
    Code:
    [frameString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
    And then, BAM, uncaught exception:

    Code:
    2009-07-04 01:47:44.315 iPhoneStreamingPlayer[50097:20b] *** -[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance 0x1032000
    2009-07-04 01:47:44.317 iPhoneStreamingPlayer[50097:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance 0x1032000'
    I'm really getting nuts, maybe it's getting too late :p
    I tried several encodings (UTF8, ASCII even UTF16), they all spawn errors.
    Any ideas?
     
  2. StijnSpijker, Jul 3, 2009
    Last edited: Jan 2, 2013

    StijnSpijker thread starter macrumors newbie

    Joined:
    Jan 27, 2009
    Location:
    The Netherlands
    #2
    Okay, sorry, it was just getting late, I fixed the problem.
    You couldn't actually derive it from what I showed you here, but in fact the NSString was retrieved from some other class, but it turned it that wasn't an NSString, but NSMutableData... I casted it wrong, then ofcourse from there everything went wrong.

    So if you get this error: Check whether you're actually handling the object you THINK you are handling.
     

Share This Page