将 Python 类代理到 Objective-C
我尝试将现有的 Python 类代理到 Objective-C。我从 Python 类的简单示例开始:
Test.py
class Test:
def __init__(self):
self.text = ""
def addText(self, _text):
self.text = self.text + _text
def addSomeText(self):
self.addText("SomeText")
然后我为其编写了 Objective-C 类。
ITest.h
#import <Cocoa/Cocoa.h>
@interface ITest : NSObject
- (void) addText: (NSString *)text;
- (void) addSomeText;
+ newTest;
@property (nonatomic, assign, getter = _text) NSString *text;
@end
ITest.m
#import "ITest.h"
#define ABSTRACT { return nil; }
#define ABSTRACT_VOID { }
@implementation ITest
@dynamic text;
- (void) addText: (NSString *)text ABSTRACT_VOID;
- (void) addSomeText ABSTRACT_VOID;
+ newTest {
return [[NSClassFromString(@"TestProxy") new] autorelease];
}
@end
我的代理类(在一些关于 PyObjC 的文章之后,我对 Objective-C 类进行子类化并对原始类进行子类化以访问其实例变量):
TestProxy .py
import Foundation
import objc
from Test import Test
ITest = objc.lookUpClass("ITest")
class TestProxy (ITest, Test):
# getters/setters
def setText_(self, _text):
self.text = _text
def _text(self):
return self.text
# methods
def addText_(self, text):
Test.addText(self, text)
def addSomeText(self):
Test.addSomeText(self)
现在,如果我执行
ITest *test = [ITest newTest];
我总是得到 nil
,在调试控制台中没有任何警告或错误。
如果我删除对原始 Test
类的所有导入和引用,我将获得工作对象。我注意到,一旦我添加 from Test import Test
,nil
就会立即返回。
我认为发生这种情况是因为 PyObjC 尝试桥接 Test 类并失败,因为它的方法不符合桥命名规则。
有没有办法让 PyObjC 不这样做?或者,也许我所做的所有事情都是错误的,并且有更好的方法将现有的 Python 内容转换为 Objective-C?
I try to proxy existing Python class to Objective-C. I started from simple sample of Python class:
Test.py
class Test:
def __init__(self):
self.text = ""
def addText(self, _text):
self.text = self.text + _text
def addSomeText(self):
self.addText("SomeText")
Then I wrote Objective-C class for it.
ITest.h
#import <Cocoa/Cocoa.h>
@interface ITest : NSObject
- (void) addText: (NSString *)text;
- (void) addSomeText;
+ newTest;
@property (nonatomic, assign, getter = _text) NSString *text;
@end
ITest.m
#import "ITest.h"
#define ABSTRACT { return nil; }
#define ABSTRACT_VOID { }
@implementation ITest
@dynamic text;
- (void) addText: (NSString *)text ABSTRACT_VOID;
- (void) addSomeText ABSTRACT_VOID;
+ newTest {
return [[NSClassFromString(@"TestProxy") new] autorelease];
}
@end
My proxy class (following some articles about PyObjC I subclassing Objective-C class and subclassing original class to access its instance variables):
TestProxy.py
import Foundation
import objc
from Test import Test
ITest = objc.lookUpClass("ITest")
class TestProxy (ITest, Test):
# getters/setters
def setText_(self, _text):
self.text = _text
def _text(self):
return self.text
# methods
def addText_(self, text):
Test.addText(self, text)
def addSomeText(self):
Test.addSomeText(self)
Now, if I do
ITest *test = [ITest newTest];
I always get nil
with no warnings or errors in the debug console.
If I remove all imports and references to original Test
class, I'll get working object. I noticed, that nil
will be returned exactly as soon as I add from Test import Test
.
I think this happens because PyObjC trying to bridge the Test
class and fails because its methods do not conform bridge naming rules.
Is there a way to say PyObjC not to do so? Or, maybe, all stuff I did is wrong and there is a better way to translate existing Python stuff to Objective-C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是想确保您故意编写了没有返回类型的 newText 方法。
Objective-C 方法最有可能有其返回类型,如果您不想返回特定类型,请使用关键字“id”。
I just want to make sure that you wrote newText method without return type intentionally..
objective-c methods have its return type most likely, and if you don't want to a specific type to be return use keyword "id".