Foundation 能否告诉我一个 Objective-C 方法是否需要一个特殊的结构返回? [英] Can Foundation tell me whether an Objective-C method requires a special structure return?

查看:64
本文介绍了Foundation 能否告诉我一个 Objective-C 方法是否需要一个特殊的结构返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解的背景:Objective-C 方法调用基本上是一个带有两个隐藏参数(接收器和选择器)的 C 函数调用.Objective-C 运行时包含一个名为 objc_msgSend() 的函数,它允许以这种方式调用方法.不幸的是,当一个函数返回一个结构体时,可能需要一些特殊的处理.有一些神秘的(有些人可能会说是疯狂的)规则控制结构是否像其他值一样返回,或者它是否实际上是通过隐藏的第一个参数中的引用返回的.对于 Objective-C,在这些情况下必须使用另一个名为 objc_msgSend_stret() 的函数.

Background as I understand it: Objective-C method invocations are basically a C function call with two hidden parameters (the receiver and the selector). The Objective-C runtime contains a function named objc_msgSend() that allows to invoke methods that way. Unfortunately, when a function returns a struct some special treatment may be needed. There are arcane (some might say insane) rules that govern whether the structure is returned like other values or whether it's actually returned by reference in a hidden first argument. For Objective-C there's another function called objc_msgSend_stret() that must be used in these cases.

问题:给定一个方法,NSMethodSignature 或其他东西能告诉我我是否必须使用 objc_msgSend() 或 objc_msgSend_stret()?到目前为止,我们已经发现 NSMethodSignature 知道这一点,它会在调试输出中打印出来,但似乎没有公共 API.

The question: Given a method, can NSMethodSignature or something else tell me whether I have to use objc_msgSend() or objc_msgSend_stret()? So far we have found out that NSMethodSignature knows this, it prints it in its debug output, but there doesn't seem to be a public API.

如果您想回答到底为什么要这样做?!",请在开始之前阅读以下内容:https://github.com/erikdoe/ocmock/pull/41

In case you want to respond with "why on earth would you want to do that?!", please read the following before you do: https://github.com/erikdoe/ocmock/pull/41

推荐答案

Objective-C 在给定的架构上对 C 使用相同的底层 ABI,因为方法只是具有隐式 self 和 <代码>_cmd 参数.

Objective-C uses the same underlying ABI for C on a given architecture, because methods are just C functions with implicit self and _cmd arguments.

换句话说,如果你有一个方法:

In other words, if you have a method:

- (SomeStructType)myMeth:(SomeArgType)arg;

那么这真的是一个普通的 C 函数:

then really this is a plain C function:

SomeStructType myMeth(id self, SEL _cmd, SomeArgType arg);

我很确定您已经知道这一点,但我只是向其他读者提及它.换句话说,您想询问 libffi 或任何类型的类似库如何为该架构返回 SomeStructType.

I'm pretty sure you already know that, but I'm merely mentioning it for other readers. In other words, you want to ask libffi or any kind of similar library how SomeStructType would be returned for that architecture.

这篇关于Foundation 能否告诉我一个 Objective-C 方法是否需要一个特殊的结构返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆