類型方法
encode(_:
encode(_:into:)
通過在每個代碼單元上調用給定的閉包,將 Unicode 標量編碼為一係列代碼單元。
必需的。
聲明
static func encode(
_ input: Unicode.Scalar,
into processCodeUnit: (Self.CodeUnit) -> Void
)
參數
input
要編碼的 Unicode 標量值。
processCodeUnit
一次處理一個代碼單元參數的閉包。
詳述
例如,音樂 fermata 符號 (“𝄐”) 是單個 Unicode 標量值 (\u{1D110}
),但其 UTF-8 表示需要四個代碼單元。以下代碼使用 UTF8
編解碼器以 UTF-8 編碼 fermata:
var bytes: [UTF8.CodeUnit] = []
UTF8.encode("𝄐", into: { bytes.append($0) })
print(bytes)
// Prints "[240, 157, 132, 144]"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift UnicodeCodec decode(_:)用法及代碼示例
- 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.UTF32 encode(_:into:)用法及代碼示例
- 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:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift Unicode.Scalar escaped(asASCII:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View contains(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View filter(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift Unicode.Scalar.UTF16View enumerated()用法及代碼示例
- Swift Unicode.Scalar.UTF16View reduce(into:_:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View allSatisfy(_:)用法及代碼示例
- Swift Unicode.UTF16 transcodedLength(of:decodedAs:repairingIllFormedSequences:)用法及代碼示例
- Swift Unicode.UTF16 trailSurrogate(_:)用法及代碼示例
- Swift Unicode.Scalar.UTF8View map(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 UnicodeCodec encode(_:into:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。