Using KVC in NSSortDescriptor

喜你入骨 提交于 2019-12-11 18:09:38

问题


I need to sort a bunch of objects based on an integer that is stored in an NSString.

I know this is one solution and it works:

NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from" ascending: YES comparator:^(id obj1, id obj2)
{
   if ([obj1 integerValue] > [obj2 integerValue])
   {
       return (NSComparisonResult) NSOrderedDescending;
   }

   if ([obj1 integerValue] < [obj2 integerValue])
   {
       return (NSComparisonResult) NSOrderedAscending;
   }

   return (NSComparisonResult) NSOrderedSame;
}];

self.myObjects = [[self.data allObjects] sortedArrayUsingDescriptors: @[mySortDescriptor]];

My question is, why can't I use KVO for this, it looks much cleaner, eg like this:

NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from.integerValue" ascending: YES];

And then pass that to my NSFetchRequest

With this code, 200 appears before 21.


回答1:


A sort descriptor for a (SQLite based) Core Data fetch request can only use persistent attributes and a limited set of built-in selectors, but not Objective-C methods like integerValue.

In this case, it seems that the integerValue is just ignored, so that the from values are sorted as strings, and not as numbers.

If you cannot change the attribute type to "Integer" (which would solve the problem as well) then you can use a special selector as a workaround:

NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"from"
                                  ascending:YES
                                   selector:@selector(localizedStandardCompare:)];

localizedStandardCompare is the "Finder-like sort" and sorts strings containing digits according to their numerical value.



来源:https://stackoverflow.com/questions/19197855/using-kvc-in-nssortdescriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!