NSComparator出错:初始化不兼容的块指针类型

时间:2013-08-02 10:16:22

标签: objective-c sorting compiler-errors objective-c-blocks comparator

执行此NSComparator:

NSComparator comparatore = ^NSComparisonResult(NSMutableDictionary *aDictionary, NSMutableDictionary *anotherDictionary) {
            return [[aDictionary objectForKey:@"Item"] localizedCaseInsensitiveCompare:[anotherDictionary objectForKey:@"Item"]];
    };
lista = [listaNonOrdinata sortedArrayUsingComparator:comparatore];

我收到此错误: 不兼容的块指针类型初始化'int(^)(struct NSMutableDictionary *,struct NSMutableDictionary *)',期望'NSComparator'

我在本网站和官方指南上看到过这个错误,但我还没有找到解决方案。

我已经尝试了一切,也许有人可以帮助我,或者也许有人知道如何以另一种方式做同样的事情。 谢谢!

1 个答案:

答案 0 :(得分:4)

NSComparator是预期两个id的块。您需要将id作为参数类型,并在必要时在块内部进行转换(在这种情况下,没有必要):

NSComparator comparatore = ^NSComparisonResult(id aDictionary, id anotherDictionary) {
    return [[aDictionary objectForKey:@"Item"] localizedCaseInsensitiveCompare:[anotherDictionary objectForKey:@"Item"]];
};
lista = [listaNonOrdinata sortedArrayUsingComparator:comparatore];