Contents

write(toFile:atomically:encoding:)

Writes the contents of the receiver to a file at a given path using a given encoding.

Declaration

func write(toFile path: String, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws

Parameters

  • path:

    The file to which to write the receiver. If path contains a tilde (~) character, you must expand it with Expandingtildeinpath before invoking this method.

  • useAuxiliaryFile:

    If True, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. If False, the receiver is written directly to path. The True option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

  • enc:

    The encoding to use for the output. For possible values, see Nsstringencoding.

Discussion

This method overwrites any existing file at path.

This method stores the specified encoding with the file in an extended attribute under the name com.apple.TextEncoding. The value contains the IANA name for the encoding and the CFStringEncoding value for the encoding, separated by a semicolon. The CFStringEncoding value is written as an ASCII string containing an unsigned 32-bit decimal integer and is not terminated by a null character. One or both of these values may be missing. Examples of the value written include the following:

  • MACINTOSH;0

  • UTF-8;134217984

  • UTF-8;

  • ;3071

The methods init(contentsOfFile:usedEncoding:), NSString/init(contentsOfURL:usedEncoding:)-2c72d, stringWithContentsOfFile:usedEncoding:error:, and NSString/init(contentsOfURL:usedEncoding:)-9jrum use this information to open the file using the right encoding.

See Also

Writing to a File or URL