Contents

init(exporting:importing:)

Creates a value representation that supports bidirectional conversion between an entity and a system intent value.

Declaration

init(exporting: @escaping  @Sendable (Item) async throws -> IntentValue, importing: @escaping  @Sendable (IntentValue) async throws -> Item)

Parameters

  • exporting:

    A closure that converts an entity to a system intent value.

  • importing:

    A closure that converts a system intent value back to an entity.

Example

struct LocationEntity: AppEntity, Transferable {
    static var transferRepresentation: some TransferRepresentation {
        IntentValueRepresentation(
            exporting: { entity in
                PlaceDescriptor(
                    representations: [
                        .coordinate(.init(
                            latitude: entity.latitude,
                            longitude: entity.longitude
                        ))
                    ],
                    commonName: entity.name
                )
            },
            importing: { place in
                guard let coordinate = place.coordinate else {
                    throw ImportError.missingCoordinate
                }
                return LocationEntity(
                    name: place.commonName ?? "Unknown Location",
                    latitude: coordinate.latitude,
                    longitude: coordinate.longitude
                )
            }
        )
    }
}