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
 

Jeremy1026

macrumors 68020
Nov 3, 2007
2,215
1,029
I think you would have to use an SQlite Data Base, but I could very well be wrong.
 

cmaier

Suspended
Jul 25, 2007
25,405
33,471
California
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)).
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
If you are just interested in essentially dumping a string to a text file, you can use NSString writeToFile:
 

fishkorp

macrumors 68030
Apr 10, 2006
2,536
650
Ellicott City, MD
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

macrumors member
Original poster
Apr 21, 2007
46
0
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?
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Side question: does iPhone support preferences/defaults? Are there UIDefaults and UINotificationCenter classes?
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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

macrumors newbie
Oct 31, 2008
1
0
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.
 

riruilo

macrumors newbie
Sep 28, 2008
3
0
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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.
 

riruilo

macrumors newbie
Sep 28, 2008
3
0
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?
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

sm4ck

macrumors newbie
Dec 2, 2009
8
0
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!
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
This works for me:

Code:
std::string str;
str = [[NSSearchPathForDirectoriesInDomains(  NSDocumentDirectory
                                            , NSUserDomainMask
                                            , YES) objectAtIndex:0] UTF8String];
 

drf1229

macrumors regular
Jun 22, 2009
237
0
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..."
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Code:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

Not deprecated.
 

drf1229

macrumors regular
Jun 22, 2009
237
0
Oh writeToFile:atomically was depreciated not writeToFile:atomically:encoding:error:
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

drf1229

macrumors regular
Jun 22, 2009
237
0
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.