How do I save data??

Discussion in 'iOS Programming' started by Zac atack303, Apr 17, 2009.

  1. macrumors newbie

    Apr 16, 2009
    Well a little more complex than that I want to type in a text field then have it save to a txt file.

    I also want to acces it. like readfromfile??? Or somthing???

    Ps I tried [mytextfield writeToFile: @"mytext.txt" atomic: YES]; and it didn't work. (I assigned that line of code to a "done typing" button).
  2. macrumors regular

    Apr 9, 2009
    well ... why should that piece of code work? you just made up that method, it doesn't exist.

    the easiest way to store data is NSUserDefaults ;-)
  3. thread starter macrumors newbie

    Apr 16, 2009
    Seriously I got that method (writetofile) off of apple?!?

    Could you give an example of the Ns data default thingy.
    Also I need a table view to read that data. I'm makeing a sort of entrée list and to add an entrée I need the user to type a name and have it stored on the table view.

    Btw I know that readfromfile doesn't exist, but writetofile I got at the apple iPhone developers page???
  4. macrumors member

    Feb 12, 2009
    Boston, MA
    What language do you program in?

    Edit: sorry, wrong forum. I will try finding a solution for that, however.
  5. macrumors regular

    Apr 9, 2009
    yes, but writeToFile is a method of NSData and you try to call it on UITextField which is no subclass of NSData.

    for an overview over NSUserDefaults. It's the basic class to store preferences in cocoa touch, it works very much like a NSDictionary (it is one, actually).

    I never worked with table view's so far, but as far as I can see from the documentation you should use
    to get the UITableViewCells. every cell has a text property which gives you the text. but table views are not THAT easy I suppose and it seems to me like you are pretty unexperienced, so you might should avoid them for now.
  6. thread starter macrumors newbie

    Apr 16, 2009
    I am sort of unexperianced but not THAT unexerianced I read a book and I made a calculator (which is probably everyones first step) that handles decimals and powers (but not roots). Thanks for the help. Um how would one avoid useing tables when they need a list of entrees??? I appreciate the help.
  7. macrumors 68000

    Feb 12, 2008
    The three most common ways to save data is in a SQLite database, Property List (plist), or NSUserDefaults.

    I find the SQLite example too buffed up so it's harder to follow so I never learned it...but it's definitely good to store large amounts of data.

    Property Lists is what I use and I like it a lot. Property Lists use the writeToFile method.

    Property Lists hold strings, numbers, dictionaries, booleans, arrays, data, and date.

    You almost have it correct. The problem is that you said:
    [mytextfield writeToFile: @"mytext.txt" atomicYES];
    That's not a method for textfields. Also, you didn't specify a path. By default most people write to the Documents directory because that's the directory for read/write. What you can do is:

    NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // The above code is to get the path to the documents directory.
    [mytextfield.text writeToFile:[documentsDirectory stringByAppendingPathComponent:@"mytext.plist"atomicYES];
  8. thread starter macrumors newbie

    Apr 16, 2009
    Hey thanks could you mabye give a more detailed examplr or a link to an iPhone developers page. If you could do that that'd be great thanks.
  9. macrumors regular

    Apr 9, 2009
    btw, NSUserDefaults is also just a .plist, it's the applications central settings file. if you have to store large amounts of data a seperate .plist or a sql database is probably your best choice, but for smaller stuff you can use NSUserDefaults.

Share This Page