【问题标题】:PyObjc: implement NSXPCInterfacePyObjc:实现 NSXPCInterface
【发布时间】:2021-08-20 14:29:30
【问题描述】:

我正在编写一个扩展程序,主应用程序在 PyObjc 中。我想设置主应用程序和扩展程序之间的通信。 参考link,我尝试编写协议。

SampleExtensionProtocol = objc.formal_protocol('SampleExtensionProtocol', (), [
    objc.selector(None, b"upperCaseString:withReply:", signature=b"v@:@@",isRequired=0),
    objc.selector(None, b"setEnableTemperProof:withReply:", signature=b"v@:@@",isRequired=0),
])

连接对象已创建。

connection = NSXPCConnection.alloc().initWithMachServiceName_options_("com.team.extension",NSXPCConnectionPrivileged)

注册元数据也是如此。

objc.registerMetaDataForSelector(b'NSObject', b'upperCaseString:withReply:', {
'arguments': {
    3: {
       'callable': {
          'retval': {'type': b'@'}, 
          'arguments': {
              0: {'type': b'^v'}, 
              1: {'type': b'i'}, 
          },
       },
    }
  }
})

objc.registerMetaDataForSelector(b'NSObject', b'setEnableTemperProof:withReply:', {
'arguments': {
    3: {
       'callable': {
          'retval': {'type': b'@'}, 
          'arguments': {
              0: {'type': b'^v'}, 
              1: {'type': b'i'}, 
          },
       },
    }
  }
})

但是在创建接口时出错。

mySvcIF = Foundation.NSXPCInterface.interfaceWithProtocol_(SampleExtensionProtocol)


ValueError: NSInvalidArgumentException - NSXPCInterface: Unable to get extended method signature from Protocol data (SampleExtensionProtocol / upperCaseString:withReply:). Use of clang is required for NSXPCInterface.

【问题讨论】:

  • PyObjC 作者在这里。看起来 NSXPCInterface 需要 PyObjC 目前不提供的信息。我必须调查这里发生了什么,以及是否可以提供来自 PyObjC 的扩展信息。
  • 一种解决方法是在使用 clang 编译的小型 C 扩展中定义协议。不是很好,但至少你可以继续。

标签: macos pyobjc nsxpcconnection


【解决方案1】:

无法在 Python 中定义可与 NSXPCInterface 一起使用的协议,因为该类需要“扩展方法签名”,无法使用公共 API 进行注册,以便在 Objective-C 运行时中以编程方式创建协议。

作为一种解决方法,您必须在定义协议的小型 C 扩展中定义协议。 PyObjC 文档在https://pyobjc.readthedocs.io/en/latest/notes/using-nsxpcinterface.html 描述了一个小问题,包括如何避免该问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多