开发者

Sorting a NSSet of NSManagedObjects by a NSDate yields error

开发者 https://www.devze.com 2023-03-26 21:34 出处:网络
I am trying to pull in a RSS fee开发者_C百科d 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 se

I am trying to pull in a RSS fee开发者_C百科d 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

Sorting a NSSet of NSManagedObjects by a NSDate yields error

figure2

Sorting a NSSet of NSManagedObjects by a NSDate yields error


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消