按 NSDate 对 NSManagedObjects 的 NSSet 进行排序会产生错误

发布于 2024-11-28 21:12:12 字数 299 浏览 1 评论 0原文

我正在尝试提取 RSS 提要并按 pubDate 排序。当我检查“更新”属性时,大多数情况下它是正确的并给我一个正确的日期,但是当我尝试从集合转换为排序数组时,我会从排序中获得随机结果。我已经追踪到这样一个事实:当 sort 进行比较时,属性(它是一个 NSDate,见图 1)会进入并作为 __nscfnumber 进行比较! (也是图2)

任何帮助或想法将不胜感激。

图1

figure1

图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

figure1

figure2

figure2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-12-05 21:12:12

我认为比较器块仅用于诊断目的?您实际上不需要为 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 a NSCFNumber instance is being assigned to to the updated attribute. The debugger ignores factors like a cast and instead simply asked the object what its class is. If the object says it is a NSCFNumber 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文