如何将类型为DateComponents的对象保存到CloudKit字段? [英] How would I save an object of type DateComponents to a CloudKit field?
问题描述
我正在为iOS应用程序使用Swift。
我需要将DateComponents对象存储为CloudKit中的字段。
我该怎么做?
到目前为止,我认为我需要将CloudKit字段设置为Bytes类型,并将DateComponents对象转换为Data对象。 。查看Data类的文档,我不知道如何初始化Data对象。我不知道如何使用初始化程序之一中使用的UnsafeBufferPointer。
以下代码给出了运行时错误:
newRecord.setObject((dateComponents as!__CKRecordObjCValue),forKey:DatabaseNameStrings.fieldTime)
以下是调试窗口中的运行时错误消息:
无法转换'NSDateComponents'类型的值(0x1d04208c8)到'__C.CKRecordValue'(0x1d0420ab0)。
我还需要将Data转换为DateComponents。 Data和DateComponents类均符合Codable协议,该协议继承了Decoder协议。 DateComponents的初始化程序之一是:
init(来自:解码器)
这使我怀疑在从类型为Bytes的CloudKit字段中获取Data对象之后,有一种方法可以使用该init将Data类型转换为DateComponents。 / p>
以下代码:
如果让数据= privateRecord.object( forKey:DatabaseNameStrings.fieldTime)为?数据{
let dateComponents = DateComponents(from:data)//错误在这里
}
在Xcode编辑器中生成错误,提示:
参数类型'Data'不符合预期的'Decoder'类型。
我以为,因为Data和DateComponents都符合可以工作的Decoder协议。
似乎您可能对此进行了过度设计。您可以考虑以下几种选择:
- 将日期存储在CloudKit的日期字段中,并重新解析应用程序中的日期组件
- 将每个日期成分(年,月,日等)存储为CloudKit上单独的整数字段(如果要对其进行预格式化,则存储为字符串)。
只是一个想法。祝好运。 :)
I am using Swift for an iOS app.
I need to store a DateComponents object as a field in CloudKit.
How would I do that?
So far I think I need to set the CloudKit field to type Bytes, and convert the DateComponents object to a Data object. Looking at the documentation for the Data class, I can't figure out how to initialize the Data object. I have no idea how to use the UnsafeBufferPointer used in one of the initializers.
The following code gives a runtime error:
newRecord.setObject((dateComponents as! __CKRecordObjCValue), forKey: DatabaseNameStrings.fieldTime)
Here's the runtime error message in the debug window:
Could not cast value of type 'NSDateComponents' (0x1d04208c8) to '__C.CKRecordValue' (0x1d0420ab0).
I also need to convert Data to DateComponents. Both Data and DateComponents classes conform to the Codable protocol, which inherits the Decoder protocol. One of DateComponents' initializers is:
init(from: Decoder)
which leads me to suspect there is a way to use that init to convert type Data to DateComponents after I get the Data object from the CloudKit field of type Bytes.
The following code:
if let data = privateRecord.object(forKey: DatabaseNameStrings.fieldTime) as? Data {
let dateComponents = DateComponents(from: data) // error here
}
generates an error in the Xcode editor which says:
Argument type 'Data' does not conform to expected type 'Decoder'
I thought since both Data and DateComponents conform to Decoder protocol that code would work.
It seems you might be over-engineering this a bit. Here are a couple alternatives you could consider:
- Store the date in a date field on CloudKit and re-parse to date components in your app.
- Store each date component (year, month, day, etc.) as separate integer fields on CloudKit (or as strings if you want to pre-format them).
Just a thought. Good luck. :)
这篇关于如何将类型为DateComponents的对象保存到CloudKit字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!