Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 25, 2008, 06:26 AM   #1
nickyu
macrumors newbie
 
Join Date: Jun 2008
iPhone Saving File Failed

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]] writeToFileath 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"
}

Last edited by nickyu; Jun 25, 2008 at 06:33 AM.
nickyu is offline   0 Reply With Quote
Old Jun 25, 2008, 12:46 PM   #2
white89gt
macrumors regular
 
Join Date: Jan 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.
white89gt is offline   0 Reply With Quote
Old Jun 25, 2008, 06:52 PM   #3
nickyu
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
Quote:
Originally Posted by white89gt View Post
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.
nickyu is offline   0 Reply With Quote
Old Jun 25, 2008, 10:35 PM   #4
white89gt
macrumors regular
 
Join Date: Jan 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.
white89gt is offline   0 Reply With Quote
Old Jun 26, 2008, 05:00 PM   #5
nickyu
Thread Starter
macrumors newbie
 
Join Date: Jun 2008
Quote:
Originally Posted by white89gt View Post
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.
nickyu is offline   0 Reply With Quote
Old Jun 26, 2008, 08:25 PM   #6
white89gt
macrumors regular
 
Join Date: Jan 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.
white89gt is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
com.apple.IconServicesAgent: Failed to write file jonsuh OS X Mavericks (10.9) 6 Oct 26, 2013 12:29 PM
iPhone: Failed to fetch a cydia file notification hgrimberg Jailbreaks and iOS Hacks 0 Feb 9, 2013 12:18 AM
Resolved: Saving to Plist file. Mvkoe iPhone/iPad Programming 22 Jan 7, 2013 11:47 AM
Excel file saving in iCloud schnarkle iCloud and Apple Services 2 Nov 28, 2012 11:26 AM
Goodreader - Failed copying file The Mercurian iPad Apps 0 Jun 28, 2012 08:26 AM

Forum Jump

All times are GMT -5. The time now is 01:48 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC