How do I save data??

Discussion in 'iPhone/iPad Programming' started by Zac atack303, Apr 17, 2009.

  1. macrumors newbie

    Joined:
    Apr 16, 2009
    #1
    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

    Joined:
    Apr 9, 2009
    #2
    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

    Joined:
    Apr 16, 2009
    #3
    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

    Joined:
    Feb 12, 2009
    Location:
    Boston, MA
    #4
    What language do you program in?

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

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

    read
    http://developer.apple.com/iphone/l...ocoa/Conceptual/UserDefaults/UserDefaults.pdf
    and
    http://developer.apple.com/iphone/l...NSUserDefaults_Class/Reference/Reference.html
    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
    cellForRowAtIndexPath:
    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

    Joined:
    Apr 16, 2009
    #6
    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

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #7
    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:
    PHP:
    [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:

    PHP:
    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

    Joined:
    Apr 16, 2009
    #8
    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

    Joined:
    Apr 9, 2009
    #9
    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