Re: [Pyobjc-dev] implementing objective C interface in python
Brought to you by:
ronaldoussoren
|
From: John S. <joh...@gm...> - 2008-02-03 03:51:28
|
On Jan 31, 2008 3:43 AM, Ronald Oussoren <ron...@ma...> wrote:
>
> 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
>
>
>
>
Closer and closer:
wd = WiiRemoteDiscovery.new()
WiiRemoteDiscoveryDelegate = objc.informal_protocol(
"WiiRemoteDiscoveryDelegate",
[
# (void) WiiRemoteDiscovered:(WiiRemote*)wiimote;
objc.selector(
None,
selector='WiiRemoteDiscovered:',
signature='v@:@',
isRequired=0,
),
# (void) WiiRemoteDiscoveryError:(int)code;
objc.selector(
None,
selector='WiiRemoteDiscoveryError:',
signature='v@:i',
isRequired=0,
),
]
)
class wii_remote_discovery_delegate(NSObject):
def wiiRemoteDiscovered_(self, wiiRemote):
self.wiiRemote=wiiRemote
print "discovered"
def WiiRemoteDiscoveryError_(self, returnCode):
print "not discovered"
x=wii_remote_discovery_delegate.new()
wd.setDelegate_(x)
wd.start()
It still fails, but silently this time. Is it the 'self' reference?
I tried to find that doc you mentioned... I think it has been relocated if
not deleted. There are a few pages that I found that refer to it, but have
links to the wrong page.
--
blog:
http://www.generosity.org/stoner/
'In knowledge is power, in wisdom, humility.'
|