PDA

View Full Version : iPhone SDK Read/Writing to Files




zcarter
Mar 14, 2008, 04:11 AM
Hey guys..

I'm trying to make a to-do list and I need to know how to save data in anyway shape or form. My first thought was to figure out how to Read/Write to files, but I checked for classes on the iPhone reference forms and only found NSFileHandle, NSObject, NSFile, etc..None of them seemed to do the trick. Could anybody provide me with an example of how to save Data to a file..or save data at all if there's a better way..


Thanks a lot,

Zac



Jeremy1026
Mar 14, 2008, 07:11 AM
I think you would have to use an SQlite Data Base, but I could very well be wrong.

gifford
Mar 14, 2008, 08:24 AM
see http://forums.macrumors.com/showthread.php?t=453043

Also i presume u could look at NSData

cmaier
Mar 14, 2008, 10:47 AM
see http://forums.macrumors.com/showthread.php?t=453043

Also i presume u could look at NSData

An easier way than sql, particularly given the small amount of data in a to-do list, would be to serialize or archive your data.

For example, you can automatically write and initFrom NSDictionary's, NSArray's, etc. If you have a nested data structure you can provide callbacks to serialize or archive each element.

I don't remember the calls, but search the documentation for "archive" or "serialize."

(Note: I'm also trying to decide on how to save data. I'm wondering if it is better to save each record in its own file (time machine style) or not. Still trying to figure out the tradeoffs - I am also assuming device-wide search will come along, and am wondering what will make my future life easiest)).

admanimal
Mar 14, 2008, 11:26 AM
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:

fishkorp
Mar 14, 2008, 11:43 AM
The Apple developer how-tos tell you which method to use to get the directory you can write to.

For the app I'm working on I'm using SQLite and set that directory as the directory to store the database. All seems to work well.

zcarter
Mar 14, 2008, 01:49 PM
If for example I put a file in the folder Resources called data.txt...What would the directory be?

/Resources/data.txt doesn't work
data.txt doesn't work
Resources/data.txt doesn't work
/data.txt doesn't work

Any ideas why?

iSee
Mar 14, 2008, 02:10 PM
Read this (you'll have to log in as a registered developer):

Link: https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/chapter_6_section_3.html

HiRez
Mar 14, 2008, 05:41 PM
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?

admanimal
Mar 14, 2008, 05:47 PM
If for example I put a file in the folder Resources called data.txt...What would the directory be?

/Resources/data.txt doesn't work
data.txt doesn't work
Resources/data.txt doesn't work
/data.txt doesn't work

Any ideas why?

If you are talking about putting the file in the Resources directory in XCode, doing so puts the file into the application bundle itself once you compile it. Generally speaking this is -not- where you want to save user data, particularly on an iPhone. The iPhone has very specific places that it allows you to save data.

lg7
Oct 31, 2008, 08:48 PM
I'm new to both the Mac/iPhone platform and the Objective C 2.0 programming language.

Could someone please explain the code posted above to write data using the iPhone SDK. I'd like to write data entered by the user in text boxes to a text file and I am lost.

I'd like to have a table view with a number of entries that a user can select and see/edit details for in a new window with several text boxes.

Hopefully this is clear enough; I can clarify about any of the above if necessary. Thanks in advance.

riruilo
Feb 18, 2009, 07:29 AM
If you are talking about putting the file in the Resources directory in XCode, doing so puts the file into the application bundle itself once you compile it. Generally speaking this is -not- where you want to save user data, particularly on an iPhone. The iPhone has very specific places that it allows you to save data.

Hi!

Could you tell me in which folders should I store my ranking?
I'm doing a game in C++, so I don't use COCOA classes.
I would like to use read/write or std, I now it works, but I don't know where to create or read my file.BTW, I only know resource folders.

Thanks a lot for suggestions.

ghayenga
Feb 18, 2009, 12:25 PM
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?

Yes to both.

lazydog
Feb 18, 2009, 03:06 PM
Hi!

Could you tell me in which folders should I store my ranking?
I'm doing a game in C++, so I don't use COCOA classes.
I would like to use read/write or std, I now it works, but I don't know where to create or read my file.BTW, I only know resource folders.

Thanks a lot for suggestions.


This is what I'm using to read files from the resources... not sure if you'd want to save data there mind.



//
// Get the path to the resources.
//
NSString* resources = [ [ NSBundle mainBundle ] resourcePath ] ;

std::string respath( [ resources UTF8String ] ) ;

std::string fpath = respath + "/" + "a-file.txt" ;



b e n

EDIT: Actually after reading your question again I think I haven't posted anything useful.

riruilo
Feb 18, 2009, 04:25 PM
This is what I'm using to read files from the resources... not sure if you'd want to save data there mind.



//
// Get the path to the resources.
//
NSString* resources = [ [ NSBundle mainBundle ] resourcePath ] ;

std::string respath( [ resources UTF8String ] ) ;

std::string fpath = respath + "/" + "a-file.txt" ;



b e n

EDIT: Actually after reading your question again I think I haven't posted anything useful.

Yep, I already knew that :)
Anyway thanks.
Any idea?

lazydog
Feb 18, 2009, 05:02 PM
I think you need the document directory. Have a look at the iPhone Application Programming Guide/File and Data Management. There's a code snippet that shows you how to get the path.

b e n