OStack程序员社区-中国程序员成长平台

标题: ios - 斯皮卡-iOS : "incompatible block pointer types" [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:54
标题: ios - 斯皮卡-iOS : "incompatible block pointer types"

我已经下载了适用于 iOS 和 Android 的“Spika”脚本,但遇到了以下问题。尝试编译时出现以下错误:

Incompatible block pointer types sending 'int (^)(ModelMessage *__strong, ModelMessage *__strong)' to parameter of type 'NSComparator' (aka 'NSComparisonResult (^)(__strong id, __strong id)')

我试图通过将其更改为 NSComparisonResult 来解决此问题,但似乎无法理解问题所在。

代码如下:

NSMutableArray *removeTarget = [[NSMutableArray alloc] init];

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

    ModelMessage *message = [result objectAtIndex:i];

    if(message.valid == NO)
        [removeTarget addObject:message];

}

for(int ii = 0; ii < [removeTarget count] ; ii++){
    [result removeObject:[removeTarget objectAtIndex:ii]];
}

NSArray *sortedAry = [result sortedArrayUsingComparator:^(ModelMessage *a, ModelMessage *b) {
    return a.modified> b.modified;
}];

int fetchNum = PagingMessageFetchNum;
if(fetchNum > sortedAry.count)
    fetchNum = sortedAry.count;

return sortedAry;

//return [sortedAry subarrayWithRange:NSMakeRange(0, fetchNum)];
}

问题出在:

NSArray *sortedAry = [result sortedArrayUsingComparator:^(ModelMessage *a, ModelMessage *b) {
    return a.modified> b.modified;

任何帮助将不胜感激。谢谢大家。



Best Answer-推荐答案


您不应该像上面的代码那样返回 BOOL 结果。 结果必须是 NSComparisonResult 类型,即枚举

enum {
  NSOrderedAscending = -1,
  NSOrderedSame,
  NSOrderedDescending
};

在你的情况下,排序 block 应该是这样的

NSArray *sortedAry = [result sortedArrayUsingComparator:^(ModelMessage *a, ModelMessage *b) {
    if(a.modified<b.modified)
        return NSOrderedAscending;
    else if(a.modified>b.modified)
        return NSOrderedDescending;
    else 
        return NSOrderedSame;
}];

关于ios - 斯皮卡-iOS : "incompatible block pointer types",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051244/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4