使用 NSComparisonResult 对 CGPoints 数组进行排序时,一个项目出现错误

wic*_*ets 0 objective-c nsmutablearray cgpoint ios

我有一段代码对 NSMutableArray 点进行排序,如下所示:

[points sortUsingComparator:^NSComparisonResult (id firstObject, id secondObject)
{
     CGPoint firstPoint = [firstObject CGPointValue];
     CGPoint secondPoint = [secondObject CGPointValue];
     return firstPoint.y>secondPoint.y;
}];
Run Code Online (Sandbox Code Playgroud)

这在我的第一个项目中效果非常好。然后我尝试在另一个项目中使用它,在那里我基本上复制了整个类(为了分成单独的演示项目)。在第二个项目中,Xcode 无法构建并出现以下错误:

无法使用“bool”类型的右值初始化“NSComparisonResult”类型的返回对象。

奇怪的是,如果我将代码放在新项目中的不同类中,而不是放在我的原始类“Classname.mm”中,它就会编译。.mm 与原始项目中的相同,并且包含所有相同的标头和变量。

这两个项目都是在 Xcode 5.0.1 上针对 iOS 7.0 编译的。

有谁知道为什么这种情况只会发生在我的新项目中的一堂课上?

谢谢

rma*_*ddy 5

该块需要返回类型为 的值NSComparisonResult。你没有那样做。

尝试:

[points sortUsingComparator:^NSComparisonResult (id firstObject, id secondObject)
{
     CGPoint firstPoint = [firstObject CGPointValue];
     CGPoint secondPoint = [secondObject CGPointValue];
     if (firstPoint.y > secondPoint.y) {
         return NSOrderedDescending;
     } else if (firstPoint.y < secondPoint.y) {
         return NSOrderedAscending;
     } else {
         return NSOrderedSame;
     }
}];
Run Code Online (Sandbox Code Playgroud)

我可能会将“升序/降序”值向后排列。如果以相反的顺序得到结果,则交换这两个返回值。