protocol
LosslessStringConvertible
Inheritance |
CustomStringConvertible
View Protocol Hierarchy →
|
---|---|
Import |
|
Initializers
Instantiates an instance of the conforming type from a string representation.
Declaration
init
?(
_
description
:
String
)
Instance Variables
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
)
(
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
1 inherited item hidden. (Show all)
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.