Generic Structure

CodableConfiguration

A property wrapper that makes a type codable, by supplying a configuration that provides additional information for serialization.

Declaration 宣言

@propertyWrapper struct CodableConfiguration<T, ConfigurationProvider> where T : DecodableWithConfiguration, T : EncodableWithConfiguration, ConfigurationProvider : DecodingConfigurationProviding, ConfigurationProvider : EncodingConfigurationProviding, T.DecodingConfiguration == ConfigurationProvider.DecodingConfiguration, T.EncodingConfiguration == ConfigurationProvider.EncodingConfiguration

Overview 概要

CodableConfiguration allows you to create Codable types whose members don’t all conform to Codable. For types that can’t support encoding and decoding by themselves but could become encodable and decodable with some statically-defined information, use the @CodableConfiguration wrapper. This lets you assign a configuration provider — a type that conforms to both EncodingConfigurationProviding and DecodingConfigurationProviding — to supply this data.

Limiting the CodableConfiguration to statically-defined information protects clients from loading unexpected data, similar to the protection provided by NSSecureCoding.

In the following example, the Message type uses @CodableConfiguration for an AttributedString property called content. While AttributedString does conform to Codable, it can only encode its known attributes — those declared by the platform SDK — as part of this conformance. By adding a CodableConfiguration for the custom MyAttributes type, Message uses encode(to:configuration:) when encoding content, which preserves the custom attributes.


struct Message: Codable {
    let date: Date
    let sender: Person
    @CodableConfiguration(from: MyAttributes.self) var content = AttributedString()
}

Topics 話題

Creating a Codable Configuration コード化可能構成設定を作成する

Accessing the Wrapped Value ラップされた値にアクセスする

Encoding and Decoding エンコーディングとデコーディング

Supporting Hashing ハッシュ化をサポートする

Comparing Configuration Instances 構成設定インスタンスを比較する

Relationships 関係

Conforms To 次に準拠

  • Decodable
  • Encodable
  • Equatable
    Conforms when T conforms to Equatable. TEquatableに準拠する時に準拠します。
  • Hashable
    Conforms when T conforms to Hashable. THashableに準拠する時に準拠します。

See Also 参照

Serializing Arbitrary Payloads 恣意的なペイロードをシリアル化する