Extension of a generic Objective-C class cannot ac

2019-07-11 21:11发布

I have the following NSFetchedResultsController extension:

extension NSFetchedResultsController
{
    func tryObjectAtIndex(_ indexPath: IndexPath) throws -> AnyObject // ERROR HERE
    {
        do
        {
            try ObjC.catchException
            {
                return self.object(at: indexPath) // CAUSE
            }
        }
        catch
        {
            print("An error ocurred: \(error)")
        }
    }
}

in which ObjC is an Objective-C class (from here). Here's the .h:

#import <Foundation/Foundation.h>

@interface ObjC : NSObject

+ (BOOL)catchException:(void(^)())tryBlock error:(__autoreleasing NSError **)error;

@end

And the .m:

@implementation ObjC

+ (BOOL)catchException:(void(^)())tryBlock error:(__autoreleasing NSError **)error
{
    @try
    {
        tryBlock();
        return YES;
    }
    @catch (NSException *exception)
    {
        *error = [[NSError alloc] initWithDomain:exception.name code:0 userInfo:exception.userInfo];
        return NO;
    }
}

@end

At tryObjectAtIndex the error Extension of a generic Objective-C class cannot access the class's generic parameters at runtime appears.

The only answer to related question 'How to write an extension for NSFetchedResultsController in Swift 4' suggests to add @objc in front of the function. But in the above code this does not make any difference.

Calling self.object(at:) causes this trouble. But actually, without the Objective-C exception handling, so just return self.object(at: indexPath) as the body of the function, the error does not occur.

How to I resolve this?

IMPORTANT: I'm at Xcode 8.3.2 and can't update at the moment (as it's a legacy project).

0条回答
登录 后发表回答