Swiftpack.co - wiresoft/CloudCoder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by wiresoft.
wiresoft/CloudCoder 0.2.1
Custom Encoder and Decoder for encoding to/from CKRecord for CloudKit sync.
⭐️ 2
🕓 3 years ago
iOS macOS
.package(url: "https://github.com/wiresoft/CloudCoder.git", from: "0.2.1")

CloudCoder

Codable support for Encoding and Decoding to CKRecords for storage in CloudKit, with some caveats:

  • Unkeyed containers are stored as arrays within the CKRecord
  • As such: unkeyed containers (used for Array, Set, etc...) can only store homogeneous primitive types.
  • Be careful using Dictionary because the dictionary keys are used to build keys within the CKRecord, so any keys you use must be part of your schema.
  • CloudCoder will store nested complex types in Keyed containers (used for struct, class, dictionary, etc...) as snake_case keys beneath the parent type.
  • The top-level item being encoded must conform to Identifiable where ID: CustomStringConvertible from which the CKRecord.ID will be derived.

In this example, the CKRecord will have keys: "id", "name", "sub_name", "sub_someValue"

struct Element: Codable, Identifiable {
    var id = UUID
    var name = "My Name"
    var sub: SubElement()
}

struct SubElement: Codable {
    var name = "Other Name"
    var someValue = 42
}

let value = Element()

// Encode
let encoder = CloudRecordEncoder()
let record = try encoder.encode(value)

// Decode
let decoder = CloudRecordDecoder()
let decodedValue = try decoder.decode(Element.self, from: record)

GitHub

link
Stars: 2
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics