函數
transcode(_:
transcode(_:from:to:stoppingOnError:into:)
通過調用給定的閉包將給定的輸入從一種 Unicode 編碼轉換為另一種。
聲明
func transcode<Input, InputEncoding, OutputEncoding>(
_ input: Input,
from inputEncoding: InputEncoding.Type,
to outputEncoding: OutputEncoding.Type,
stoppingOnError stopOnError: Bool,
into processCodeUnit: (OutputEncoding.CodeUnit) -> Void
) -> Bool where Input : IteratorProtocol, InputEncoding : _UnicodeEncoding, OutputEncoding : _UnicodeEncoding, Input.Element == InputEncoding.CodeUnit
返回值
true
如果翻譯檢測到 input
中的編碼錯誤;否則,false
。
參數
input
要翻譯的代碼單元的迭代器,編碼為
inputEncoding
。如果stopOnError
是false
,則整個迭代器將被耗盡。否則,如果檢測到編碼錯誤,迭代將停止。inputEncoding
input
的 Unicode 編碼。outputEncoding
目標 Unicode 編碼。
stopOnError
當在
input
中檢測到編碼錯誤時,傳遞true
以停止翻譯。否則,將為每個檢測到的錯誤插入一個 Unicode 替換字符 ("\u{FFFD}"
)。processCodeUnit
一次處理一個
outputEncoding
代碼單元的閉包。
詳述
以下示例將字符串 "Fermata 𝄐"
的 UTF-8 表示形式轉碼為 UTF-32。
let fermata = "Fermata 𝄐"
let bytes = fermata.utf8
print(Array(bytes))
// Prints "[70, 101, 114, 109, 97, 116, 97, 32, 240, 157, 132, 144]"
var codeUnits: [UTF32.CodeUnit] = []
let sink = { codeUnits.append($0) }
transcode(bytes.makeIterator(), from: UTF8.self, to: UTF32.self,
stoppingOnError: false, into: sink)
print(codeUnits)
// Prints "[70, 101, 114, 109, 97, 116, 97, 32, 119056]"
sink
閉包在函數迭代其輸入時使用每個生成的 UTF-32 代碼單元調用。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift type(of:)用法及代碼示例
- Swift KeyValuePairs flatMap(_:)用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Int8 ~(_:)用法及代碼示例
- Swift SetAlgebra isStrictSubset(of:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
- Swift Array enumerated()用法及代碼示例
- Swift FlattenSequence prefix(_:)用法及代碼示例
- Swift Slice endIndex用法及代碼示例
- Swift LazySequence split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift MutableCollection partition(by:)用法及代碼示例
- Swift ReversedCollection min(by:)用法及代碼示例
- Swift RandomNumberGenerator用法及代碼示例
- Swift Dictionary.Keys shuffled()用法及代碼示例
- Swift AnySequence elementsEqual(_:)用法及代碼示例
- Swift UInt &<<(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift DefaultIndices endIndex用法及代碼示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代碼示例
- Swift LazyFilterSequence dropFirst(_:)用法及代碼示例
- Swift LazySequence suffix(from:)用法及代碼示例
- Swift ArraySlice starts(with:)用法及代碼示例
- Swift Int16.Words max()用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 transcode(_:from:to:stoppingOnError:into:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。