XE4 (Firemonkey + iOS static library), Pascal transform from Objective-C class?

How to convert? (Objective C Class → Delphi XE4)

and how to use the Objective-C class in a static library from Delphi XE?

Next up is my first test. But he makes a mistake.

Objective Source 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 

The conversion code is incorrect here. How to fix it?

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

thanks

Simon, Choi

+6
source share
1 answer

If you want to import the Objective-C class, you must do the following:

 type //here you define the class with it 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 determined Posix.Dlfcn)

Then you can use the code as follows:

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

Source: https://habr.com/ru/post/944843/


All Articles