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

时间:2012-08-26 16:15:36

标签: python delegates quicktime pyobjc qtkit

当我在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)

修改

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

2 个答案:

答案 0 :(得分:2)

选择器“initWithAttributes:error:”在Objective-C中有两个参数,第二个参数是pass-by-reference输出参数。 Python没有pass-by-reference参数,因此PyObjC将值作为第二个返回值返回,这就是为什么这个选择器的python包装器返回一个元组。这是一种通用机制,也可以与其他具有pass-by-reference参数的方法一起使用。

在Objective-C中:

QTMovie* movie;
NSError* error = nil;

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

在Python中:

movie, error = QTMovie.alloc().initWithAttributes_error_(attribs, None)
if movie is None:
  # do something with error

答案 1 :(得分:1)

从你的评论中,看起来QTMovie.alloc().initWithAttributes_error_实际上返回一个双元素元组,你想要的对象是第一个元素,第二个元素中有一些其他对象(可能是错误?)

您应该可以像这样访问您的对象:

(movie, error) = QTMovie.alloc().initWithAttributes_error_(attribs, objc.nil)
相关问题