简体   繁体   中英

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:

  • adding @objc before the method declaration.
  • adding public to the extension and method.
  • I'm not extending a class cluster so it's likely not this question .
  • I'm not extending a protocol so it's likely not this question .

目前,XcodeKit.framework中的类或类型均不支持Objective-C类别或Swift扩展。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM