PDA

View Full Version : iPhone Saving File Failed




nickyu
Jun 25, 2008, 06:26 AM
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:@"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"
}else{
// display "NO"
}



white89gt
Jun 25, 2008, 12:46 PM
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.

nickyu
Jun 25, 2008, 06:52 PM
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.

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

white89gt
Jun 25, 2008, 10:35 PM
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.

nickyu
Jun 26, 2008, 05:00 PM
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.

Sure that would be interesting for me and other people possibly out there.

white89gt
Jun 26, 2008, 08:25 PM
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.