Unicode.Scalar

struct Unicode.Scalar

A Unicode scalar value.

The Unicode.Scalar type, representing a single Unicode scalar value, is the element type of a string's unicodeScalars collection.

You can create a Unicode.Scalar instance by using a string literal that contains a single character representing exactly one Unicode scalar value.

let letterK: Unicode.Scalar = "K"
let kim: Unicode.Scalar = "김"
print(letterK, kim)
// Prints "K 김"

You can also create Unicode scalar values directly from their numeric representation.

let airplane = Unicode.Scalar(9992)
print(airplane)
// Prints "✈︎"
Inheritance Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, Equatable, ExpressibleByUnicodeScalarLiteral, Hashable, LosslessStringConvertible, TextOutputStreamable View Protocol Hierarchy →
Nested Types Unicode.Scalar.UTF16View
Import import Swift

Initializers

init(_: UInt8)

Creates a Unicode scalar with the specified numeric value.

For example, the following code sample creates a Unicode.Scalar instance with a value of "7":

let codepoint: UInt8 = 55
let seven = Unicode.Scalar(codepoint)
print(seven)
// Prints "7"

v: The code point to use for the scalar.

Declaration

init(_ v: UInt8)
init(_: Unicode.Scalar)

Creates a duplicate of the given Unicode scalar.

Declaration

init(_ v: Unicode.Scalar)
init(unicodeScalarLiteral:)

Creates a Unicode scalar with the specified value.

Do not call this initializer directly. It may be used by the compiler when you use a string literal to initialize a Unicode.Scalar instance.

let letterK: Unicode.Scalar = "K"
print(letterK)
// Prints "K"

In this example, the assignment to the letterK constant is handled by this initializer behind the scenes.

Declaration

init(unicodeScalarLiteral value: Unicode.Scalar)
init?(_: Int)

Creates a Unicode scalar with the specified numeric value.

v: The Unicode code point to use for the scalar. v must be a valid Unicode scalar value, in the ranges 0...0xD7FF or 0xE000...0x10FFFF. In case of an invalid unicode scalar value, nil is returned.

For example, the following code sample creates a Unicode.Scalar instance with a value of an emoji character:

let codepoint = 127881
let emoji = Unicode.Scalar(codepoint)
print(emoji)
// Prints "🎉"

In case of an invalid input value, nil is returned.

let codepoint: UInt32 = extValue // This might be an invalid value. 
if let emoji = Unicode.Scalar(codepoint) {
  print(emoji)
} else {
  // Do something else
}

Declaration

init?(_ v: Int)
init?(_: String)

Instantiates an instance of the conforming type from a string representation.

Declaration

init?(_ description: String)
init?(_: UInt16)

Creates a Unicode scalar with the specified numeric value.

For example, the following code sample creates a Unicode.Scalar instance with a value of "밥", the Korean word for rice:

let codepoint: UInt16 = 48165
let bap = Unicode.Scalar(codepoint)
print(bap!)
// Prints "밥"

In case of an invalid input value, the result is nil.

let codepoint: UInt16 = extValue   // This might be an invalid value
if let bap = Unicode.Scalar(codepoint) {
    print(bap)
} else {
    // Do something else
}

v: The Unicode code point to use for the scalar. The initializer succeeds if v is a valid Unicode scalar value, in the range 0...0xD7FF or 0xE000...0x10FFFF. If v is an invalid unicode scalar value, the result is nil.

Declaration

init?(_ v: UInt16)
init?(_: UInt32)

Creates a Unicode scalar with the specified numeric value.

For example, the following code sample creates a Unicode.Scalar instance with a value of an emoji character:

let codepoint: UInt32 = 127881
let emoji = Unicode.Scalar(codepoint)
print(emoji!)
// Prints "🎉"

In case of an invalid input value, nil is returned.

let codepoint: UInt32 = extValue   // This might be an invalid value
if let emoji = Unicode.Scalar(codepoint) {
  print(emoji)
} else {
  // Do something else
}

v: The Unicode code point to use for the scalar. The initializer succeeds if v is a valid Unicode scalar value---that is, if v is in the range 0...0xD7FF or 0xE000...0x10FFFF. If v is an invalid Unicode scalar value, the result is nil.

Declaration

init?(_ v: UInt32)

Instance Variables

var customMirror: Mirror

A mirror that reflects the Unicode.Scalar instance.

Declaration

var customMirror: Mirror { get }
var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for the Unicode.Scalar instance.

Deprecated: Unicode.Scalar.customPlaygroundQuickLook will be removed in a future Swift version.

Declaration

var customPlaygroundQuickLook: PlaygroundQuickLook { get }
var debugDescription: String

An escaped textual representation of the Unicode scalar, suitable for debugging.

Declaration

var debugDescription: String { get }
var description: String

A textual representation of the Unicode scalar.

Declaration

var description: String { get }
var hashValue: Int

The Unicode scalar's hash value.

Hash values are not guaranteed to be equal across different executions of your program. Do not save hash values to use during a future execution.

Declaration

var hashValue: Int { get }
var isASCII: Bool

A Boolean value indicating whether the Unicode scalar is an ASCII character.

ASCII characters have a scalar value between 0 and 127, inclusive. For example:

let canyon = "Cañón"
for scalar in canyon.unicodeScalars {
    print(scalar, scalar.isASCII, scalar.value)
}
// Prints "C true 67"
// Prints "a true 97"
// Prints "ñ false 241"
// Prints "ó false 243"
// Prints "n true 110"

Declaration

var isASCII: Bool { get }
var utf16: Unicode.Scalar.UTF16View

Declaration

var utf16: Unicode.Scalar.UTF16View { get }
var value: UInt32

A numeric representation of the Unicode scalar.

Declaration

var value: UInt32 { get }

Instance Methods

func escaped(asASCII:)

Returns a string representation of the Unicode scalar.

Scalar values representing characters that are normally unprintable or that otherwise require escaping are escaped with a backslash.

let tab = Unicode.Scalar(9)
print(tab)
// Prints " "
print(tab.escaped(asASCII: false))
// Prints "\t"

When the forceASCII parameter is true, a Unicode.Scalar instance with a value greater than 127 is represented using an escaped numeric value; otherwise, non-ASCII characters are represented using their typical string value.

let bap = Unicode.Scalar(48165)
print(bap.escaped(asASCII: false))
// Prints "밥"
print(bap.escaped(asASCII: true))
// Prints "\u{BC25}"

forceASCII: Pass true if you need the result to use only ASCII characters; otherwise, pass false. Returns: A string representation of the scalar.

Declaration

func escaped(asASCII forceASCII: Bool) -> String
func write(to:)

Writes the textual representation of the Unicode scalar into the given output stream.

target: An output stream.

Declaration

func write<Target>(to target: inout Target)