Search code examples
objective-cswiftnsobjectnsdocument

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


I've an objC NSDocument subclass, which is itself subclassed by a Swift version. The ObjC version calls BOOL result=[self attemptRecoveryFromError:error optionIndex:recoveryOptionIndex];, which is implemented by the Swift subclass.

From the Swift subclass, for certain errors, a call is made to super as follows:

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

This method is not implemented by the ObjC subclass but should be implemented by NSObject, but during runtime I get the following error:

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

Why does it not find the method? Does it perhaps have something to do with the type of Error not necessarily being a NSError?


Solution

  • attemptRecoveryFromError:optionIndex: is a method of informal protocol NSErrorRecoveryAttempting and is not implemented by NSObject See 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.

    More info: Error Recovery.