Instance Method

dataUsingEncoding:allowLossyConversion:

Returns an NSData object containing a representation of the receiver encoded using a given encoding.

Declaration

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;

Parameters

encoding

A string encoding. For possible values, see NSStringEncoding.

flag

If YES, then allows characters to be removed or altered in conversion.

Return Value

An NSData object containing a representation of the receiver encoded using encoding. Returns nil if flag is NO and the receiver can’t be converted without losing some information (such as accents or case).

Discussion

If flag is YES and the receiver can’t be converted without losing some information, some characters may be removed or altered in conversion. For example, in converting a character from NSUnicodeStringEncoding to NSASCIIStringEncoding, the character ‘Á’ becomes ‘A’, losing the accent.

This method creates an external representation (with a byte order marker, if necessary, to indicate endianness) to ensure that the resulting NSData object can be written out to a file safely. The result of this method, when lossless conversion is made, is the default “plain text” format for encoding and is the recommended way to save or transmit a string object.

See Also

Working with Encodings

availableStringEncodings

Returns a zero-terminated list of the encodings string objects support in the application’s environment.

defaultCStringEncoding

Returns the C-string encoding assumed for any method accepting a C string as an argument.

+ stringEncodingForData:encodingOptions:convertedString:usedLossyConversion:

Returns the string encoding for the given data as detected by attempting to create a string according to the specified encoding options.

+ localizedNameOfStringEncoding:

Returns a human-readable string giving the name of a given encoding.

- canBeConvertedToEncoding:

Returns a Boolean value that indicates whether the receiver can be converted to a given encoding without loss of information.

- dataUsingEncoding:

Returns an NSData object containing a representation of the receiver encoded using a given encoding.

description

This NSString object.

fastestEncoding

The fastest encoding to which the receiver may be converted without loss of information.

smallestEncoding

The smallest encoding to which the receiver can be converted without loss of information.

NSStringEncoding

The following constants are provided by NSString as possible string encodings.

NSString Handling Exception Names

These constants define the names of exceptions raised if NSString cannot represent a string in a given encoding, or parse a string as a property list.

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