Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么iOS中的DateComponents对象不能正确地保存到CloudKit或从CloudKit中检索?_Ios_Swift_Nsdata_Cloudkit_Nsdatecomponents - Fatal编程技术网

为什么iOS中的DateComponents对象不能正确地保存到CloudKit或从CloudKit中检索?

为什么iOS中的DateComponents对象不能正确地保存到CloudKit或从CloudKit中检索?,ios,swift,nsdata,cloudkit,nsdatecomponents,Ios,Swift,Nsdata,Cloudkit,Nsdatecomponents,我正在使用CloudKit和Swift for iOS 我正在将DateComponents类型的对象保存在Bytes类型的CloudKit字段中。检索对象时,它的值与我最初保存的对象的值不同 下面是我将对象保存到CloudKit的代码: let unsafePointer: UnsafePointer<DateComponents> = UnsafePointer<DateComponents>(&time) let unsafeBufferPointer: U

我正在使用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)
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
}
let unsafePointer:unsafePointer=unsafePointer(&time)
让unsafeBufferPointer:unsafeBufferPointer=unsafeBufferPointer(开始:unsafePointer,计数:1)
let data:data=data(缓冲区:unsafeBufferPointer)
privateRecord.setObject(数据形式为CKRecordValue?,forKey:DatabaseNameString.fieldTime)
以下是调试窗口中的打印结果:

时间=日历:公历(自动更新当前)时区:美国/芝加哥(自动更新当前)小时:12分钟:0 isLeapMonth:false

下面是我从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)
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
}
如果让dataTime=privateRecord.object(forKey:DatabaseNameStrings.fieldTime)作为?资料{
让unsafeMutableBufferPointer:unsafeMutableBufferPointer=unsafeMutableBufferPointer.allocate(容量:1)
_=dataTime.copyBytes(到:unsafeMutableBufferPointer)
打印(“unsafemtablebufferpointer.first=,unsafemtablebufferpointer.first,如有)
privateTime=unsafemeutablebufferpointer.first
}
以下是调试窗口中的打印结果:

UnsafemeutableBufferPointer.first=可选(纪元:0年:0月:0天:0小时:0分钟:0秒:0纳秒:0工作日:0工作日序号:0季度:0工作月:0工作年:0年工作周:0 isLeapMonth:false)


试图持久化
DateComponents
实例是错误的方法;您无法看到对象的内部结构,该结构可能会随着Swift或iOS更新而更改

您的假设是,只需恢复表示其他实例的字节,就可以绕过
DateComponents
的初始化;这是一个错误的假设

DateComponents
实际上是免费桥接到
NSDateComponents
的底层实例的

调用
UnsafeBufferPointer(start:unsafePointer,count:1)
时,您正在访问用于访问基础
NSDateComponents
实例的Swift结构

当您随后尝试重新创建此结构时,该基础对象不存在,您将得到0


您应该保存重新创建日期组件(例如时区、小时等)所需的特定字段,或者保存一个
date
对象。

此对象可能重复:@CliftonLabrum我需要做什么吗?不,这只是将来遇到此问题的人的参考。