Can't call methods on objects in pyObjC

爷,独闯天下 提交于 2019-12-11 10:21:42

问题


When I call setDelegate_ within my pyObjC code I get an AttributeError: 'tuple' object has no attribute 'setDelegate_'.

My Code looks like this:

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

Edit

I Found out that i can't use any instance methods with the movie object.


回答1:


From your comment, it looks like QTMovie.alloc().initWithAttributes_error_ actually returns a two-element tuple, with the object you want as first element and some other object in the second element (possibly an error ?)

You should be able to access your object like that:

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



回答2:


The selector "initWithAttributes:error:" has two arguments in Objective-C, the second of which is a pass-by-reference output parameter. Python does not have pass-by-reference arguments, therefore PyObjC returns the value as a second return value, which is why the python wrapper for this selector returns a tuple. This is a general mechanism that's also used with other methods that have pass-by-reference arguments.

In Objective-C:

QTMovie* movie;
NSError* error = nil;

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

In Python:

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


来源:https://stackoverflow.com/questions/12131709/cant-call-methods-on-objects-in-pyobjc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!