直接使用访问器的核心数据 NSTimeInterval 有问题 [英] Core Data NSTimeInterval using an accessor directly is buggy

查看:11
本文介绍了直接使用访问器的核心数据 NSTimeInterval 有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NSManagedObject 子类中使用 setValueForKey 设置 NSTimeInterval,该值设置正确,并且在使用 valueForKey 检索时也是正确的,但是,如果直接使用访问器,它将返回不正确的值.这是一个演示问题的代码示例

I'm setting an NSTimeInterval using setValueForKey within an NSManagedObject Subclass, the value gets set correctly, and is also correct when it is retrieved using valueForKey, however, if an accessor is used directly, it returns an incorrect value. Here is a code sample that demonstrates the issue

let date = NSDate() //NSTimeIntervalSince1970 = 1447054145.15281
self.setValueForKey(date, "dateLastSynced")

self.valueForKey("dateLastSynced") //= 1447054145.15281
self.dateLastSynced // !!ERROR Incorrect value = 468746945.152815

奇怪的是,如果将 dateLastSynced 转换为 NSDate,则一切正常.

Strangely enough, if the dateLastSynced is converted to an NSDate, everything works perfectly.

对正在发生的事情有什么想法吗?

Any ideas on whats happening?

推荐答案

NSTimeInterval 类型的标量属性,用于 Core Data Date属性表示自参考日期以来的时间(以秒为单位)2001 年 1 月 1 日.核心数据透明地生成访问器方法在 NSTimeIntervalNSDate 之间转换.

A scalar property of type NSTimeInterval for a Core Data Date property represents the time in seconds since the reference date Jan 1, 2001. The Core Data generated accessor methods transparently convert between NSTimeInterval and NSDate.

因此您设置一个值,使用带有

Therefore you set a value using the scalar accessor with

obj.dateLastSynced = date.timeIntervalSinceReferenceDate

然后你检索

let date = NSDate(timeIntervalSinceReferenceDate: obj.dateLastSynced)

这给出了与键值编码方法相同的结果

This gives the same results as the Key-Value Coding methods

// Set:
obj.setValueForKey(date, "dateLastSynced")
// Get:
let date = obj.valueForKey("dateLastSynced")

这篇关于直接使用访问器的核心数据 NSTimeInterval 有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆