我正在尝试使用带有 PyObjC 的分布式对象来制作一个简单的示例。在服务器端,我有(在 Xcode 中):
class VendedObject(NSObject):
@objc.signature('@24@0:')
def speak(self):
return 'woof'
class TalkAppDelegate(NSObject):
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
conn = NSConnection.defaultConnection()
NSLog("Creating connection")
obj = VendedObject.alloc().init()
print obj.description()
conn.setRootObject_(obj)
result = conn.registerName_("my_server")
if not result:
NSLog("Failed to register Name")
#conn.setDelegate_(self)
NSLog(conn.description())
当我运行它时,我得到:
2011-01-27 10:27:55.695 Talk[34432:a0f] Application did finish launching.
2011-01-27 10:27:55.698 Talk[34432:a0f] Creating connection
<VendedObject: 0x3e45970>
2011-01-27 10:27:55.701 Talk[34432:a0f] (** NSConnection 0x28f2030 receivePort <NSMachPort: 0x28f2160> sendPort <NSMachPort: 0x28f2160> refCount 2 **)
在客户端,我有:
class ListenAppDelegate(NSObject):
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
proxy_obj = NSConnection.rootProxyForConnectionWithRegisteredName_host_(
"my_server", None)
if not proxy_obj:
print 'Did not get an object from the server.'
else:
print proxy_obj.description()
print proxy_obj.speak()
我得到:
2011-01-27 10:28:35.821 Listen[34460:a0f] Application did finish launching.
<VendedObject: 0x3e45970>
2011-01-27 10:28:35.829 Listen[34460:a0f] -[OC_PythonString initWithBytes:length:encoding:]: unrecognized selector sent to instance 0x3635130
2011-01-27 10:28:35.832 Listen[34460:a0f] -[OC_PythonString initWithBytes:length:encoding:]: unrecognized selector sent to instance 0x3635130
我错过了一些东西,但不知道是什么?
编辑:修改为使用我认为正确的签名,并显示出现的新问题。谢谢。