Instance Method

data(using:allowLossyConversion:)

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

Declaration

func data(using encoding: UInt, allowLossyConversion lossy: Bool) -> Data?

Parameters

encoding

A string encoding. For possible values, see NSStringEncoding.

flag

If true, 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 false and the receiver can’t be converted without losing some information (such as accents or case).

Discussion

If flag is true 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

Related Symbols

func canBeConverted(to: UInt)

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

class var availableStringEncodings: UnsafePointer<UInt>

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