按 NSDate 对 NSManagedObjects 的 NSSet 进行排序会产生错误
我正在尝试提取 RSS 提要并按 pubDate 排序。当我检查“更新”属性时,大多数情况下它是正确的并给我一个正确的日期,但是当我尝试从集合转换为排序数组时,我会从排序中获得随机结果。我已经追踪到这样一个事实:当 sort 进行比较时,属性(它是一个 NSDate,见图 1)会进入并作为 __nscfnumber 进行比较! (也是图2)
任何帮助或想法将不胜感激。
图1
图2
I am trying to pull in a RSS feed and sort by pubDate. When I examine the 'updated' property, most of the time it is correct and give me a proper date but when I try to convert from a set to a sorted array, I get random results from the sort. I've tracked this down to the fact that when sort is doing it's comparesion, the property (which is an NSDate, see figure1) is coming in and being compared as a __nscfnumber! (also figure2)
Any help or idea would be much appreciated.
figure1
figure2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为比较器块仅用于诊断目的?您实际上不需要为 NSDate 或任何提供的属性类型类提供比较器。
如果调试器报告
date1
对象属于 NSNumber-cluster 类类型,则某个NSCFNumber
实例将被分配给updated
属性。调试器忽略诸如强制转换之类的因素,而只是询问对象它的类是什么。如果对象说它是 NSCFNumber,那么它就是 NSCFNumber,无论代码如何处理它。为什么会发生这种情况,根据提供的代码我不能说。
在尝试排序之前,您可以尝试记录
updated
属性的值和类,以查看其报告是否正确。我还建议分解整条线。嵌套所有这些方法调用当然可以工作,但它很容易出错并且难以调试。I assume the comparator block is just for diagnostic purposes? You don't actually need to supply a comparator for NSDate or any of the provided attribute type classes.
If the debugger is reporting that the
date1
object is of a NSNumber-cluster class type, then somewhere aNSCFNumber
instance is being assigned to to theupdated
attribute. The debugger ignores factors like a cast and instead simply asked the object what its class is. If the object says it is aNSCFNumber
then it is, regardless of how the code treats it otherwise.Why that happens, I can't say based on the code provided.
You might try logging the value and class of the
updated
attribute before you attempt the sort to see if it reports properly. I would also recommend decomposing the entire line. Nesting all those method calls will work of course but it is error prone and hard to debug.