• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Best way to save a simple text file?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to save a simple text file?


  • Subject: Re: Best way to save a simple text file?
  • From: Jean-Nicolas Jolivet <email@hidden>
  • Date: Fri, 16 Nov 2007 14:35:20 -0500

What I am trying to do is a Subtitle file editor/converter... (Subtitles file are basically just text files following a certain format.... they are used by movie players such as VLC etc.. to display subtitles on DivX files etc...)

The thing is: They have to support multiple language obviously because they are mostly used to translate (i.e. subtitle) movies in other languages.... UTF-8 might be enough? I'll have to check multiple subtitle files see what the encoding usually is... But basically... it's not like my App will be the only one reading these text files... (i.e. movie players/dvd player etc.. will read'em too)...



Aki Inoue wrote:
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


References: 
 >Best way to save a simple text file? (From: Jean-Nicolas Jolivet <email@hidden>)
 >Re: Best way to save a simple text file? (From: Aki Inoue <email@hidden>)

  • Prev by Date: QuickLook Plugins
  • Next by Date: Re: IS-A relationships in core data
  • Previous by thread: Re: Best way to save a simple text file?
  • Next by thread: Reordering CALayer sublayers without raping my performance?
  • Index(es):
    • Date
    • Thread