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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone 4 Call Failed ssdeg7 iPhone Tips, Help and Troubleshooting 25 Jul 23, 2013 11:13 PM
HELP! SHOULD I USE iPHONE BACKUP EXTRACTOR?iPod Touch 4G Fails To Restore From Backup macx3127 iPod touch 0 Sep 9, 2011 04:57 PM
XMBC: Playback failed for every file type. What last tweak am I missing? macnvrbck Apple TV and Home Theater 2 Aug 3, 2011 09:35 AM
Save Files From Email To Your iPhone? Daiden Jailbreaks and iOS Hacks 5 Sep 12, 2010 04:14 PM
WD mybook failed need help saving files please maturnip Mac Basics and Help 0 Apr 20, 2010 06:24 PM


All times are GMT -5. The time now is 02:44 AM.

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

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