XE4 (Firemonkey + iOS静态库),从Objective C类转换Pascal



如何转换?(Objective C Class -> Delphi XE4)

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

以下是我的第一次试验。但是它出错了。

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

感谢

西蒙,崔

当你想导入一个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;

最新更新