XE4 ( Firemonkey + iOS Static Library) , Pascal conversion from Objective C Class?

混江龙づ霸主 提交于 2019-12-28 06:34:05

问题


How to Conversion ? (Objective C Class -> Delphi XE4 )

and How to use Objective-C Class in static Library from Delphi XE ?

Following is the my first trial. But It makes error.

Objective C Source

// 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

Here is wrong my conversion code. How to fix that ?

Delphi Source

// 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;

Thanks

Simon,Choi


回答1:


When you want to import a Objective C class you have to do the following:

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;

Also you need a dlopen('test.a', RTLD_LAZY) (dlopen is defined in Posix.Dlfcn)

Then you can use the code as following:

procedure Test;
var
   testClass: objc_test;
begin
   testClass := TObjc_TestClass.Create;
   testClass.test(3);

end;


来源:https://stackoverflow.com/questions/16515218/xe4-firemonkey-ios-static-library-pascal-conversion-from-objective-c-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!