python - 在 PyObjC 中实现 openFile 的 NSApplication 委托(delegate)协议(protocol)

标签 python objective-c cocoa appdelegate pyobjc

我想在 Python (pyobjc) 中执行此操作

-(BOOL) application: (NSApplication*)sharedApplication openFile:(NSString*) fileName {
...
}

我的委托(delegate)是一个像这样的Python类:

class ApplicationDelegate(NSObject):
    ...
    def applicationDidFinishLaunching_(self, notification):
    ...
    def applicationWillTerminate_(self, sender):
    ...

如何在 PyObjC 中实现 openFile 的 NSApplication 委托(delegate)协议(protocol)?

最佳答案

Objective-C 方法名称为“application:openFile:”,包含冒号。 PyObjC translates ObjC names by replacing colons with underscores 。所以你需要的方法名称是“application_openFile_”:

class ApplicationDelegate (NSObject):
    def application_openFile_(self, application, fileName):
        pass

由于 NSApplicationDelegate 是一个“非正式协议(protocol)”,并且方法是可选的 there's no need in Python to declare your conformance 。如果有的话,该协议(protocol)将在 Python 端由 mixin 样式的类表示,并且您的类定义将如下所示:

class AppDelegate (NSObject, NSApplicationDelegate):
    pass

关于python - 在 PyObjC 中实现 openFile 的 NSApplication 委托(delegate)协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35355613/

相关文章:

python - 将字典中的键与字符串匹配

ios - 执行选择器 : didn't work properly when using dispatch_async and global_queue

cocoa - 网络服务器用 ="/path/to/file"png 回复 ?q "path bar"

python - 为什么数据类型在 Opencv python 包装器中必须是 'uint8'?

python - 运行时警告 : coroutine was never awaited self. _target(*self._args, **self._kwargs)

objective-c - valgrind 和 iOS SDK 4.2?

cocoa - 如何使 NSUndoManager 撤消/重做操作名称正常工作?

objective-c - 如何使用按钮删除大纲 View 中包含核心数据的行?

Python - 作业列表

ios - Objective-C:其 View 不在窗口层次结构中