Re: Best way to save a simple text file?
Re: Best way to save a simple text file?
- Subject: Re: Best way to save a simple text file?
- From: Aki Inoue <email@hidden>
- Date: Fri, 16 Nov 2007 10:31:50 -0800
Jean-Nicolas,
However, I was wondering... is it really necessary for me to convert
my string to an NSData object before saving it? Isn't there any
other way to do it? Also, what if I would like to support multiple
languages (i.e. multiple encoding??)... right now the string is
converted to UTF8...
The questions all depend on the intended use of your text file.
If you're saving it as your document representation and don't expect
anything else to read the file, it is perfectly fine just using -
[NSString writeToFile:atomically:] directly. The data is saved as
UTF-16. TextEdit and other text editing apps can read/write the file
easily.
This is the most efficient way for saving string data (since they are
in NSString's native format).
You could choose to save in UTF-8 using -[NSString
writeToFile:atomically:encoding:error:] if cross-platform
compatibility is import (i.e. editing with command-line tools).
Aki
On 2007/11/16, at 10:02, Jean-Nicolas Jolivet wrote:
What would be the best way to save a simple NSString to a text file
in a document based application?
Right now I am using the following (in writeToUrl:ofType:error
method):
---------------------------------------------
NSData *data = [NSData dataWithBytes: [myString UTF8String] length:
[myString length]];
BOOL writeSuccess = [data writeToURL:inAbsoluteURL atomically:NO];
---------------------------------------------
However, I was wondering... is it really necessary for me to convert
my string to an NSData object before saving it? Isn't there any
other way to do it? Also, what if I would like to support multiple
languages (i.e. multiple encoding??)... right now the string is
converted to UTF8...
So.. in other words...any other technique to save a simple NSString
to a text file? (Same goes for opening... do I need to convert to
NSData?? Assuming I want to do file opening in readFromUrl). There
are many different examples, but they all do it a different way (and
most are outdated...i.e. using deprecated methods)...
Thanks
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden