CustomStringConvertible
A type with a customized textual representation.
protocol CustomStringConvertible
Types that conform to the CustomStringConvertible
protocol can provide their own representation to be used when converting an instance to a string. The String(describing:)
initializer is the preferred way to convert an instance of any type to a string. If the passed instance conforms to CustomStringConvertible
, the String(describing:)
initializer and the print(_:)
function use the instance’s custom description
property.
Accessing a type’s description
property directly or using CustomStringConvertible
as a generic constraint is discouraged.
Conforming to the CustomStringConvertible Protocol
Add CustomStringConvertible
conformance to your custom types by defining a description
property.
For example, this custom Point
struct uses the default representation supplied by the standard library:
struct Point {
let x: Int, y: Int
}
let p = Point(x: 21, y: 30)
print(p)
// Prints "Point(x: 21, y: 30)"
After implementing the description
property and declaring CustomStringConvertible
conformance, the Point
type provides its own custom representation.
extension Point: CustomStringConvertible {
var description: String {
return "(\(x), \(y))"
}
}
print(p)
// Prints "(21, 30)"
Requirements
var description: String
A textual representation of this instance.
Citizens in Swift
Subtypes
protocol BinaryInteger
An integer type with a binary representation.
protocol CodingKey
A type that can be used as a key for encoding and decoding.
protocol FixedWidthInteger
An integer type that uses a fixed size for every instance.
protocol LosslessStringConvertible
A type that can be represented as a string in a lossless, unambiguous way.
protocol SIMD
A SIMD vector of a fixed number of elements.
protocol SignedInteger
An integer type that can represent both positive and negative values.
protocol StringProtocol
A type that can represent a string as a collection of characters.
protocol UnsignedInteger
An integer type that can represent only nonnegative values.
Available in _RegexParser
Members
Available in Foundation
Subtypes
protocol AttributedStringProtocol
protocol ReferenceConvertible
Decorates types which are backed by a Foundation reference type.
Extension in GRPC
Subtypes
protocol GRPCErrorProtocol
Requirements for
GRPCError
types.
Extension in Atomics
Subtypes
protocol AtomicInteger
A type that supports atomic integer operations through a separate atomic storage representation.
Extension in Crypto
Subtypes
protocol Digest
A type that represents the output of a hash.
protocol MessageAuthenticationCode
A type that represents a message authentication code.
Extension in Testing
Subtypes
Extension in TraceableErrors
Subtypes
protocol NamedError
An error type that supports printing with a custom heading.
protocol TraceableError
A link in a propogated error.
Extension in Durations
Subtypes
Extension in SwiftSyntax
Subtypes
protocol BracedSyntax
protocol DeclGroupSyntax
protocol DeclSyntaxProtocol
Protocol to which all
DeclSyntax
nodes conform.protocol EffectSpecifiersSyntax
protocol ExprSyntaxProtocol
Protocol to which all
ExprSyntax
nodes conform.protocol FreestandingMacroExpansionSyntax
protocol MissingNodeSyntax
Represents a layout node that is missing in the source file.
protocol NamedDeclSyntax
protocol ParenthesizedSyntax
protocol PatternSyntaxProtocol
Protocol to which all
PatternSyntax
nodes conform.protocol RawDeclSyntaxNodeProtocol
protocol RawExprSyntaxNodeProtocol
protocol RawPatternSyntaxNodeProtocol
protocol RawStmtSyntaxNodeProtocol
protocol RawSyntaxNodeProtocol
All typed raw syntax nodes conform to this protocol.
RawXXXSyntax
is a typed wrappeer ofRawSyntax
.protocol RawTypeSyntaxNodeProtocol
protocol StmtSyntaxProtocol
Protocol to which all
StmtSyntax
nodes conform.protocol SyntaxChildChoices
Protocol for the enums nested inside
Syntax
nodes that enumerate all the possible types a child node might have.protocol SyntaxCollection
protocol SyntaxProtocol
Provide common functionality for specialized syntax nodes. Extend this protocol to provide common functionality for all syntax nodes. DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
protocol TypeSyntaxProtocol
Protocol to which all
TypeSyntax
nodes conform.protocol WithAttributesSyntax
protocol WithCodeBlockSyntax
protocol WithGenericParametersSyntax
Syntax nodes that have generic parameters.
protocol WithModifiersSyntax
protocol WithStatementsSyntax
protocol WithTrailingCommaSyntax
Extension in SwiftParser
Subtypes
Extension in SwiftSyntaxBuilder
Subtypes
protocol DeclSyntaxParseable
Adds an initializer that allows the creation of declaration from string interpolations.
Extension in LexicalPaths
Subtypes
Extension in MarkdownRendering
Subtypes
Extension in SemanticVersions
Subtypes
Extension in UnidocPages
Subtypes
Extension in Vapor
Subtypes
protocol DebuggableError
Debuggable
provides an interface that allows a type to be more easily debugged in the case of an error.