Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zcarter

macrumors member
Original poster
Apr 21, 2007
46
0
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
 
I think you would have to use an SQlite Data Base, but I could very well be wrong.
 
see https://forums.macrumors.com/threads/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)).
 
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:
 
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.
 
Directories..

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?
 
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?
 
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.
 
Writing data explanation

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.
 
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.
 
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.

Code:
//
// 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.
 
This is what I'm using to read files from the resources... not sure if you'd want to save data there mind.

Code:
//
// 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?
 
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
 
Saving Files with C++

I realize this is an old post but really hoping someone on this thread can help me out.

I'm trying to save a file to the iPhone documents directory using C++. I know the standard C++ file functions but don't know how to get the directory. The NSString, NSSearch, etc. referenced in all the examples are not available as it is not objective c. Any help would be appreciated.

Thanks!
 
Write your own utility functions with C linkage that provide the full paths to the app bundle and the documents folder. You'll then be able to access these from your C++ code. You implement those functions using Objective-C.
 
This works for me:

Code:
std::string str;
str = [[NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory
                                            , NSUserDomainMask
                                            , YES) objectAtIndex:0] UTF8String];
 
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:

When I try and use NSString writeToFile: I get "Warning: writeToFile was depreciated in Xcode..."
 
Code:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

Not deprecated.
 
Oh writeToFile:atomically was depreciated not writeToFile:atomically:encoding:error:
 
Right. There was a period of time when apple decided to add NSError** parameters to lots of apis and also updated many of the NSString apis to use encodings. When they did those things they deprecated the older apis. This happened on MacOS X before iPhone OS was available but the headers are shared.

In almost every case where you find a deprecated api there is a new api that replaces it or a new way of accomplishing the same task.
 
Ah... I've been having some issues with "writeToFile" using NSMutableArray lately. For some odd reason I'm able to write to certain file names but not others. Any suggestions?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.