4

Delphi XE4 可以导入 iOS ObjC 类吗?( 静态库 *.a )

对象代码:test.a

// test.h ---------------------------------------------------------------
#import <Foundation/Foundation.h>
@interface mycalc : NSObject {
   BOOL busy;
  }
- (int) calc : (int) value;
@end


//  test.m --------------------------------------------------------------
#import "test.h"
@implementation mycalc
 -(int) calc:(int)value {
    busy = TRUE;
    return ( value + 1);
  }
@end

用于测试的 Delphi XE4 代码“Unit1.pas”

Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, 
  System.Variants,System.TypInfo,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  //
  Posix.Dlfcn,
  Macapi.ObjectiveC,Macapi.ObjCRuntime,
  iOSapi.CocoaTypes,iOSapi.foundation,iOSapi.uikit,iOSapi.CoreGraphics;

{$Link libtest.a} // <-- ObjC Static Library (3 Architectures included)
                  //     But Compiled binary size is always same,
                  //     with or without "$Link libtest.a" lines.

type
  //
  mycalc = interface(NSObject)
   ['{12891142-0F45-410D-A9EF-212F1AE23294}'] // Any Unique GUID
   function  test(value : integer) : integer; cdecl; 
  end;

  mycalcclass = interface(NSObjectClass)
   ['{42891142-0F45-410D-A9EF-212F1AE23295}'] // Any Unique GUID
   end;

  //the TOCGenericImport maps objC Classes to Delphi Interfaces 
  // when you call Create of TObjc_TestClass
  TmycalcClass = class(TOCGenericImport<mycalcclass, mycalc>) end;

  // -------------------------------------------------------------------------
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
 Var
  occalc : mycalc;
  rtn    : integer;
  handle : integer;
begin
 handle := dlOpen('libtest.a',RTLD_LAZY); // ## Q2 : Is It Right ?
 occalc := TmycalcClass.Create;           // ## Q3 : Error :
                                          //          Can't found ObjC Class mycalc  
 rtn := occalc.test(100);                  
 Button1.Text := 'rst:' + IntToStr(rtn);
end;

end.

我用Lars 的建议 XE4(Firemonkey + iOS 静态库)重写代码,从 Objective C 类转换 Pascal?, 编译就OK了。

但是,在 iPad3 设备上运行程序后,当按下按钮时,它显示“ObjectiveC class myclac cound not be found”

我的问题是

Q1。XE4 和 iOS 静态库的语法是否正确?

Q2。with / wihtout Line {$Link libtest.a} 编译后的大小始终相同。我的代码有什么问题?

Q3。我知道 iOS App 不能使用除了苹果库之外的静态库。因此,不需要 dlOpen。这样对吗 ?

永远感谢

西蒙,崔

4

0 回答 0