<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/EncodableAttributedStringKey/encode(_:to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation28EncodableAttributedStringKeyP6encode_2toy5ValueQz_s7Encoder_ptKFZ"
  },
  "title" : "encode(_:to:)"
}
-->

# encode(_:to:)

Encodes a value to the provided encoder.

```
static func encode(_ value: Self.Value, to encoder: any Encoder) throws
```

## Parameters

`value`

The value to encode.

`encoder`

The encoder to write data to.

## Discussion

This method throws an error if writing to the encoder fails.

---

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)