LosslessStringConvertible

protocol LosslessStringConvertible

A type that can be represented as a string in a lossless, unambiguous way.

For example, the integer value 1050 can be represented in its entirety as the string "1050".

The description property of a conforming type must be a value-preserving representation of the original value. As such, it should be possible to re-create an instance from its string representation.

Inheritance CustomStringConvertible View Protocol Hierarchy →
Import import Swift

Initializers

init?(_:) Required

Instantiates an instance of the conforming type from a string representation.

Declaration

init?(_ description: String)

Instance Variables

var description: String Required

A textual representation of this instance.

Instead of accessing this property directly, convert an instance of any type to a string by using the String(describing:) initializer. For example:

struct Point: CustomStringConvertible {
    let x: Int, y: Int

    var description: String {
        return "(\(x), \(y))"
    }
}

let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints "(21, 30)"

The conversion of p to a string in the assignment to s uses the Point type's description property.

Declaration

var description: String { get }

Declared In

CustomStringConvertible