實例方法
decode(_:)
開始或繼續解碼 UTF-32 序列。
聲明
mutating func decode<I>(_ input: inout I) -> UnicodeDecodingResult where I : IteratorProtocol, I.Element == UInt32
返回值
UnicodeDecodingResult
實例,表示下一個 Unicode 標量、錯誤指示或 UTF 序列已完全解碼的指示。
參數
input
要解碼的代碼單元的迭代器。
input
在重複調用此方法時必須是相同的迭代器實例。不要在此方法之外推進迭代器或迭代器的任何副本。
詳述
要完全解碼代碼單元序列,請重複調用此方法,直到它返回 UnicodeDecodingResult.emptyInput
。檢查迭代器是否耗盡是不夠的,因為解碼器可以存儲來自輸入迭代器的緩衝數據。
由於緩衝,不可能在迭代器中找到給定返回的Unicode.Scalar
或錯誤的對應位置。
以下示例將字符串的 UTF-16 編碼字節解碼為 Unicode.Scalar
實例數組。這隻是一個演示——如果您需要字符串的 Unicode 標量表示,請使用它的 unicodeScalars
視圖。
// 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
}
}
print(scalars)
// Prints "["\u{2728}", "U", "n", "i", "c", "o", "d", "e", "\u{2728}"]"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Unicode.UTF32 encode(_:into:)用法及代碼示例
- Swift Unicode.UTF16 transcodedLength(of:decodedAs:repairingIllFormedSequences:)用法及代碼示例
- Swift Unicode.UTF16 trailSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 encode(_:into:)用法及代碼示例
- Swift Unicode.UTF8 width(_:)用法及代碼示例
- Swift Unicode.UTF16 isTrailSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF8 isContinuation(_:)用法及代碼示例
- Swift Unicode.UTF16 leadSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 isLeadSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 width(_:)用法及代碼示例
- Swift Unicode.UTF16 decode(_:)用法及代碼示例
- Swift Unicode.UTF8 decode(_:)用法及代碼示例
- Swift Unicode.UTF8 encode(_:into:)用法及代碼示例
- Swift Unicode.CanonicalCombiningClass ...(_:_:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View contains(where:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View dropFirst(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View firstIndex(of:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View randomElement()用法及代碼示例
- Swift Unicode.Scalar.UTF8View contains(where:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View forEach(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View allSatisfy(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View elementsEqual(_:)用法及代碼示例
- Swift Unicode.CanonicalCombiningClass ..<(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View prefix(through:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Unicode.UTF32 decode(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。