在NSSortDescriptor中使用KVC [英] Using KVC in NSSortDescriptor

查看:213
本文介绍了在NSSortDescriptor中使用KVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据存储在 NSString 中的整数对一堆对象进行排序。



我知道这是一个解决方案,它的工作原理:

  NSSortDescriptor * mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@from :$($ 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]];

我的问题是,为什么我不能使用KVO这个,看起来更干净, this:

  NSSortDescriptor * mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@from.integerValueascending:YES]; 

然后传递给我的 NSFetchRequest / p>

使用此代码,在21之前出现200。

解决方案

(基于SQLite的)核心数据获取请求的描述符只能使用持久属性和一组有限的内置选择器,但不能使用像
integerValue 。



在这种情况下,似乎 integerValue 被忽略,因此
排序为字符串,而不是数字


$ b b

如果不能将属性类型更改为Integer(也可以解决问题)
那么您可以使用特殊的选择器作为解决方法:

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

localizedStandardCompare 并根据数字值对包含数字
的字符串进行排序。


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.

解决方案

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.

这篇关于在NSSortDescriptor中使用KVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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