`attemptRecovery(fromError:optionIndex:)` not found on super of Swift subclass of NSDocument

`attemptRecovery(fromError:optionIndex:)` not found on super of Swift subclass of NSDocument

我有一个 objC NSDocument 子类,它本身是 Swift 版本的子类。 ObjC版本调用BOOL result=[self attemptRecoveryFromError:error optionIndex:recoveryOptionIndex];,由Swift子类实现

从Swift子类开始,对于某些错误,调用super如下:

    guard let (error as NSError).domain == "someDomain" else {
        return super.attemptRecovery(fromError: error, optionIndex: recoveryOptionIndex)
    }

该方法不是ObjC子类实现的,应该是NSObject实现的,但是在运行时出现如下错误:

-[App.AppDocument attemptRecoveryFromError:optionIndex:]: unrecognized selector sent to instance 0x60000350d340

为什么找不到方法?它可能与不一定是 NSError 的错误类型有关吗?

attemptRecoveryFromError:optionIndex: 是非正式协议 NSErrorRecoveryAttempting 的一种方法,未​​被 NSObject 实现,参见 Formal and Informal Protocols

An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

更多信息:Error Recovery