调用sortedArrayUsingComparator时,编译器会抱怨类型不匹配

时间:2012-12-17 08:31:21

标签: objective-c objective-c-blocks

static NSArray *topologicallySortedReadableTypes;
static dispatch_once_t pred;

dispatch_once(&pred, ^{
    topologicallySortedReadableTypes = [self readableTypes];
    topologicallySortedReadableTypes = [topologicallySortedReadableTypes sortedArrayUsingComparator:^(id type1, id type2) {
        if (type1 == type2) return (NSComparisonResult)NSOrderedSame;
        if (UTTypeConformsTo((CFStringRef)type1, (CFStringRef)type2)) return (NSComparisonResult)NSOrderedAscending;
        if (UTTypeConformsTo((CFStringRef)type2, (CFStringRef)type1)) return (NSComparisonResult)NSOrderedDescending;
        // the problem is right here
        return ((NSUInteger)type1 < (NSUInteger)type2) ? (NSComparisonResult)NSOrderedAscending : (NSComparisonResult)NSOrderedDescending;
    }];
});

这是苹果提供的textedit最新版本的源代码,适用于10.7。因为苹果没有给出10.8的来源,所以它应该在10.8下正常工作。但是它给出了一个错误, 返回类型&#39; NSInteger&#39; (又名&#39; long&#39;)必须匹配之前的返回类型&#39; NSComparisonResult&#39; (又名&#39; enum NSComparisonResult&#39;)当block literal有未指定的返回类型时。

1 个答案:

答案 0 :(得分:3)

我猜编译器现在对块返回类型更加严格/困惑。此related question建议将显式返回类型添加到比较块:

topologicallySortedReadableTypes = [topologicallySortedReadableTypes
    sortedArrayUsingComparator:^NSComparisonResult(id type1, id type2) {…}];