類型方法
width(_:)
返回對給定 Unicode 標量進行編碼所需的代碼單元數。
聲明
static func width(_ x: Unicode.Scalar) -> Int
返回值
x
以 UTF-16 編碼時的寬度,可以是 1
或 2
。
參數
x
一個 Unicode 標量值。
詳述
由於 Unicode 標量值最多需要 21 位來存儲其值,因此某些 Unicode 標量在 UTF-16 中由一對 16 位代碼單元表示。該對的第一個和第二個代碼單元,指定為 leading
和 trailing
代理,組成一個 surrogate pair
。
let anA: Unicode.Scalar = "A"
print(anA.value)
// Prints "65"
print(UTF16.width(anA))
// Prints "1"
let anApple: Unicode.Scalar = "🍎"
print(anApple.value)
// Prints "127822"
print(UTF16.width(anApple))
// Prints "2"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Unicode.UTF16 transcodedLength(of:decodedAs:repairingIllFormedSequences:)用法及代碼示例
- Swift Unicode.UTF16 trailSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 encode(_:into:)用法及代碼示例
- Swift Unicode.UTF16 isTrailSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 leadSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 isLeadSurrogate(_:)用法及代碼示例
- Swift Unicode.UTF16 decode(_:)用法及代碼示例
- Swift Unicode.UTF32 encode(_:into:)用法及代碼示例
- Swift Unicode.UTF8 width(_:)用法及代碼示例
- Swift Unicode.UTF8 isContinuation(_:)用法及代碼示例
- Swift Unicode.UTF8 decode(_:)用法及代碼示例
- Swift Unicode.UTF32 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.UTF16 width(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。