无法对pyObjC中的对象调用方法

2024-09-30 02:32:29 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在pyObjC代码中调用setDelegate_时,我得到一个AttributeError: 'tuple' object has no attribute 'setDelegate_'。在

我的代码如下:

def createMovie(self):
        attribs = NSMutableDictionary.dictionary()
        attribs['QTMovieFileNameAttribute'] = '<My Filename>'
        movie = QTMovie.alloc().initWithAttributes_error_(attribs, objc.nil)
        movie.setDelegate_(self)

编辑

我发现我不能对movie对象使用任何实例方法。在


Tags: no代码selfobjectdefattributepyobjcmovie
2条回答

从您的注释来看,QTMovie.alloc().initWithAttributes_error_实际上返回了一个双元素元组,其中第一个元素是您想要的对象,第二个元素中有其他对象(可能是错误?)在

您应该能够访问该对象:

(movie, error) = QTMovie.alloc().initWithAttributes_error_(attribs, objc.nil)

选择器“initWithA属性:错误:“在Objective-C中有两个参数,第二个参数是通过引用传递的输出参数。Python没有通过引用传递的参数,因此PyObjC将该值作为第二个返回值返回,这就是此选择器的Python包装器返回元组的原因。这是一种通用机制,也可用于其他具有传递引用参数的方法。在

在目标C中:

QTMovie* movie;
NSError* error = nil;

movie = [[QTMovie alloc] initWithAttributes: attribs error:&error]
if (movie == nil) {
   // do something with error 
}

在Python中:

^{pr2}$

相关问题 更多 >

    热门问题