Thread: [Pyobjc-dev] implementing objective C interface in python
Brought to you by:
ronaldoussoren
|
From: John S. <joh...@gm...> - 2008-01-31 06:19:34
|
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?
--
blog:
http://www.generosity.org/stoner/
'In knowledge is power, in wisdom, humility.'
|
|
From: Bob V. <pyo...@bo...> - 2008-01-31 08:32:07
|
WiiRemoteDiscoveryDelegate is the name of an informal protocol,
declared here as a category, not an Objective-C class. You can't
instantiate an instance of a category as you can a class.
To implement this protocol, simply define a class that implements the
instance methods declared in the protocol interface, like so:
from Foundation import *
class WiiRemoteDiscoveryDelegate(NSObject):
def wiiRemoteDiscovered_(self, wiiRemote):
self.wiiRemote = wiiRemote
print "discovered"
def WiiRemoteDiscoveryError_(self, returnCode):
print "not discovered"
See http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_6_section_7.html
for more information.
Ciao,
Bob
On Jan 30, 2008, at 10:19 PM, 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?
> --
> 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
|
|
From: Ronald O. <ron...@ma...> - 2008-01-31 09:43:14
Attachments:
smime.p7s
|
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
|
|
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.'
|