protocol LosslessStringConvertible
Inheritance |
CustomStringConvertible
View Protocol Hierarchy →
|
---|---|
Import | import Swift |
Initializers
Instantiates an instance of the conforming type from a string representation.
Declaration
init?(_ description: String)
Instance Variables
A textual representation of this instance.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the String(describing:)
initializer. This initializer works with any type, and uses the custom
description
property for types that conform to
CustomStringConvertible
:
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
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.