为什么有些ios标头不带cdecl转换为pascal? [英] why some ios header are translated to pascal without cdecl?
问题描述
当我查看如何将某些ios标头转换为delphi时,我可以看到一个例子
When i look how some ios header are translated to delp i can see for exemple
typedef void(^FIRMessagingConnectCompletion)(NSError * __nullable error);
被翻译的
:
FIRMessagingConnectCompletion = procedure(error: NSError) of object;
但是我不明白为什么不使用 cdecl ;?因为ios中的所有内容都必须与cdecl一起使用;为什么没有这样翻译:
however i not understand why not with cdecl; ? because everything in ios must be with cdecl; Why it's not translated like :
FIRMessagingConnectCompletion = procedure(error: NSError) of object; cdecl;
注意/编辑
要回答雷米(Rumy)和鲁迪(Rudy)并弄清楚一切,我想举一个很好的例子来说明一个问题,该问题取自原始的Delphi来源
To answer to Remy and Rudy and make everything clear i want to make a good example of one problem, taken from the original Delphi source
在delphi中,我们声明了以下接口:
in delphi we have this interface declared :
NSURLSessionTaskDelegate = interface(IObjectiveC)
['{C48E0AED-64F3-45A4-8D42-E3DB12F668E7}']
procedure URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession;
task: NSURLSessionTask; willPerformHTTPRedirection: NSHTTPURLResponse; newRequest: NSURLRequest;
completionHandler: TFoundationCompletionHandler7); cdecl;
end;
使用
type
TFoundationCompletionHandler7 = procedure(param1: NSURLRequest) of object;
所以我决定像这样实现它:
so i decide to implement it like this :
TMyNSURLSessionTaskDelegate = class(TOCLocal, NSURLSessionTaskDelegate)
public
procedure URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession;
task: NSURLSessionTask; willPerformHTTPRedirection: NSHTTPURLResponse; newRequest: NSURLRequest;
completionHandler: TFoundationCompletionHandler7); cdecl;
end;
但是(您可以尝试)一旦完成TMyNSURLSessionTaskDelegate.create,您将收到错误 TFoundationCompletionHandler7 =对象的过程(参数1:NSURLRequest);不是有效的ObjectiveC类型
but (you can try youself) as soon as you will do TMyNSURLSessionTaskDelegate.create you will receive an error TFoundationCompletionHandler7 = procedure(param1: NSURLRequest) of object; is not a valid ObjectiveC type
那么问题出在哪里呢?如何使用NSURLSessionTaskDelegate?
So where is the problem ? how to use NSURLSessionTaskDelegate ?
推荐答案
您为Delphi的 NSURLSessionTaskDelegate
接口显示的声明与
The declaration you have shown for Delphi's NSURLSessionTaskDelegate
interface does not match Apple's declaration of URLSessionTaskDelegate
, and as such cannot be used as-is in Objective-C. And that is what the compiler is complaining about.
NSURLSessionTaskDelegate
参数应该使用的完成处理程序的类型看起来更像这样:
The types for the completion handlers used by NSURLSessionTaskDelegate
parameters should look more like this instead:
TFoundationCompletionHandler6 = procedure(param1: NSURLSessionAuthChallengeDisposition; param2: NSURLCredential); cdecl;
TFoundationCompletionHandler7 = procedure(request: NSURLRequest); cdecl;
TFoundationNeedNewBodyStream = procedure(param1: NSInputStream); cdecl;
(对象的
替换为 cdecl
)
在翻译iOS标头时,这显然是Embarcadero的一个错误(坦率地说,我很惊讶曾经通过测试而没有被发现).
That is clearly a bug on Embarcadero's part when translating iOS headers (and frankly, I'm surprised that ever made it through testing without being spotted).
然后,您将像这样实现您的委托类:
Then, you would implement your delegate class like this:
type
TMyUrlSessionHTTPRedirection = procedure(
session: NSURLSession; task: NSURLSessionTask;
willPerformHTTPRedirection: NSHTTPURLResponse;
var newRequest: NSURLRequest) of object;
TMyNSURLSessionTaskDelegate = class(TOCLocal, NSURLSessionTaskDelegate)
public
OnHttpRedirection: TMyUrlSessionHTTPRedirection;
...
procedure URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(
session: NSURLSession; task: NSURLSessionTask;
willPerformHTTPRedirection: NSHTTPURLResponse; newRequest: NSURLRequest;
completionHandler: TFoundationCompletionHandler7); cdecl;
...
end;
procedure TMyNSURLSessionTaskDelegate.URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(
session: NSURLSession; task: NSURLSessionTask;
willPerformHTTPRedirection: NSHTTPURLResponse; newRequest: NSURLRequest;
completionHandler: TFoundationCompletionHandler7); cdecl;
var
finalRequest: NSURLRequest;
begin
finalRequest := newRequest;
if Assigned(OnHttpRedirection) then
OnHttpRedirection(session, task, willPerformHTTPRedirection, finalRequest);
completionHandler(finalRequest);
end;
然后,您将创建 TMyNSURLSessionTaskDelegate
的实例,并将Delphi对象方法分配给 OnHttpRedirection
成员,然后将委托实例传递给iOS.在适当的时候,它将调用委托的 urlSession()
方法,该方法将调用 OnHttpRedirection
处理程序.
You would then create an instance of TMyNSURLSessionTaskDelegate
and assign a Delphi object method to the OnHttpRedirection
member, and then pass the delegate instance to iOS. When appropriate, it will call the delegate's urlSession()
method, which will call the OnHttpRedirection
handler.
在Embarcadero修复其错误之前,您必须使用正确的类型声明在自己的代码中重新声明 NSURLSessionTaskDelegate
接口.
Until Embarcadero fixes their bug, you will have to redeclare the NSURLSessionTaskDelegate
interface in your own code with the correct type declarations.
这篇关于为什么有些ios标头不带cdecl转换为pascal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!