This project implements CloudKitRecordEncoder
and CloudKitRecordDecoder
, allowing for custom data types to be converted to/from CKRecord
automatically.
For details on how to use CloudKitCodable, please check the included documentation.
Declaring a model that can be encoded as a CKRecord
:
struct Person: CustomCloudKitCodable {
var cloudKitSystemFields: Data?
let name: String
let age: Int
let website: URL
let avatar: URL
let isDeveloper: Bool
}
Creating a CKRecord
from a CustomCloudKitCodable
type:
let rambo = Person(...)
do {
let record = try CloudKitRecordEncoder().encode(rambo)
// record is now a CKRecord you can upload to CloudKit
} catch {
// something went wrong
}
Decoding a CustomCloudKitCodable
type from a CKRecord
:
let record = // record obtained from CloudKit
do {
let person = try CloudKitRecordDecoder().decode(Person.self, from: record)
} catch {
// something went wrong
}
- iOS 14
- tvOS 14
- watchOS 5
- macOS 11
- Xcode 15 (recommended)
Add CloudKitCodable to your Package.swift
:
dependencies: [
.package(url: "https://github.com/insidegui/CloudKitCodable.git", from: "0.3.0")
]
If you prefer not to use Swift Package Manager, you can integrate CloudKitCodable into your project manually by copying the files in.