我想在 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/