PDA

View Full Version : How do I save data??




Zac atack303
Apr 17, 2009, 06:51 PM
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).



BlackWolf
Apr 18, 2009, 10:52 AM
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).

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 ;-)

Zac atack303
Apr 18, 2009, 11:47 AM
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???

Virtuo
Apr 18, 2009, 12:01 PM
What language do you program in?

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

BlackWolf
Apr 18, 2009, 12:11 PM
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???

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/library/documentation/Cocoa/Conceptual/UserDefaults/UserDefaults.pdf
and
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/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.

Zac atack303
Apr 18, 2009, 02:34 PM
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.

Niiro13
Apr 18, 2009, 05:38 PM
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" atomic: YES];

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(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// The above code is to get the path to the documents directory.
[mytextfield.text writeToFile:[documentsDirectory stringByAppendingPathComponent:@"mytext.plist"] atomic: YES];

Zac atack303
Apr 18, 2009, 08:34 PM
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" atomic: YES];

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(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// The above code is to get the path to the documents directory.
[mytextfield.text writeToFile:[documentsDirectory stringByAppendingPathComponent:@"mytext.plist"] atomic: YES];

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.

BlackWolf
Apr 18, 2009, 08:54 PM
The three most common ways to save data is in a SQLite database, Property List (plist), or NSUserDefaults.

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.