ProtocolSwift5.9.0

    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

    Citizens in Swift

    Subtypes

    Available in _RegexParser

    Members

    Available in Foundation

    Subtypes

    Extension in GRPC

    Subtypes

    Extension in Atomics

    Subtypes

    • protocol AtomicInteger

      A type that supports atomic integer operations through a separate atomic storage representation.

    Extension in Crypto

    Subtypes

    Extension in Testing

    Subtypes

    Extension in TraceableErrors

    Subtypes

    Extension in Durations

    Subtypes

    Extension in SwiftSyntax

    Subtypes

    Extension in SwiftParser

    Subtypes

    Extension in SwiftSyntaxBuilder

    Subtypes

    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.