Question

I have a piece of code which orders an NSMutableArray of points, as follows:

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

This works perfectly well in my first project. I have then tried to use this in another project, where I have basically copied my entire class across (for the purpose of splitting out into separate demo projects). In the second project, Xcode fails to build with the error:

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

Bizarrely, it will compile if I put the code in a different class within the new project, but never within my original class, 'Classname.mm'. The .mm is the same as in the original project, and all of the same headers and variables are included.

Both projects are compiled for iOS 7.0, on Xcode 5.0.1.

Does anyone have any idea why this would be happening in one class only in my new project?

Thanks

Was it helpful?

Solution

The block needs to return a value of type NSComparisonResult. You are not doing that.

Try:

[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;
     }
}];

I might have the "Ascending/Descending" values backward. If you get the results in the reverse order, swap those two return values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top