使用NSComparisonResult排序CGPoints数组时,一个项目出错

时间:2013-11-24 21:45:28

标签: ios objective-c nsmutablearray cgpoint

我有一段代码可以命令一个NSMutableArray点,如下所示:

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

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

  

无法初始化类型' NSComparisonResult'的返回对象与   类型' bool'的右值。

奇怪的是,如果我将代码放在新项目中的其他类中,它将编译,但从不在我原来的类中,' Classname.mm'。 .mm与原始项目中的相同,并且包含所有相同的标题和变量。

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

有没有人知道为什么只在我的新项目中才会在一个班级中发生这种情况?

由于

1 个答案:

答案 0 :(得分:4)

块需要返回类型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;
     }
}];

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