PDA

View Full Version : core data, data models, saving data




liptonlover
Jun 16, 2008, 02:52 PM
I've been reading my library of books, searching online via google extensively, going over archives, going through my bookmarks, but I still don't know how to save a simple text files, then open it again with my application. For learning purposes I have a bare bones document based cocoa app, where the gui is simply a text field. I want to be able to save that text field to a .txt file. But the problem is, I can't find the right tutorial/example for me. Apple's documentation explains WHAT core data is but not how to use it. Online stuff I was able to follow, only to find out at compile time that it's too old of a version and doesn't work. And I'm not far enough in my book yet to do the chapter there. Can anyone help? I'm looking for the simplest example ever, that I can then go over minutely and examine and learn from and ask questions about. Thanks!



Cromulent
Jun 16, 2008, 03:01 PM
I've been reading my library of books, searching online via google extensively, going over archives, going through my bookmarks, but I still don't know how to save a simple text files, then open it again with my application. For learning purposes I have a bare bones document based cocoa app, where the gui is simply a text field. I want to be able to save that text field to a .txt file. But the problem is, I can't find the right tutorial/example for me. Apple's documentation explains WHAT core data is but not how to use it. Online stuff I was able to follow, only to find out at compile time that it's too old of a version and doesn't work. And I'm not far enough in my book yet to do the chapter there. Can anyone help? I'm looking for the simplest example ever, that I can then go over minutely and examine and learn from and ask questions about. Thanks!

Core data is the wrong framework I would have thought for things like this.

http://developer.apple.com/documentation/Cocoa/Conceptual/AppFileMgmt/AppFileMgmt.html

http://developer.apple.com/documentation/Cocoa/Conceptual/LowLevelFileMgmt/LowLevelFileMgmt.html

Edit : having done a bit of reading on the subject I guess you can do some cool stuff with Core Data, the question is do you really need to? If all you are doing is simple text file stuff you could probably even get away with using the C functions. It just depends how complex you want your app and what ideas you have in mind.

yeroen
Jun 16, 2008, 03:14 PM
Cromulent is right, by looking at CoreData you're really overshooting the problem.

The simplest method is to use the C standard IO library. For example, the code segment:

#include <stdio.h>
..
..
FILE *fp;
..
fp = fopen ("/tmp/somefile", w+);
fprintf(fp, "%s", "I'm somefile");
...


Creates a file in /tmp called somefile and writes to it the text "I'm somefile".

A standard reference for all this is:
GNU C Library (http://www.gnu.org/software/libc/manual/html_node/index.html)

or, in a book form which I highly recommend:
APUE (http://www.amazon.com/Programming-Environment-Addison-Wesley-Professional-Computing/dp/0201433079/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1213647375&sr=8-1)

liptonlover
Jun 16, 2008, 03:19 PM
I've been trying to find just ANYTHING to get started in saving data. Core data is the only type of file saving I could find that was mentioned, though I knew that plain C must have some. Thanks guys I will look into those links :)

white89gt
Jun 16, 2008, 04:29 PM
I've been working on this a lot lately, and at the moment I'm just using the writeToFile and initWithContentsOfFile methods of the NSArray and NSMutableArray classes (I think NSString has the same methods, but I'm not sure). There are better ways, but it's pretty damned easy this way.

liptonlover
Jun 16, 2008, 06:26 PM
white could you please supply a line of code? I get so messed up trying to understand the documentation...

white89gt
Jun 17, 2008, 08:52 AM
Here you go. Here's code to save a string to a file. It puts it in the root of your harddrive. You can specify a path to the file if you want.

NSString *myString = [NSString stringWithFormat:@"Save this"];
[myString writeToFile:@"myFile.txt" atomically:YES];

And this loads it.
NSString *myString = [NSString initWithContentsOfFile:@"myFile.txt"];

There's more to it (like using NSFileManagers to check if a file exists, or searching for a particular user's documents directory), but that will get you started. There are also several different version of the writeToFile method, some of which are deprecated. The one that I showed is the most basic, and might throw a warning due to deprecation. It will work, though. I also worked on figuring out NSKeyedArchivers and NSKeyedUnarchivers last night, and they're almost just as easy. However, you won't really need those until you start getting ready to save arrays of custom NSObject subclasses.

liptonlover
Jun 17, 2008, 09:16 AM
thanks :) and now that I have access to my computer with the xcode documentation I can look around.