StaticString

struct StaticString

A string type designed to represent text that is known at compile time.

Inheritance ExpressibleByUnicodeScalarLiteral, ExpressibleByExtendedGraphemeClusterLiteral, ExpressibleByStringLiteral, CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable

Instances of the StaticString type are immutable. StaticString provides limited, pointer-based access to its contents, unlike Swift's more commonly used String type. A static string can store its value as a pointer to an ASCII code unit sequence, as a pointer to a UTF-8 code unit sequence, or as a single Unicode scalar value.

Initializers

init init() Required

Creates an empty static string.

Declaration

public init()
init init(extendedGraphemeClusterLiteral:) Required

Creates an instance initialized to a single character that is made up of one or more Unicode scalar values.

Do not call this initializer directly. It may be used by the compiler when you initialize a static string using an extended grapheme cluster.

Declaration

public init(extendedGraphemeClusterLiteral value: StaticString)
init init(stringLiteral:) Required

Creates an instance initialized to the value of a string literal.

Do not call this initializer directly. It may be used by the compiler when you initialize a static string using a string literal.

Declaration

public init(stringLiteral value: StaticString)
init init(unicodeScalarLiteral:) Required

Creates an instance initialized to a single Unicode scalar.

Do not call this initializer directly. It may be used by the compiler when you initialize a static string with a Unicode scalar.

Declaration

public init(unicodeScalarLiteral value: StaticString)

Instance Variables

var customMirror Required

The custom mirror for this instance.

If this type has value semantics, the mirror should be unaffected by subsequent mutations of the instance.

Declaration

var customMirror: Mirror
var debugDescription Required

A textual representation of the static string, suitable for debugging.

Declaration

var debugDescription: String
var description Required

A string representation of the static string.

Declaration

var description: String
var hasPointerRepresentation Required

A Boolean value indicating whether the static string stores a pointer to ASCII or UTF-8 code units.

Declaration

var hasPointerRepresentation: Bool
var isASCII Required

A Boolean value that is true if the static string stores a pointer to ASCII code units.

Use this property in conjunction with hasPointerRepresentation to determine whether a static string with pointer representation stores an ASCII or UTF-8 code unit sequence.

Warning: If the static string stores a single Unicode scalar value, the value of isASCII is unspecified.

Declaration

var isASCII: Bool
var unicodeScalar Required

The stored Unicode scalar value.

The static string must store a single Unicode scalar value. Accessing this property when hasPointerRepresentation is true triggers a runtime error.

Declaration

var unicodeScalar: Unicode.Scalar
var utf8CodeUnitCount Required

The length in bytes of the static string's ASCII or UTF-8 representation.

Warning: If the static string stores a single Unicode scalar value, the value of utf8CodeUnitCount is unspecified.

Declaration

var utf8CodeUnitCount: Int
var utf8Start Required

A pointer to the beginning of the string's UTF-8 encoded representation.

The static string must store a pointer to either ASCII or UTF-8 code units. Accessing this property when hasPointerRepresentation is false triggers a runtime error.

Declaration

var utf8Start: UnsafePointer<UInt8>

Instance Methods

func withUTF8Buffer(_ body: (UnsafeBufferPointer<UInt8>) -> R) -> R Required

Invokes the given closure with a buffer containing the static string's UTF-8 code unit sequence.

This method works regardless of whether the static string stores a pointer or a single Unicode scalar value.

The pointer argument to body is valid only during the execution of withUTF8Buffer(_:). Do not store or return the pointer for later use.

  • Parameter body: A closure that takes a buffer pointer to the static string's UTF-8 code unit sequence as its sole argument. If the closure has a return value, that value is also used as the return value of the withUTF8Buffer(invoke:) method. The pointer argument is valid only for the duration of the method's execution.

Declaration

public func withUTF8Buffer<R>(_ body: (UnsafeBufferPointer<UInt8>) -> R) -> R