Re: [Pyobjc-dev] implementing objective C interface in python
Brought to you by:
ronaldoussoren
|
From: Ronald O. <ron...@ma...> - 2008-01-31 09:43:14
|
On 31 Jan, 2008, at 7:19, John Stoner wrote:
> in WiiRemoteDiscovery.h;
>
> @interface NSObject( WiiRemoteDiscoveryDelegate )
>
> - (void) WiiRemoteDiscovered:(WiiRemote*)wiimote;
> - (void) WiiRemoteDiscoveryError:(int)code;
>
> @end;
> in python;
>
> wd = WiiRemoteDiscovery.new()
> class wii_remote_discovery_delegate(WiiRemoteDiscoveryDelegate):
> def wiiRemoteDiscovered_(self, wiiRemote):
> self.wiiRemote=wiiRemote
> print "discovered"
> def WiiRemoteDiscoveryError_(self, returnCode):
> print "not discovered"
> x=wii_remote_discovery_delegate()
> wd.setDelegate_(x)
> wd.start()
>
> Traceback (most recent call last):
> File "/Users/jstoner/Documents/projects/boogiepants/wii.py", line
> 29, in <module>
> class wii_remote_discovery_delegate(WiiRemoteDiscoveryDelegate):
> NameError: name 'WiiRemoteDiscoveryDelegate' is not defined
> >>>
>
> Do I need to do a stub implementation of WiiRemoteDiscoveryDelegate
> or does PyObjC have a way to handle this?
WiiRemoteDiscoveryDelegate is not a class, but a category (which is
the ObjC way of adding new methods to an existingclass). You have to
define an objc.informal_protocol that defines which methods are
present in the WiiRemoteDiscoveryDelegate informal protocol (the
category in the header file). You can then define your delegate as a
subclass of NSObject, PyObjC will automaticly detect that you're
implementing an informal protocol and use the right type information
for your methods.
This is a sample protocol definition from the Foundation wrappers in
PyObjC 1.4:
NSArchiverCallback = _objc.informal_protocol(
"NSArchiverCallback",
[
# (Class)classForArchiver
_objc.selector(
None,
selector='classForArchiver',
signature='#@:',
isRequired=0,
),
# (id)replacementObjectForArchiver:(NSArchiver *)archiver
_objc.selector(
None,
selector='replacementObjectForArchiver:',
signature='@@:@',
isRequired=0,
),
]
)
The hardest part is defining the 'signature' strings, Apple's ADC
website contains an Objective-C runtime manual that explains how to
construct these strings.
Ronald
>
> --
> blog:
> http://www.generosity.org/stoner/
> 'In knowledge is power, in wisdom, humility.'
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/_______________________________________________
> Pyobjc-dev mailing list
> Pyo...@li...
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
|