[Pyobjc-dev] Documentation submission...
Brought to you by:
ronaldoussoren
From: Jiva D. <ji...@de...> - 2004-01-10 23:33:44
|
I would like to respectfully submit the following clarification/documentation for the docs page on pyobjc.sourceforge.net. (Credit to bbum for the basics, all I am adding is some example code to make it clear.) --------- Instantiating Python Classes from Obj-C First, follow these instructions:http://pyobjc.sourceforge.net/doc/ extending_objc_with_python.php to add python to your PyObjC app. Ok, that's the hard part. From here, everything is easy. Once you have imported your Python class, you can do this: @class MyPythonClass @implementation MyObjCClassCallingMyPythonClass -(void)aMethod { id myPythonObj = [[MyPythonClass alloc] init]; [myPythonObj someMethodInPython]; } @end Assuming you have a "MyPythonClass" defined in a python module in your resources directory. What this means, is basically PyObjC magically does it for you. Nothing else is needed. Now this will all work, and as long as you don't mind warnings when compiling... this is great. But, being obsessive about such things, we probably want to get rid of the compile warnings we're getting. To solve this problem, use an abstract superclass implemented in Objective-C (a class that simply declares all the methods where the implementation is stubbed out) and a concrete subclass implemented in Python. To the abstract superclass-- the objc class-- add a factory method that instantiates and returns the concrete subclass. So in other words to make Obj-C not complain about your undefined methods, you need to create an abstract superclass with a constructor that looks something like this: -(id)init { self = [[MyPythonClass alloc] init]; return self; } Or something along those lines. With this code, you should be able to instantiate your abstract superclass and get everything you need with no warnings! Viola! -- Jiva DeVoe jiva at devoesquared.com http://www.devoesquared.com |