core data, data models, saving data

Discussion in 'Mac Programming' started by liptonlover, Jun 16, 2008.

  1. liptonlover macrumors 6502a

    Mar 13, 2008
    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!
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Core data is the wrong framework I would have thought for things like this.

    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.
  3. yeroen macrumors 6502a


    Mar 8, 2007
    Cambridge, MA
    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

    or, in a book form which I highly recommend:
  4. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    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 :)
  5. white89gt macrumors regular

    Jan 18, 2006
    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.
  6. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    white could you please supply a line of code? I get so messed up trying to understand the documentation...
  7. white89gt macrumors regular

    Jan 18, 2006
    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.
  8. liptonlover thread starter macrumors 6502a

    Mar 13, 2008
    thanks :) and now that I have access to my computer with the xcode documentation I can look around.

Share This Page