简体   繁体   English

XCSourceEditorCommandInvocation快速扩展导致“无法识别的选择器发送到实例”异常

[英]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: 当我尝试向XcodeKit类添加快速扩展(用于添加Xcode扩展的框架)时,编译器很乐意构建而没有任何错误,但是当代码运行时,出现以下异常:

-[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. 过去,我迅速扩展了ObjC类,没有任何问题,所以我有点陷入困境。

I've tried: 我试过了:

  • adding @objc before the method declaration. 在方法声明之前添加@objc
  • adding public to the extension and method. 在扩展名和方法中添加public
  • 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扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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