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

时间:2016-02-12 05:45:07

标签: python objective-c cocoa appdelegate pyobjc

我想用Python(pyobjc)

来做这件事
-(BOOL) application: (NSApplication*)sharedApplication openFile:(NSString*) fileName {
...
}

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

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

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

1 个答案:

答案 0 :(得分:2)

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是“非正式协议”,因此方法是可选的there's no need in Python to declare your conformance。如果有的话,协议将由一个mixin风格的类在Python端表示,你的类定义将如下所示:

class AppDelegate (NSObject, NSApplicationDelegate):
    pass