<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/String/init(reflecting:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SS10reflectingSSx_tclufc"
  },
  "title" : "init(reflecting:)"
}
-->

# init(reflecting:)

Creates a string with a detailed representation of the given value,
suitable for debugging.

```
init<Subject>(reflecting subject: Subject)
```

## Discussion

Use this initializer to convert an instance of any type to its custom
debugging representation. The initializer creates the string
representation of `instance` in one of the following ways, depending on
its protocol conformance:

- If `subject` conforms to the `CustomDebugStringConvertible` protocol,
  the result is `subject.debugDescription`.
- If `subject` conforms to the `CustomStringConvertible` protocol, the
  result is `subject.description`.
- If `subject` conforms to the `TextOutputStreamable` protocol, the
  result is obtained by calling `subject.write(to: s)` on an empty
  string `s`.
- An unspecified result is supplied automatically by the Swift standard
  library.

For example, this custom `Point` struct uses the default representation
supplied by the standard library.

```
struct Point {
    let x: Int, y: Int
}

let p = Point(x: 21, y: 30)
print(String(reflecting: p))
// Prints "p: Point = {
//           x = 21
//           y = 30
//         }"
```

After adding `CustomDebugStringConvertible` conformance by implementing
the `debugDescription` property, `Point` provides its own custom
debugging representation.

```
extension Point: CustomDebugStringConvertible {
    var debugDescription: String {
        return "Point(x: \(x), y: \(y))"
    }
}

print(String(reflecting: p))
// Prints "Point(x: 21, y: 30)"
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)