通过选择器使用自定义比较器的NSSortDescriptor问题 [英] Problem with NSSortDescriptor using custom comparator via selector

查看:66
本文介绍了通过选择器使用自定义比较器的NSSortDescriptor问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有自定义比较器的sortdescriptor

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:@"object.dateTime" 
    ascending:YES 
    selector:@selector(compareObject:toObject:)];

(密钥是ManagedObject)

比较器方法:

- (NSComparisonResult)compareObject:(id)date1 toObject:(id)date2 {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

但是,我得到一个错误: 'NSInvalidArgumentException',原因:'-[__ NSDate compareObject:toObject:]:无法识别的选择器已发送.....

我做错了什么? 如果我在一个块中使用比较器,则它可以工作,但是我需要它通过选择器来工作. 我找不到任何示例代码或有关如何通过选择器使用比较器的清晰文档(对于iOS 3.x.x).该文档讨论了与自我比较,但是我尝试将compare方法合并到对象中,但这也不起作用.

谁能指出我的问题或有关如何通过选择器使用它的示例代码?

注意:比较器本身不是简单的日期比较.还有更多的事情正在进行.

解决方案

如果要比较NSDate对象,则传递的选择器必须是NSDate类的方法,并且只能接受一个参数.

来自NSSortDescriptor文档:

选择器必须指定一个方法 由...的值来实现 由keyPath标识的属性.这 用于比较的选择器是 传递了一个参数.

要提供自己的排序选择器,应在NSDate上定义一个类别,然后在此处放置自定义排序方法,例如

- (NSComparisonResult)customCompare:(id)toDate {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

OR (如果您不关心iOS版本)< 4.0,您也可以使用

- (id)initWithKey:(NSString *)key ascending:(BOOL)ascending comparator:(NSComparator)cmptr

cmptr是这样的一个块:

^(id date1, id date2) {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

I want to use a sortdescriptor with a custom comparator

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:@"object.dateTime" 
    ascending:YES 
    selector:@selector(compareObject:toObject:)];

(the key is a ManagedObject)

Comparator method:

- (NSComparisonResult)compareObject:(id)date1 toObject:(id)date2 {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

However, I get an error: 'NSInvalidArgumentException ', reason: '-[__NSDate compareObject:toObject:]: unrecognized selector sent .....

What am I doing wrong? The comparator works if I use it in a block, but I need it to work via a selector. I can't find any example code or clear documentation on how to use comparators via a selector (for iOS 3.x.x). The documentation talks about comparing to self, but I've tried to incorporate the compare method in object, but that didn't work either.

Who can point me to my problem or to some example code as to how to use this via a selector?

Note: The comparator itself is not a simple date comparison. There is a lot more going on in there.

解决方案

If you're comparing NSDate objects, the selector you pass has to be a method of the NSDate class and take only one argument.

From the NSSortDescriptor doc:

The selector must specify a method implemented by the value of the property identified by keyPath. The selector used for the comparison is passed a single parameter.

To provide your own sort selector you should define a category on NSDate and place your custom sort method here like

- (NSComparisonResult)customCompare:(id)toDate {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

OR If you don't care about iOS versions < 4.0, you could also use

- (id)initWithKey:(NSString *)key ascending:(BOOL)ascending comparator:(NSComparator)cmptr

cmptr being a block like this:

^(id date1, id date2) {
    NSComparisonResult comparisonResult;
    // Complex comparator contents
    return comparisonResult;
}

这篇关于通过选择器使用自定义比较器的NSSortDescriptor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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