iPhone Saving File Failed

Discussion in 'iOS Programming' started by nickyu, Jun 25, 2008.

  1. nickyu macrumors newbie

    Jun 13, 2008
    list ---> is a NSMutableArray,
    every node in list has "title", "field2", "field3"
    So I found that :
    [list writeToFile: path atomically:YES]; ----> always return "NO", file not saved

    [[[list objectAtIndex:0] title] writeToFile: path atomically:YES]; ---> is fine, so it means the file is accessible.

    I know that it is supposed to be [NSArray WriteToFile: Automatically:]
    but as document suggested NSMutableArray is extended from NSArray, so I guess it should be fine. Compiler didn't report error on this.

    BTW, I also tried to first convert the NSMutableArray back into NSArray, but it crashes the simulator, here is how I do the convert:

    NSArray *tmpList;
    retVal = [[tmpList initWithArray:list] writeToFile: path atomically:YES];

    I just tried to do it with "initWithObjects:list" as well, it also causes the simulator crash:

    retVal = [[tmpList initWithObjects:list count: (NSUInteger) [list count]] writeToFile:path atomically:YES];

    Could someone point out what the problem here with the file saving, thanks in advance.

    ==================code begin=====================

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:mad:"newslist.txt"];

    NSInteger retVal;
    retVal = [list writeToFile: path atomically:YES];
    //retVal = [@"string" writeToFile: path atomically:YES];
    //retVal = [[[list objectAtIndex:0] title] writeToFile: path atomically:YES];

    if( retVal==YES ){
    // display "Yes"
    // display "NO"
  2. white89gt macrumors regular

    Jan 18, 2006
    I had the same problem, and the best that I can come up with is that an array doesn't know how to save itself when it contains custom nsobject-subclasses. I had to use a NSKeyedArchiver for saving and a NSKeyedUnarchiver for reading.
  3. nickyu thread starter macrumors newbie

    Jun 13, 2008
    Sounds like your NSKeyedArchiver is a good choice.

    I am quite impatient and don't want wait for an answer nor read through manuals, so I did it a rather clumsy way:
    I took the array apart into strings, and save them into many small files string-by-string.
    Luckily there are known finite number of files, this thing creates. so I just want to get things out of my way. oh well, first experience in this, comparatively I think google does a better in explaining their stuff plus java is more straightforward.
  4. white89gt macrumors regular

    Jan 18, 2006
    Saving and loading using NSKeyedArchivers and NSKeyedUnarchivers is actually pretty simple. Forgoing error checking, your app delegate can load and save using three lines of code. You have to add two methods to your subclass(es) so that they'll know how to save and load themselves, but it's pretty easy. If you want, I can put together a simple example (probably just a command line tool, or an empty window that just NSLogs everything) and post it so you and anyone else that wants it can download it.

    Let me know if you're interested, and I'll try to get something up this weekend.
  5. nickyu thread starter macrumors newbie

    Jun 13, 2008
    Sure that would be interesting for me and other people possibly out there.
  6. white89gt macrumors regular

    Jan 18, 2006
    Alright. I'm actually downloading the new iphone beta right now, so it'll be tomorrow or the next day before I'll have time to get something put together.

Share This Page