【问题标题】:XE4 ( Firemonkey + iOS Static Library) , Pascal conversion from Objective C Class?XE4(Firemonkey + iOS 静态库),来自 Objective C 类的 Pascal 转换?
【发布时间】:2013-05-07 02:00:20
【问题描述】:

如何转换? (目标 C 类 -> 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 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;

谢谢

西蒙,崔

【问题讨论】:

  • 您似乎在 obj-c 和 pascal 中都实现了该功能。难道你不想在obj-c中实现并在pascal中消费吗?
  • 更重要的是,我确信编译器所做的不仅仅是“制造错误”。编译器努力描述错误。为什么你不能努力告诉我们它说了什么?

标签: delphi class type-conversion static-libraries firemonkey


【解决方案1】:

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

【讨论】:

  • 真的非常感谢,拉斯。这是非常有用的粘合剂。
猜你喜欢
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2011-02-03
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
相关资源
最近更新 更多