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

前端之家收集整理的这篇文章主要介绍了delphi – XE4(Firemonkey iOS静态库),Objective C类的Pascal转换?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何转换? (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;
原文链接:https://www.f2er.com/delphi/239396.html

猜你在找的Delphi相关文章