Instance Method

writeToURL:atomically:encoding:error:

Writes the contents of the receiver to the URL specified by url using the specified encoding.

Declaration

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;

Parameters

url

The URL to which to write the receiver. Only file URLs are supported.

useAuxiliaryFile

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

The useAuxiliaryFile parameter is ignored if url is not of a type that can be accessed atomically.

enc

The encoding to use for the output.

error

If there is an error, upon return contains an NSError object that describes the problem. If you are not interested in details of errors, you may pass in NULL.

Return Value

YES if the URL is written successfully, otherwise NO (if there was a problem writing to the URL or with the encoding).

Discussion

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 initWithContentsOfFile:usedEncoding:error:, initWithContentsOfURL:usedEncoding:error:, stringWithContentsOfFile:usedEncoding:error:, and stringWithContentsOfURL:usedEncoding:error: use this information to open the file using the right encoding.

See Also

Writing to a File or URL

- writeToFile:atomically:encoding:error:

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software