【发布时间】:2019-11-21 17:15:44
【问题描述】:
我正在使用 CloudKit 和 Swift for iOS。
我将 DateComponents 类型的对象保存在 Bytes 类型的 CloudKit 字段中。当我检索对象时,它的值与我最初保存的对象不同。
这是我将对象保存到 CloudKit 的代码:
let unsafePointer: UnsafePointer<DateComponents> = UnsafePointer<DateComponents>(&time)
let unsafeBufferPointer: UnsafeBufferPointer<DateComponents> = UnsafeBufferPointer<DateComponents>(start: unsafePointer, count: 1)
let data: Data = Data(buffer: unsafeBufferPointer)
privateRecord.setObject(data as CKRecordValue?, forKey: DatabaseNameStrings.fieldTime)
这是调试窗口中的打印结果:
time = calendar: gregorian (autoupdatingCurrent) timeZone: America/Chicago (autoupdatingCurrent) 小时: 12 分钟: 0 isLeapMonth: false
这是我从 CloudKit 中检索对象的代码:
if let dataTime = privateRecord.object(forKey: DatabaseNameStrings.fieldTime) as? Data {
let unsafeMutableBufferPointer: UnsafeMutableBufferPointer<DateComponents> = UnsafeMutableBufferPointer<DateComponents>.allocate(capacity: 1)
_ = dataTime.copyBytes(to: unsafeMutableBufferPointer)
print("unsafeMutableBufferPointer.first =", unsafeMutableBufferPointer.first as Any)
privateTime = unsafeMutableBufferPointer.first
}
这是调试窗口中的打印结果:
unsafeMutableBufferPointer.first = 可选(时代:0 年:0 月:0 日:0 小时:0 分钟:0 秒:0 纳秒:0 工作日:0 工作日序数:0 季度:0 周:0 周:0 年:0 年: 0 isLeapMonth: false )
【问题讨论】:
-
@CliftonLabrum 我需要做什么吗?
-
不是,仅供以后遇到这个问题的人参考。
标签: ios swift nsdata cloudkit nsdatecomponents