A type with a customized textual representation.
SDK
- Xcode 7.0+
Framework
- Swift Standard Library
Declaration
Overview
Types that conform to the Custom
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 Custom
, the String(describing:)
initializer and the print(_:)
function use the instance’s custom description
property.
Accessing a type’s description
property directly or using Custom
as a generic constraint is discouraged.
Conforming to the CustomStringConvertible Protocol
Add Custom
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:
After implementing the description
property and declaring Custom
conformance, the Point
type provides its own custom representation.