I tried to write a wrapper around CKRecord
so that I can use CodingKey
enums instead of string literals when referring to record fields. I get casting issues that don't make sense.
public struct RecordView<K: CodingKey> {
let r: CKRecord
// public subscript(_ key: K) -> CKRecordValueProtocol? {
public subscript(_ key: K) -> CKRecordValue? {
get { r[key.stringValue] }
set { r[key.stringValue] = newValue }
}
}
With that, I get a compiler error trying to assign to it:
view[.isActive] = true
// error: Cannot assign value of type 'Bool' to subscript of type 'CKRecordValue?'
// (aka 'Optional<__CKRecordObjCValue>')
But it works for a raw CKRecord
, which appears to have the exact same type for the subscript (CKRecordValue?
, aka __CKRecordObjCValue?
)
rec["isActive"] = true
If I go with other type for the subscript (CKRecordValueProtocol
) then I get runtime errors trying to read the bool.
view[.isActive] as! Bool
// runtime error: Could not cast value of type 'Swift.Int64' (0x7fff89306878)
// to 'Swift.Bool' (0x7fff89301230)
Is it possible to write something in Swift that behaves like the CKRecord, or is it using some Objective-C magic that I can't replicate in my wrapper subscript?