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

Working with Encodings

class var availableStringEncodings: UnsafePointer<UInt>

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

class var defaultCStringEncoding: UInt

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

class func localizedName(of: UInt)

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

func canBeConverted(to: UInt)

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

func data(using: UInt)

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

var description: String

This NSString object.

var fastestEncoding: UInt

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

var smallestEncoding: UInt

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