Structureswift 6.0.1Swift
StaticString
A string type designed to represent text that is known at compile time.
@frozen struct StaticString
Instances of the StaticString
type are immutable.
StaticString
provides only low-level access to its contents, unlike Swift’s more commonly used String
type. A static string can use either of the following as its storage:
a pointer to a null-terminated sequence of UTF-8 code units:
let emoji: StaticString = "\u{1F600}" emoji.hasPointerRepresentation //-> true emoji.isASCII //-> false emoji.unicodeScalar //-> Fatal error! emoji.utf8CodeUnitCount //-> 4 emoji.utf8Start[0] //-> 0xF0 emoji.utf8Start[1] //-> 0x9F emoji.utf8Start[2] //-> 0x98 emoji.utf8Start[3] //-> 0x80 emoji.utf8Start[4] //-> 0x00
a single Unicode scalar value, under very limited circumstances:
struct MyStaticScalar: ExpressibleByUnicodeScalarLiteral { typealias UnicodeScalarLiteralType = StaticString let value: StaticString init(unicodeScalarLiteral value: StaticString) { self.value = value } } let emoji: StaticString = MyStaticScalar("\u{1F600}").value emoji.hasPointerRepresentation //-> false emoji.isASCII //-> false emoji.unicodeScalar.value //-> 0x1F600 emoji.utf8CodeUnitCount //-> Fatal error! emoji.utf8Start //-> Fatal error!
You can use the withUTF8Buffer(_:)
method to access a static string’s contents, regardless of which representation the static string uses.
emoji.withUTF8Buffer { utf8 in
utf8.count //-> 4
utf8[0] //-> 0xF0
utf8[1] //-> 0x9F
utf8[2] //-> 0x98
utf8[3] //-> 0x80
utf8[4] //-> Fatal error!
}
Citizens in Swift
Conformances
protocol BitwiseCopyable
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomReflectable
A type that explicitly supplies its own mirror.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Escapable
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol Sendable
Type members
init(
) Creates an empty static string.
init(extendedGraphemeClusterLiteral: StaticString
) Creates an instance initialized to a single character that is made up of one or more Unicode scalar values.
init(stringLiteral: StaticString
) Creates an instance initialized to the value of a string literal.
init(unicodeScalarLiteral: StaticString
) Creates an instance initialized to a single Unicode scalar.
Instance members
var customMirror: Mirror
var debugDescription: String
A textual representation of the static string, suitable for debugging.
var description: String
A textual representation of the static string.
var hasPointerRepresentation: Bool
A Boolean value that indicates whether the static string stores a pointer to a null-terminated sequence of UTF-8 code units.
var isASCII: Bool
A Boolean value that indicates whether the static string represents only ASCII code units (or an ASCII scalar value).
var unicodeScalar: Unicode.Scalar
A single Unicode scalar value.
var utf8CodeUnitCount: Int
The number of UTF-8 code units (excluding the null terminator).
var utf8Start: UnsafePointer<UInt8>
A pointer to a null-terminated sequence of UTF-8 code units.
func withUTF8Buffer<R>((UnsafeBufferPointer<UInt8>) -> R
) -> R Invokes the given closure with a buffer containing the static string’s UTF-8 code unit sequence (excluding the null terminator).
Type features
Extension in Vapor
Conformances
protocol AsyncResponseEncodable
Can convert
self
to aResponse
.protocol ResponseEncodable
Can convert
self
to aResponse
.
Instance members
func encodeResponse(for: Request
) -> EventLoopFuture<Response> func encodeResponse(for: Request
) async throws -> Response
Instance features
func encodeResponse(status: HTTPStatus, headers: HTTPHeaders, for: Request
) -> EventLoopFuture<Response> Asynchronously encodes
Self
into aResponse
, setting the supplied status and headers.