Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 245
user1497524 Avatar asked May 13 '13 05:05

user1497524


People also ask

What is a static library in Objective-C?

A UNIX static library is just a collection of object files. Normally the linker only pulls in an object file from a static library if doing so would resolve some undefined symbol. Not pulling in all object files reduces the size of the final executable. The dynamic nature of Objective-C complicates things slightly.

How to consume a third-party Objective-C library in iOS applications?

When working on iOS, you might encounter cases where you want to consume a third-party Objective-C library. In those situations, you can use a Xamarin.iOS Binding Project to create a C# binding that will allow you to consume the library in your Xamarin.iOS applications. Generally in the iOS ecosystem you can find libraries in 3 flavors:

Why are there no linker symbols for methods in Objective-C?

The dynamic nature of Objective-C complicates things slightly. Because the code that implements a method is not determined until the method is actually called, Objective-C does not define linker symbols for methods. Linker symbols are only defined for classes.

What are the required files for Objective-C API's?

ApiDefinition.cs - This file will contain the contracts that define how Objective-C API's will be wrapped in C#. Structs.cs - This file will hold any structures or enumeration values that are required by the interfaces and delegates.


1 Answers

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;
like image 67
Lars Avatar answered Oct 15 '22 07:10

Lars