在 PyObjC 中为 openFile 实现 NSApplication 委托协议

Implementing NSApplication delegate protocol for openFile in PyObjC

我想在 Python (pyobjc)

中执行此操作
-(BOOL) application: (NSApplication*)sharedApplication openFile:(NSString*) fileName {
...
}

我的代表是 Python class 这样的:

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

如何在 PyObjC 中实现 openFile 的 NSApplication 委托协议?

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 是一个 "informal protocol" 并且方法是可选的 there's no need in Python to declare your conformance。如果有,该协议将在 Python 端由混合样式 class 表示,并且您的 class 定义将如下所示:

class AppDelegate (NSObject, NSApplicationDelegate):
    pass