本文介绍了XE4(Firemonkey + iOS 静态库),来自 Objective C 类的 Pascal 转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何转换?(目标 C 类 -> Delphi XE4)
How to Conversion ? (Objective C Class -> Delphi XE4 )
以及如何在 Delphi XE 的静态库中使用 Objective-C 类?
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 源代码
// 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 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 类时,你必须执行以下操作:
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;
您还需要一个 dlopen('test.a', RTLD_LAZY)
(dlopen 在 Posix.Dlfcn 中定义)
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;
这篇关于XE4(Firemonkey + iOS 静态库),来自 Objective C 类的 Pascal 转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!