Extension of a generic Objective-C class cannot access the class's generic parameters at runtime

别说谁变了你拦得住时间么 提交于 2019-12-10 17:57:31

问题


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).

来源:https://stackoverflow.com/questions/50005767/extension-of-a-generic-objective-c-class-cannot-access-the-classs-generic-param

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!