swiftswift-extensionsxcode-extension

XCSourceEditorCommandInvocation swift extension causes "unrecognized selector sent to instance" exception


When I try and add swift extensions to classes in XcodeKit (framework for adding Xcode extensions) the compiler is happy to build without any errors, but when the code runs I get the following exception:

-[XCSourceEditorCommandInvocation test]: unrecognized selector sent to instance 0x7fc60543f2b0

Below is sample code which can reproduce the exception:

class SourceEditorCommand: NSObject, XCSourceEditorCommand {
    func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
        // Call extension method
        invocation.test() // <--- Exception thrown here

        completionHandler(nil)
    }
}

extension XCSourceEditorCommandInvocation {
    func test() {
        print("it works!")
    }
}

I've extended ObjC classes in swift in the past without any issues so I'm a bit stuck here.

I've tried:


Solution

  • Neither Objective-C categories nor Swift extensions are supported on the classes or types in XcodeKit.framework at this time.