使用iPhone的排序描述符对核心数据位置进行排序 [英] Sorting core data position changes with sort descriptors for iPhone

查看:469
本文介绍了使用iPhone的排序描述符对核心数据位置进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个属性的CoreData实体。一个称为位置,另一个称为positionChange。它们都是整数,其中position属性是当前位置,positionChange是前一个位置和新位置之间的差值。这意味着positionChange可以是负数。

I have a CoreData entity with two attributes. One called 'position' the other called 'positionChange'. Both of them are integers where the position attribute is the current position and the positionChange is the difference between the previous position and the new position. This means that the positionChange can be negative.

现在我想按positionChange排序。但我希望它无视负面价值观。目前我正在将它降序排序,这将得到结果:2,1,0,-1,-2。
但我正在寻找的是得到这个结果:2,-2,1,-1,0。

Now I would like to sort by positionChange. But I would like it to disregard the negative values. Currently I'm sorting it descending, which will give the result: 2, 1, 0, -1, -2. But what I'm looking for is to get this result: 2, -2, 1, -1, 0.

关于如何解决的任何想法这使用排序描述符?

Any ideas on how to solve this using sort descriptors?

编辑

我有2类,一个叫做DataManager,另一个包含我的NSNumber类别(positionChange属于NSNumber类型)。

I got 2 classes, one called DataManager and the other containing my NSNumber category (positionChange is of type NSNumber).

在DataManager中我有一个名为'fetchData:'的方法我在哪里使用排序描述符执行我的获取请求:

In DataManager I have a method called 'fetchData:' where I'm executing my fetch request with a sort descriptor:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionChange" ascending:NO selector:@selector(comparePositionChange:)];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

我正在为请求做更多的事情,但这对此问题并不感兴趣。

I'm doing some more stuff to the request, but that's not interesting for this issue.

我的NSNumber类别应该与你发布的类似:
在.h:

My NSNumber category should be exactly like the one you posted: In the .h:

@interface NSNumber (AbsoluteValueSort)
- (NSComparisonResult)comparePositionChange:(NSNumber *)otherNumber;
@end

并且在.m:

@implementation NSNumber (AbsoluteValueSort)

- (NSComparisonResult)comparePositionChange:(NSNumber *)otherNumber
{
    return [[NSNumber numberWithFloat:fabs([self floatValue])] compare:[NSNumber numberWithFloat:fabs([otherNumber floatValue])]];
}

@end

当我在我的电话上调用fetchData时DataManager对象我收到此错误:

When I call fetchData on my DataManager object I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: comparePositionChange:'

可能是什么情况的想法?我在我的DataManager类中包含了我的NSNumber类头文件。

Any ideas of what might be the case? I have included my NSNumber category header file in my DataManager class.

推荐答案

试试这个,假设你的positionChange是一个NSNumber:

Try this, assuming your positionChange is an NSNumber:

@interface NSNumber (AbsoluteValueSort)

-(NSComparisonResult) comparePositionChange:(NSNumber *)otherNumber;

@end

后跟:

@implementation NSNumber (AbsoluteValueSort)

-(NSComparisonResult) comparePositionChange:(NSNumber *)otherNumber
{
    return [[NSNumber numberWithFloat: fabs([self floatValue])] compare:[NSNumber numberWithFloat: fabs([otherNumber floatValue])]];
}

@end

然后执行以下操作:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionChange" ascending:NO selector:@selector(comparePositionChange:)];

并使用该描述符排序。如果你想使-2总是在2之后,或者5总是在-5之后你可以修改comparePositionChange:在一个数字是另一个数字的负数的情况下返回NSOrderedAscending。

and sort with that descriptor. If you want to make it so that -2 is always after 2, or 5 is always after -5 you can modify comparePositionChange: to return NSOrderedAscending in the cases when one number is the negative of the other.

这篇关于使用iPhone的排序描述符对核心数据位置进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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