StaticString

struct StaticString

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

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.

Inheritance CustomDebugStringConvertible, CustomReflectable, CustomStringConvertible, ExpressibleByExtendedGraphemeClusterLiteral, ExpressibleByStringLiteral, ExpressibleByUnicodeScalarLiteral View Protocol Hierarchy →
Import import Swift

Initializers

init()

Creates an empty static string.

Declaration

init()
init(extendedGraphemeClusterLiteral:)

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

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

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

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

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

init(unicodeScalarLiteral value: StaticString)

Instance Variables

var customMirror: Mirror

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 { get }
var debugDescription: String

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

Declaration

var debugDescription: String { get }
var description: String

A string representation of the static string.

Declaration

var description: String { get }
var hasPointerRepresentation: Bool

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

Declaration

var hasPointerRepresentation: Bool { get }
var isASCII: Bool

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 { get }
var unicodeScalar: Unicode.Scalar

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 { get }
var utf8CodeUnitCount: Int

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 { get }
var utf8Start: UnsafePointer<UInt8>

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> { get }

Instance Methods

func withUTF8Buffer(_:)

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.

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. Returns: The return value, if any, of the body closure.

Declaration

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

Conditionally Inherited Items

The initializers, methods, and properties listed below may be available on this type under certain conditions (such as methods that are available on Array when its elements are Equatable) or may not ever be available if that determination is beyond SwiftDoc.org's capabilities. Please open an issue on GitHub if you see something out of place!

Where ExtendedGraphemeClusterLiteralType == StringLiteralType

init(extendedGraphemeClusterLiteral:)

Creates an instance initialized to the given value.

value: The value of the new instance.

Declaration

init(extendedGraphemeClusterLiteral value: StaticString.StringLiteralType)

Declared In

ExpressibleByStringLiteral

Where ExtendedGraphemeClusterLiteralType == UnicodeScalarLiteralType

init(unicodeScalarLiteral:)

Creates an instance initialized to the given value.

value: The value of the new instance.

Declaration

init(unicodeScalarLiteral value: StaticString.ExtendedGraphemeClusterLiteralType)

Declared In

ExpressibleByStringLiteral , ExpressibleByExtendedGraphemeClusterLiteral