如何转换? (Objective C Class – > Delphi XE4)
如何在Delphi XE的静态库中使用Objective-C类?
以下是我的第一次试验.
但它会犯错误.
目标C来源
- // objective C : test.h ----------------------------------------
- @interface objc_test : NSObject {
- BOOL busy;
- }
- - (int) test :(int) value;
- @end
- // objective C : test.m ----------------------------------------
- @implementation objc_test
- - (int) test :(int) value {
- busy = true;
- return( value + 1);
- }
- @end
Delphi来源
- // Delphi XE4 / iOS -------------------------------------------
- {$L test.a} // ObjC Static Library
- type
- objc_test = interface (NSObject)
- function test(value : integer) : integer; cdecl;
- end;
- Tobjc_test = class(TOCLocal)
- Public
- function GetObjectiveCClass : PTypeInfo; override;
- function test(value : integer): integer; cdecl;
- end;
- implmentation
- function Tobjc_test.GetObjectiveCClass : PTypeInfo;
- begin
- Result := TypeInfo(objc_test);
- end;
- function Tobjc_test.test(value : integer): integer;
- begin
- // ????????
- //
- end;
谢谢
西蒙,彩
解决方法
如果要导入Objective C类,则必须执行以下操作:
- type
- //here you define the class with it's non static Methods
- objc_test = interface (NSObject)
- [InterfaceGUID]
- function test(value : integer) : integer; cdecl;
- end;
- type
- //here you define static class Methods
- objc_testClass = interface(NSObjectClass)
- [InterfaceGUID]
- end;
- type
- //the TOCGenericImport maps objC Classes to Delphi Interfaces when you call Create of TObjc_TestClass
- TObjc_TestClass = class(TOCGenericImport<objc_testClass,objc_Test>) end;
你还需要一个dlopen(‘test.a’,RTLD_LAZY)(dlopen在Posix.Dlfcn中定义)
然后您可以使用以下代码:
- procedure Test;
- var
- testClass: objc_test;
- begin
- testClass := TObjc_TestClass.Create;
- testClass.test(3);
- end;