enum Unicode.UTF32
Inheritance UnicodeCodec, _UnicodeEncoding View Protocol Hierarchy →
Associated Types
CodeUnit = UInt32

The basic unit of encoding

EncodedScalar = CollectionOfOne<UInt32>

A valid scalar value as represented in this encoding

ForwardParser = Unicode.UTF32.Parser

A type that can be used to parse CodeUnits into EncodedScalars.

ReverseParser = Unicode.UTF32.Parser

A type that can be used to parse a reversed sequence of CodeUnits into EncodedScalars.

Nested Types Unicode.UTF32.Parser
Import import Swift




Creates an instance of the UTF-32 codec.



Static Variables

static var encodedReplacementCharacter: Unicode.UTF32.EncodedScalar

A unicode scalar value to be used when repairing encoding/decoding errors, as represented in this encoding.

If the Unicode replacement character U+FFFD is representable in this encoding, encodedReplacementCharacter encodes that scalar value.


static var encodedReplacementCharacter: Unicode.UTF32.EncodedScalar { get }

Static Methods

static func decode(_: Unicode.UTF32.EncodedScalar)

Converts from encoded to encoding-independent representation


static func decode(_ source: Unicode.UTF32.EncodedScalar) -> Unicode.Scalar
static func encode(_:)

Converts from encoding-independent to encoded representation, returning nil if the scalar can't be represented in this encoding.


static func encode(_ source: Unicode.Scalar) -> Unicode.UTF32.EncodedScalar?
static func encode(_:into:)

Encodes a Unicode scalar as a UTF-32 code unit by calling the given closure.

For example, like every Unicode scalar, the musical fermata symbol ("𝄐") can be represented in UTF-32 as a single code unit. The following code encodes a fermata in UTF-32:

var codeUnit: UTF32.CodeUnit = 0
UTF32.encode("𝄐", into: { codeUnit = $0 })
// Prints "119056"

Parameters: input: The Unicode scalar value to encode. processCodeUnit: A closure that processes one code unit argument at a time.


static func encode(_ input: Unicode.Scalar, into processCodeUnit: (Unicode.UTF32.CodeUnit) -> Void)

Instance Methods

mutating func decode<I>(_: inout I)

Starts or continues decoding a UTF-32 sequence.

To decode a code unit sequence completely, call this method repeatedly until it returns UnicodeDecodingResult.emptyInput. Checking that the iterator was exhausted is not sufficient, because the decoder can store buffered data from the input iterator.

Because of buffering, it is impossible to find the corresponding position in the iterator for a given returned Unicode.Scalar or an error.

The following example decodes the UTF-16 encoded bytes of a string into an array of Unicode.Scalar instances. This is a demonstration only---if you need the Unicode scalar representation of a string, use its unicodeScalars view.

// UTF-32 representation of "✨Unicode✨"
let codeUnits: [UTF32.CodeUnit] =
        [10024, 85, 110, 105, 99, 111, 100, 101, 10024]

var codeUnitIterator = codeUnits.makeIterator()
var scalars: [Unicode.Scalar] = []
var utf32Decoder = UTF32()
Decode: while true {
    switch utf32Decoder.decode(&codeUnitIterator) {
    case .scalarValue(let v): scalars.append(v)
    case .emptyInput: break Decode
    case .error:
        print("Decoding error")
        break Decode
// Prints "["\u{2728}", "U", "n", "i", "c", "o", "d", "e", "\u{2728}"]"

input: An iterator of code units to be decoded. input must be the same iterator instance in repeated calls to this method. Do not advance the iterator or any copies of the iterator outside this method. Returns: A UnicodeDecodingResult instance, representing the next Unicode scalar, an indication of an error, or an indication that the UTF sequence has been fully decoded.


mutating func decode<I>(_ input: inout I) -> UnicodeDecodingResult where I : IteratorProtocol, I.Element == Unicode.UTF32.CodeUnit