プロトコル


CustomStringConvertible


カスタマイズされたテキスト表現を持つ型。


iOS 8.0+ iPadOS 8.0+ Mac Catalyst 13.0+ macOS 10.10+

tvOS 9.0+ VisionOS 1.0+ WatchOS 2.0+


protocol CustomStringConvertible






概観


CustomStringConvertible プロトコルに準拠する型は、インスタンスを文字列に変換するときに使用する独自の表現を提供できます。String(describing:) イニシャライザは、任意の型のインスタンスを文字列に変換する場合に推奨される方法です。渡されたインスタンスが CustomStringConvertible に準拠している場合、String(describing:) イニシャライザと print(_:) 関数はインスタンスのカスタム description プロパティを使用します。


型の descriotion プロパティに直接アクセスしたり、CustomStringConvertible を汎用制約として使用したりすることは推奨されません。


CustomStringConvertible プロトコルに準拠


description プロパティを定義して、カスタム型に CustomStringConvertible 準拠を追加します。


たとえば、このカスタム Point 構造体は、標準ライブラリによって提供されるデフォルトの表現を使用します。


  1. struct Point {
  2. let x: Int, y: Int
  3. }
  4. let p = Point(x: 21, y: 30)
  5. print(p)
  6. // Prints "Point(x: 21, y: 30)"


description プロパティを実装し、CustomStringConvertible 準拠を宣言すると、Point 型は独自のカスタム表現を提供します。


  1. extension Point: CustomStringConvertible {
  2. var description: String {
  3. return "(\(x), \(y))"
  4. }
  5. }
  6. print(p)
  7. // Prints "(21, 30)"




トピックス


インスタンスプロパティ


var description: String

このインスタンスのテキスト表現。

必須 デフォルトの実装が提供されます。





関連


以下による継承


BinaryInteger

CodingKey

FixedWidthInteger

LosslessStringConvertible

SIMD

SignedInteger

StringProtocol

UnsignedInteger



準拠する型


AnyHashable

Array

ElementCopyable かつ Escapable に準拠している場合に準拠します。


ArraySlice

ElementCopyable かつ Escapable に準拠している場合に準拠します。


AtomicLoadOrdering

AtomicStoreOrdering

AtomicUpdateOrdering

Bool

Character

ClosedRange

BoundComparable に準拠する場合に準拠します。


ContiguousArray

ElementCopyable かつ Escapable に準拠している場合に準拠します。


DefaultStringInterpolation

Dictionary

KeyHashable に準拠し、ValueCopyable に準拠し、ValueEscapable に準拠する場合に準拠します。


Dictionary.Keys

KeyHashable に準拠し、ValueCopyable に準拠し、ValueEscapable に準拠する場合に準拠します。


Dictionary.Values

KeyHashable に準拠し、ValueCopyable に準拠し、ValueEscapable に準拠する場合に準拠します。


DiscontiguousSlice

BaseCollection に準拠する場合に準拠します。


DiscontiguousSlice.Index

BaseCollection に準拠する場合に準拠します。


Double

Duration

Float

Float16

Float80

Int

Int128

Int16

Int32

Int64

Int8

KeyValuePairs

KeyCopyable に準拠し、KeyEscapable に準拠し、ValueCopyable に準拠し、ValueEscapable に準拠する場合に準拠します。


Mirror

Range

BoundComparable に準拠する場合に準拠します。


RangeSet

BoundComparable に準拠する場合に準拠します。


RangeSet.Ranges

BoundComparable に準拠する場合に準拠します。


RemoteCallTarget

SIMD16

SIMD2

SIMD3

SIMD32

SIMD4

SIMD64

SIMD8

SIMDMask

Set

ElementHashable に準拠する場合に準拠します。


StaticBigInt

StaticString

String

String.Encoding

String.UTF16View

String.UTF8View

String.UnicodeScalarView

Substring

TaskLocal

TaskPriority

UInt

UInt128

UInt16

UInt32

UInt64

UInt8

Unicode.Scalar

UnownedJob

WordPair





以下も見よ


文字列表現


protocol LosslessStringConvertible

ロスのない、かつ明確な方法で文字列として表現できる型。


protocol CustomDebugStringConvertible

デバッグ目的に適した、カスタマイズされたテキスト表現を持つ型。














トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ