从Xcode 5.0移到5.1时,在gpuimage库中出现以下错误。在google上搜索之后,我发现我需要像这样发送int[nsnumber numberthint:number],但问题是我无法控制在下面给定的代码中使用comparator传递给sortedarrayingcomparator的值。
错误日志:
向“nscomparator”类型(也称为“nscomparisonresult(^)”(“nscomparisonid,”“nscomparator”类型)的参数发送“int(^)”的块指针类型不兼容
此行错误:nsarray*sortedpoints=[使用比较器的sortedarray点:^(id a,id b){

- (id) initWithCurveFile:(NSString*)curveFile
{
    self = [super init];
    if (self != nil)
    {
        NSString *bundleCurvePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: curveFile];

        NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath: bundleCurvePath];

        if (file == nil)
        {
            NSLog(@"Failed to open file");

            return self;
        }

        NSData *databuffer;

        // 2 bytes, Version ( = 1 or = 4)
        databuffer = [file readDataOfLength: 2];
        version = CFSwapInt16BigToHost(*(int*)([databuffer bytes]));

        // 2 bytes, Count of curves in the file.
        [file seekToFileOffset:2];
        databuffer = [file readDataOfLength:2];
        totalCurves = CFSwapInt16BigToHost(*(int*)([databuffer bytes]));

        NSMutableArray *curves = [NSMutableArray new];

        float pointRate = (1.0 / 255);
        // The following is the data for each curve specified by count above
        for (NSInteger x = 0; x<totalCurves; x++)
        {
            // 2 bytes, Count of points in the curve (short integer from 2...19)
            databuffer = [file readDataOfLength:2];
            short pointCount = CFSwapInt16BigToHost(*(int*)([databuffer bytes]));

            NSMutableArray *points = [NSMutableArray new];
            // point count * 4
            // Curve points. Each curve point is a pair of short integers where
            // the first number is the output value (vertical coordinate on the
            // Curves dialog graph) and the second is the input value. All coordinates have range 0 to 255.
            for (NSInteger y = 0; y<pointCount; y++)
            {
                databuffer = [file readDataOfLength:2];
                short y = CFSwapInt16BigToHost(*(int*)([databuffer bytes]));
                databuffer = [file readDataOfLength:2];
                short x = CFSwapInt16BigToHost(*(int*)([databuffer bytes]));

                [points addObject:[NSValue valueWithCGSize:CGSizeMake(x * pointRate, y * pointRate)]];
            }

            [curves addObject:points];
        }

        [file closeFile];

        rgbCompositeCurvePoints = [curves objectAtIndex:0];
        redCurvePoints = [curves objectAtIndex:1];
        greenCurvePoints = [curves objectAtIndex:2];
        blueCurvePoints = [curves objectAtIndex:3];
    }

    return self;

}


- (NSArray *)getPreparedSplineCurve:(NSArray *)points

{

    if (points && [points count] > 0)

    {

        // Sort the array.

        NSArray *sortedPoints = [points sortedArrayUsingComparator:^(id a, id b) {

            float x1 = [(NSValue *)a CGPointValue].x;

            float x2 = [(NSValue *)b CGPointValue].x;

            return x1 > x2;

        }];

        // Convert from (0, 1) to (0, 255).

        NSMutableArray *convertedPoints = [NSMutableArray arrayWithCapacity:[sortedPoints count]];

        for (int i=0; i<[points count]; i++){

            CGPoint point = [[sortedPoints objectAtIndex:i] CGPointValue];

            point.x = point.x * 255;

            point.y = point.y * 255;

            [convertedPoints addObject:[NSValue valueWithCGPoint:point]];

        }


        NSMutableArray *splinePoints = [self splineCurve:convertedPoints];

        // If we have a first point like (0.3, 0) we'll be missing some points at the beginning

        // that should be 0.

        CGPoint firstSplinePoint = [[splinePoints objectAtIndex:0] CGPointValue];

        if (firstSplinePoint.x > 0) {

            for (int i=0; i <=firstSplinePoint.x; i++) {

                CGPoint newCGPoint = CGPointMake(0, 0);

                [splinePoints insertObject:[NSValue valueWithCGPoint:newCGPoint] atIndex:0];

            }

        }


        // Prepare the spline points.

        NSMutableArray *preparedSplinePoints = [NSMutableArray arrayWithCapacity:[splinePoints count]];

        for (int i=0; i<[splinePoints count]; i++)

        {

            CGPoint newPoint = [[splinePoints objectAtIndex:i] CGPointValue];

            CGPoint origPoint = CGPointMake(newPoint.x, newPoint.x);

            float distance = sqrt(pow((origPoint.x - newPoint.x), 2.0) + pow((origPoint.y - newPoint.y), 2.0));

            if (origPoint.y > newPoint.y)

            {

                distance = -distance;

            }

            [preparedSplinePoints addObject:[NSNumber numberWithFloat:distance]];

        }

        return preparedSplinePoints;

    }

    return nil;
}

最佳答案

比较块必须返回NSComparisonResult,即
NSOrderedAscendingNSOrderedSameNSOrderedDescending(亦称-10+1),
取决于第一个元素是否小于、等于或大于第二个元素)。
返回x1 > x2不满足此要求,编译器会因为
它从最后一条语句“推断”返回类型为int
要解决这个问题,你可以
使用Is there a standard sign function (signum, sgn) in C/C++?中的技术返回-10+1,以及
显式声明块的返回类型为NSComparisonResult
让编译器高兴。
还可以通过将块参数声明为
NSValue *

NSArray *sortedPoints = [points sortedArrayUsingComparator:^NSComparisonResult(NSValue *a, NSValue *b) {
    float x1 = [a CGPointValue].x;
    float x2 = [b CGPointValue].x;
    return (x1 > x2) - (x2 > x1); // -1, 0, or +1
}];

关于objective-c - 不兼容的块指针类型将'int(^)(__ strong id,__strong id)发送给'NSComparator'类型的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22398455/

10-09 17:59