Best way to save a simple text file?

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

      How about NSString's -writeToURL:atomically:encoding:error:  (10.4
    and above only)?

    --
    I.S.
  • I've came across this method in the doc...

    Could this be used in my NSDocument's writeToUrl method?? (So that it
    works with NSDocument's saving/opening/closing etc..)

    If so, I think it would, indeed, be a better way!

    I. Savant wrote:
    >> 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...
    >>
    >
    > How about NSString's -writeToURL:atomically:encoding:error:  (10.4
    > and above only)?
    >
    > --
    > I.S.
    >
    >
  • This method has the added advantage that under Leopard it will save
    the encoding out, making it possible to open the file as plain text
    correctly in TextEdit and other apps that use NSString to open files.
    Ali

    On Nov 16, 2007, at 10:19 , Jean-Nicolas Jolivet wrote:

    > I've came across this method in the doc...
    >
    > Could this be used in my NSDocument's writeToUrl method?? (So that
    > it works with NSDocument's saving/opening/closing etc..)
    >
    > If so, I think it would, indeed, be a better way!
    >
    >
    > I. Savant wrote:
    >>> 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...
    >>>
    >>
    >> How about NSString's -writeToURL:atomically:encoding:error:  (10.4
    >> and above only)?
    >>
    >> --
    >> I.S.
  • On Nov 16, 2007, at 10:02 AM, 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...

    The best way to save a string to a text file is to use -[NSString
    writeToURL:atomically:encoding:error:], or the corresponding method
    taking a path.  This is even more important on Leopard, because on
    Leopard NSString will tag the file with an extended attribute
    specifying the encoding, so that it can be opened properly by TextEdit
    and anything else using Cocoa APIs to load text files.  Take a look at
    the TextEdit source on Leopard for example usage in a document-based
    application.

    If you happen to need the contents of a string converted into an
    NSData in a particular encoding, rather than a file, you can use -
    [NSString dataUsingEncoding:] instead of copying twice.

    However, please don't think that you need multiple encodings to
    support multiple languages; Unicode encodings (in particular, UTF-8
    and UTF-16) are sufficient for any language you are likely to
    encounter.  Other encodings should be used only if necessary to
    support legacy data and protocols.

    Douglas Davidson
  • On 11/16/07, Jean-Nicolas Jolivet <silvertab...> wrote:
    > I've came across this method in the doc...
    >
    > Could this be used in my NSDocument's writeToUrl method?? (So that it
    > works with NSDocument's saving/opening/closing etc..)

      Do you see a reason why not? Have you tried it yourself?

    --
    I.S.
  • 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
  • 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
    >
    >
  • > The best way to save a string to a text file is to use -[NSString
    > writeToURL:atomically:encoding:error:], or the corresponding method
    > taking a path.  This is even more important on Leopard, because on
    > Leopard NSString will tag the file with an extended attribute
    > specifying the encoding, so that it can be opened properly by
    > TextEdit and anything else using Cocoa APIs to load text files.

    Unfortunately that method doesn't allow for controlling whether the
    save can be lossy or not. Is there a way to manually set the text
    encoding file attribute after using the NSData method? I don't see a
    constant for the atribute in NSFileManager.

    ~Martin
  • No, but the format is documented in the Foundation release notes.
    Ali

    On Nov 16, 2007, at 16:56 , Martin Wierschin wrote:

    >> The best way to save a string to a text file is to use -[NSString
    >> writeToURL:atomically:encoding:error:], or the corresponding method
    >> taking a path.  This is even more important on Leopard, because on
    >> Leopard NSString will tag the file with an extended attribute
    >> specifying the encoding, so that it can be opened properly by
    >> TextEdit and anything else using Cocoa APIs to load text files.
    >
    > Unfortunately that method doesn't allow for controlling whether the
    > save can be lossy or not. Is there a way to manually set the text
    > encoding file attribute after using the NSData method? I don't see a
    > constant for the atribute in NSFileManager.
    >
    > ~Martin
  • Thanks, using that documentation and setxattr() seems to be working
    just fine.

    ~Martin

    On Nov 16, 2007, at 5:21 PM, Ali Ozer wrote:

    > No, but the format is documented in the Foundation release notes.
    > Ali
    >
    >
    > On Nov 16, 2007, at 16:56 , Martin Wierschin wrote:
    >
    >>> The best way to save a string to a text file is to use -[NSString
    >>> writeToURL:atomically:encoding:error:], or the corresponding
    >>> method taking a path.  This is even more important on Leopard,
    >>> because on Leopard NSString will tag the file with an extended
    >>> attribute specifying the encoding, so that it can be opened
    >>> properly by TextEdit and anything else using Cocoa APIs to load
    >>> text files.
    >>
    >> Unfortunately that method doesn't allow for controlling whether
    >> the save can be lossy or not. Is there a way to manually set the
    >> text encoding file attribute after using the NSData method? I
    >> don't see a constant for the atribute in NSFileManager.
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today