insidegui / CloudKitCodable

An encoder and decoder for CKRecord
BSD 2-Clause "Simplified" License
387 stars 29 forks source link

Add support for encoding/decoding String and Int enum properties #12

Closed insidegui closed 5 months ago

insidegui commented 11 months ago

This introduces a couple of protocols, CloudKitStringEnum and CloudKitIntEnum.

When a model that conforms to CustomCloudKitCodable wants to encode/decode enum properties, the enums can adopt CloudKitStringEnum or CloudKitIntEnum and CoudKitRecordEncoder/Decoder will encode the values to the CKRecord as String and Int respectively.

Both enum protocols adopt CloudKitEnum, which has a single requirement:

public protocol CloudKitEnum {
    static var cloudKitFallbackCase: Self? { get }
}

This allows enums to provide a fallback value in case decoding from an unsupported value fails because of new cases being added, for example.

There's a default implementation for enums that conform to CaseIterable that uses the first case as the fallback value.