Ios 泛型Objective-C类的扩展无法访问该类';s运行时的泛型参数
我有以下Ios 泛型Objective-C类的扩展无法访问该类';s运行时的泛型参数,ios,swift,exception,swift4,nsfetchedresultscontroller,Ios,Swift,Exception,Swift4,Nsfetchedresultscontroller,我有以下NSFetchedResultsController扩展名: extension NSFetchedResultsController { func tryObjectAtIndex(_ indexPath: IndexPath) throws -> AnyObject // ERROR HERE { do { try ObjC.catchException {
NSFetchedResultsController
扩展名:
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)")
}
}
}
其中,ObjC
是一个Objective-C类(from)。这是.h
:
#import <Foundation/Foundation.h>
@interface ObjC : NSObject
+ (BOOL)catchException:(void(^)())tryBlock error:(__autoreleasing NSError **)error;
@end
在tryObjectAtIndex
出现泛型Objective-C类的错误扩展无法在运行时访问该类的泛型参数
相关问题的唯一答案是建议在函数前面添加@objc
。但是在上面的代码中,这没有任何区别
调用self.object(at:)
会导致此问题。但是实际上,没有Objective-C异常处理,因此只要返回self.object(at:indexPath)
作为函数体,就不会发生错误
我如何解决这个问题
重要提示:我使用的是Xcode 8.3.2,目前无法更新(因为这是一个遗留项目)。您可以改为使用子类吗?
@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