delphi – XE4(Firemonkey iOS静态库),Objective C类的Pascal转换?

前端之家收集整理的这篇文章主要介绍了delphi – XE4(Firemonkey iOS静态库),Objective C类的Pascal转换?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何转换? (Objective C Class – > Delphi XE4)

如何在Delphi XE的静态库中使用Objective-C类?

以下是我的第一次试验.
但它会犯错误.

目标C来源

  1. // objective C : test.h ----------------------------------------
  2. @interface objc_test : NSObject {
  3. BOOL busy;
  4. }
  5. - (int) test :(int) value;
  6. @end
  7.  
  8. // objective C : test.m ----------------------------------------
  9. @implementation objc_test
  10. - (int) test :(int) value {
  11. busy = true;
  12. return( value + 1);
  13. }
  14. @end

这是我的转换代码错误.
如何解决

Delphi来源

  1. // Delphi XE4 / iOS -------------------------------------------
  2. {$L test.a} // ObjC Static Library
  3.  
  4. type
  5. objc_test = interface (NSObject)
  6. function test(value : integer) : integer; cdecl;
  7. end;
  8.  
  9. Tobjc_test = class(TOCLocal)
  10. Public
  11. function GetObjectiveCClass : PTypeInfo; override;
  12. function test(value : integer): integer; cdecl;
  13. end;
  14.  
  15. implmentation
  16.  
  17. function Tobjc_test.GetObjectiveCClass : PTypeInfo;
  18. begin
  19. Result := TypeInfo(objc_test);
  20. end;
  21.  
  22. function Tobjc_test.test(value : integer): integer;
  23. begin
  24. // ????????
  25. //
  26. end;

谢谢

西蒙,彩

解决方法

如果要导入Objective C类,则必须执行以下操作:
  1. type
  2. //here you define the class with it's non static Methods
  3. objc_test = interface (NSObject)
  4. [InterfaceGUID]
  5. function test(value : integer) : integer; cdecl;
  6. end;
  7.  
  8. type
  9. //here you define static class Methods
  10. objc_testClass = interface(NSObjectClass)
  11. [InterfaceGUID]
  12. end;
  13.  
  14. type
  15. //the TOCGenericImport maps objC Classes to Delphi Interfaces when you call Create of TObjc_TestClass
  16. TObjc_TestClass = class(TOCGenericImport<objc_testClass,objc_Test>) end;

你还需要一个dlopen(‘test.a’,RTLD_LAZY)(dlopen在Posix.Dlfcn中定义)

然后您可以使用以下代码

  1. procedure Test;
  2. var
  3. testClass: objc_test;
  4. begin
  5. testClass := TObjc_TestClass.Create;
  6. testClass.test(3);
  7.  
  8. end;

猜你在找的Delphi相关文章