ProtocolSwift5.9.0
CustomDebugStringConvertible
A type with a customized textual representation suitable for debugging purposes.
protocol CustomDebugStringConvertible
Swift provides a default debugging textual representation for any type. That default representation is used by the String(reflecting:)
initializer and the debugPrint(_:)
function for types that don’t provide their own. To customize that representation, make your type conform to the CustomDebugStringConvertible
protocol.
Because the String(reflecting:)
initializer works for instances of any type, returning an instance’s debugDescription
if the value passed conforms to CustomDebugStringConvertible
, accessing a type’s debugDescription
property directly or using CustomDebugStringConvertible
as a generic constraint is discouraged.
Conforming to the CustomDebugStringConvertible Protocol
Add CustomDebugStringConvertible
conformance to your custom types by defining a debugDescription
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(String(reflecting: p))
// Prints "Point(x: 21, y: 30)"
After adding CustomDebugStringConvertible
conformance by implementing the debugDescription
property, Point
provides its own custom debugging representation.
extension Point: CustomDebugStringConvertible {
var debugDescription: String {
return "(\(x), \(y))"
}
}
print(String(reflecting: p))
// Prints "(21, 30)"
Requirements
var debugDescription: String
A textual representation of this instance, suitable for debugging.
Citizens in Swift
Subtypes
protocol CodingKey
A type that can be used as a key for encoding and decoding.
Available in Foundation
Subtypes
protocol ReferenceConvertible
Decorates types which are backed by a Foundation reference type.
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 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.