gpt4 book ai didi

ios - 使用 NSComparisonResult 对 CGPoints 数组进行排序时在一个项目中出错

转载 作者:行者123 更新时间:2023-11-28 19:06:19 26 4
gpt4 key购买 nike

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

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

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

Cannot initialize return object of type 'NSComparisonResult' with an rvalue of type 'bool'.

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

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

有谁知道为什么这只会在我的新项目的一个类(class)中发生?

谢谢

最佳答案

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

我可能会将“升序/降序”值倒过来。如果您以相反的顺序获得结果,请交换这两个返回值。

关于ios - 使用 NSComparisonResult 对 CGPoints 数组进行排序时在一个项目中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com