【问题标题】:Incompatible block pointer types sending 'int (^)(__strong id, __strong id)' to parameter of type 'NSComparator'不兼容的块指针类型将“int (^)(__strong id, __strong id)”发送到“NSComparator”类型的参数
【发布时间】:2014-03-14 07:15:00
【问题描述】:

从 xcode 5.0 移至 5.1 时,GPUImage 库中出现以下错误。在谷歌上搜索后,我发现我需要像这样发送 int [NSNumber numberWithInt:number] 但问题是我无法控制在下面给定代码中传递给 sortedArrayUsingComparator 的值。

错误日志:

不兼容的块指针类型将“int (^)(__strong id, _strong id)”发送到“NSComparator”类型的参数(又名“NSComparisonResult (^)(_strong id, __strong id) ')

这一行出错:NSArray *sortedPoints = [points sortedArrayUsingComparator:^(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;
}

【问题讨论】:

  • 显示points的结构。
  • 编辑问题,添加完整功能
  • points 将引用这些数组 redCurvePoints、greenCurvePoints、blueCurvePoints
  • 我认为你因为NSValue 而被卡住了,你用它来装箱 NSPoint 将它存储在数组中。而是创建您自己的类 MyPoint 作为使用。你所有的问题都会迎刃而解。

标签: objective-c gpuimage xcode5.1


【解决方案1】:

比较块必须返回一个NSComparisonResult,即 NSOrderedAscendingNSOrderedSameNSOrderedDescending(又名-10+1), 取决于第一个元素是小于、等于还是大于第二个元素)。

返回x1 &gt; x2 不满足这个要求,编译器报错是因为 它从最后一条语句“推断”返回类型为int

要解决这个问题,你可以

您还可以通过将块参数声明为 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
}];

【讨论】:

  • 另外,NSComparisonResult 不是布尔值。您不能使用运算符
  • @StevenKramer:你完全正确,我忽略了那部分,感谢您的更正。
  • 三值比较 (-1,0,1) 的技巧很巧妙。是否可以保证“true”提升为“1”?
  • @StevenKramer:在 C 中,比较表达式的结果是 0 或 1,这在 C 标准中的某处定义。
  • 是的。我能找到的最佳参考在 C 常见问题解答中:c-faq.com/bool/bool2.html
【解决方案2】:

您对 CFSwapInt16BigToHost 的调用是错误的。您是否想到将 32 位 int 传递给将 16 位中的字节交换为的函数是不对的?从仅包含两个字节的 NSData 对象中读取 4 个字节是不对的?

您收到一条错误消息。这对我来说似乎很清楚。它告诉您期望哪个参数,以及您给了哪个参数。当您收到错误消息时,不要让您的大脑陷入糊涂,然后想“哦,我的上帝,代码精灵对我不利,我需要一些我不知道的魔法”。你需要思考:“我的代码有错误。是哪个错误?”在这种情况下,很明显。错误信息告诉你。您需要一个返回 NSComparisonResult 的块。

下一步应该是寻找 NSComparisonResult 的定义。在那里你会发现你应该返回三个可能值之一,而你的函数只返回两个可能值之一。所以如果你只是让它编译,它不会正确排序。

您可以使用代码完成,它会为具有所有正确类型的块填充骨架,因此您一开始就没有机会犯错。您还应该找到 Apple 的块文档,并了解如何确定返回类型。阅读文档是学习和成为更好的程序员的好方法。

【讨论】:

    【解决方案3】:

    我遇到了同样的错误。解决方案正如其他人所建议的那样,我试图以一种简单的方式来说明:
    从以下位置更改块:
    ^(id a, id b)
    至:
    ^NSComparisonResult(id a, id b)
    然后你就准备好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多