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

2023-12-23

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

错误日志:

不兼容的块指针类型发送 'int (^)(__strong id, _强 id)' 到类型 'NSComparator' 的参数(又名 'NSComparisonResult (^)(_强ID,__强ID)')

此行错误: NSArray *sortedPoints = [pointssortedArrayUsingComparator:^(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,即NSOrderedAscending, NSOrderedSame, or NSOrderedDescending(又名-1, 0, +1), 取决于第一个元素是小于、等于还是大于第二个元素)。

返回x1 > x2不满足这个要求,编译器会抱怨,因为 它将返回类型“推断”为int从最后的陈述。

要解决这个问题,您可以

  • 使用来自的技术C/C++ 中有标准的符号函数(signum、sgn)吗? https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c回来-1, 0, or +1, and
  • 将块的返回类型显式声明为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
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不兼容的块指针类型将“int (^)(__strong id, __strong id)”发送到“NSComparator”类型的参数 的相关文章

  • 左右并排放置两个 UILabels,而不知道左标签中文本的字符串长度

    在 iPhone fb 应用程序的照片选项卡中 对于每个表格视图单元格 他们都会放置相册标题 后跟相册中的图片数量 例如 第一张专辑 22 最后也是最后的 12 我认为有两个标签 一个用于标题 一个用于数字 因为数字实际上是不同的 UICo
  • 主队列上的dispatch_sync 与dispatch_async

    请耐心等待 这需要一些解释 我有一个类似于下面的函数 上下文 aProject 是一个名为 LPProject 的核心数据实体 其数组名为 memberFiles 其中包含另一个名为 LPFile 的核心数据实体的实例 每个 LPFile
  • 存储和检索多维 NSMutableArray 的最佳方法是什么?

    我将一堆数据存储在 plist 文件 在应用程序文档文件夹中 中 其结构如下 Dictionary description String Value sections Array Array Number Number Array Numb
  • 通过 CTFontRef 或 CGFontRef 对象中的字形索引获取 unicode 字符

    CTFontRef 提供了出色的方法 例如CTFontGetGlyphsForCharacters用于将字符映射到字形 我的问题是 有没有逆映射的方法 也就是说 我可以通过给定的字形获取字符吗 自从我发现有一个CTFontCopyChara
  • AVMutableComposition - 导出错误的视频转换

    导出 VideoAsset 后 问题 视频方向是不是原始变换 导出视频层似乎总是景观 尝试去 变换视频层方向 旋转至原始方向 视频层大小 使其全屏尺寸 按原始方向 一些注意事项 videoAsset 的 CGRect 从一开始就是相反的 a
  • 确定第三方应用程序在 iPhone 上播放的歌曲

    我正在尝试确定 iPhone 上当前正在播放的歌曲的标题 我知道如果本机 Apple 应用程序正在播放音乐 我可以使用以下代码来找出正在播放的内容 但如果从 Spotify 或其他音乐播放应用程序播放歌曲 我如何找到歌曲的标题 MPMedi
  • 一种简单、干净的方式来切换/交换视图?

    我已经看了几个来源 但我仍然很困惑 我想创建一个具有多个视图的应用程序 只有标准视图 没有表视图或其他任何内容 我可以在其中单击每个视图上的按钮来访问其他视图 我已经看到了多种方法来做到这一点 但对我来说唯一有意义的方法是让应用程序委托负责
  • 如何在操作表中添加日期选择器?

    IBAction showCatPicker if self catList nil self catList nil catList release self catList NSMutableArray alloc init self
  • 在 Objective-C iPad 开发中发布

    我正在尝试发出 POST 请求 但我似乎无法弄清楚出了什么问题 我从服务器收到响应 但我的电子邮件 密码对似乎没有正确发送 读取 由服务器 它告诉我不存在这样的帐户 这是我的代码 它包含在一个函数中 当用户按下我创建的 登录 按钮时调用该函
  • iOS 上读取证书问题

    我正在尝试从 iOS 中的各种 URL 读取证书 然而 我的代码运行不佳 应该返回我需要的信息的数组总是返回null 我缺少什么 void findCertificate NSString url NSInputStream input N
  • 动画导航控制器“后退”按钮

    我在导航控制器层次结构中的视图控制器上有一个自定义按钮 按下该按钮时 会弹出可见的视图控制器 我想使用 UIView 的transform属性来动画关闭视图控制器 它有效 但如果我使用 popViewControllerAnimated Y
  • iOS 中的视频可以进行反卷积吗?

    我想拍摄击球手挥动棒球的镜头 但球棒很模糊 视频为 30 fps 通过研究 我发现反卷积似乎是最小化运动模糊的方法 但我不知道是否或如何在我的 iOS 应用程序后处理中实现它 我希望有人能给我指出正确的方向 比如如何在 iOS 中应用反卷积
  • 文档 Main.storyboard 需要 Xcode 8.0 或更高版本

    我下载了 Xcode beta 并打开了现有的项目 看看它如何与 Xcode 8 beta 一起使用 我从 Xcode 8 打开了 Storyboard 文件 现在 当我从 Xcode 7 3 打开项目时 我无法打开故事板文件 它给出了以下
  • MPMoviePlayerViewController,删除 Quicktime 符号/添加背景图像?

    我有一个播放音频的 MPMoviePlayerViewController 我想删除 Quicktime 徽标和 或向播放器添加自定义背景图像 但保留播放控件 我发誓我在 iOS 5 之前就已经这样做过 但我无法重新弄清楚 我尝试过的事情
  • 如何在UIWindow中添加视图?

    我想添加一个视图UIWindow与以下代码 AppDelegate delegate AppDelegate UIApplication sharedApplication delegate UIWindow window delegate
  • 在 Objective-C 中比较两次的最好/最简单的方法是什么?

    我有一个时间的字符串表示形式 例如 11 13 AM 这是使用 NSDateFormatter 和 stringFromDate 方法生成的 我想将此时间与当前时间进行比较 但是当我使用 dateFromString 方法将字符串转回日期时
  • 如何命名一段代码并在不同的方法中调用它?

    我使用 Grand Central Dispatch 方法在队列中执行我的应用程序 我在该队列的计算中决定按钮的框架 我希望我的应用程序重新绘制其屏幕并计算旋转后的新帧 这是我所做的一些伪代码解释 CGFloat a 123 b 24 di
  • Xcode 4 Core Data:如何使用在数据模型编辑器中创建的获取属性

    如何在 Xcode 4 中实现获取的属性 Here is an example of two entities a book and a page 我按照此处的指南创建了一个获取的属性 该属性使用变量 FETCH SOURCE 引用来自源实
  • 从 iOS 中的 App Delegate 调用当前视图控制器中的方法

    我有两个视图控制器 BuildingsViewController 和 RoomsViewController 它们都使用应用程序委托中名为上传的函数 上传函数基本上执行一个 HTTP 请求 如果成功或不成功 都会触发 uialertvie
  • 如何为 Mac OS X 制作可拖动的菜单栏图标

    我正在为我正在开发的应用程序编写菜单栏图标 但是 NSStatusBar 类没有可以通过 cmd 鼠标左键拖动来使图标可拖动的方法 如何使用 Objective C 代码使菜单栏图标可拖动 谢谢 目前您无法使用 NSStatusBar 来完

随机推荐