Spika-iOS:“不兼容的块指针类型”

时间:2014-09-26 02:41:17

标签: ios xcode nsarray

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

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;

非常感谢任何帮助。谢谢你们。

1 个答案:

答案 0 :(得分:2)

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

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

在您的情况下,排序块应该看起来像那样

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