当 object_getIvar(self, myIntVar) 返回指针时,如何从 object_getIvar(self, myIntVar) 获取 int 值

发布于 2024-07-30 20:43:09 字数 135 浏览 8 评论 0原文

如果 object_getIvar 中的变量是基本数据类型(例如 float、int、bool),根据文档,当函数返回指针(id)时,如何获取值。 我尝试过转换为 int, int* 但当我尝试将其传递给 NSLog 时,我收到有关不兼容指针类型的错误。

if the variable in object_getIvar is a basic data type (eg. float, int, bool) how do I get the value as the function returns a pointer (id) according to the documentation. I've tried casting to an int, int* but when I try to get that to NSLog, I get error about an incompatible pointer type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盛夏已如深秋| 2024-08-06 20:43:09

返回的值是对象中正确位置的值; 只是类型不对。 对于 intBOOL(但不是 float),您可以将指针强制转换为 int>BOOL,因为指针和整数的大小相同,并且它们可以相互转换:

(int)object_getIvar(obj, myIntVar)

The value that is returned is the value from the right place in the object; just not the right type. For int and BOOL (but not float), you could just cast the pointer to an int or BOOL, since pointers and ints are the same size and they can be cast to each other:

(int)object_getIvar(obj, myIntVar)
混浊又暗下来 2024-08-06 20:43:09

它可能将值装箱到 NSNumber 中。 您可以通过 NSLogging 返回的 id 的 className 来验证这一点,如下所示:

id returnedValue = object_getIvar(self, myIntVar);
NSLog(@"Class: %@", [returnedValue className]);

编辑:我在这里发现了另一个与此类似的问题:处理 object_getIvar 的返回值(id object, Ivar ivar)

从我自己的实验来看,我原来的假设是不正确的。 int 和 float 以及其他基元似乎作为实际值返回。 但是,最好使用 ivar_getTypeEncoding 来验证返回的值是否是您期望的类型。

It's probably boxing the value in an NSNumber. You can verify this by NSLogging the returned id's className, like so:

id returnedValue = object_getIvar(self, myIntVar);
NSLog(@"Class: %@", [returnedValue className]);

Edit: I found another question just like this one here: Handling the return value of object_getIvar(id object, Ivar ivar)

From my own experimentation, it would appear that my original assumption was incorrect. int and float and other primitives appear to be returned as the actual value. However, it would be appropriate to use ivar_getTypeEncoding to verify that the returned value is the type that you're expecting it to be.

温柔一刀 2024-08-06 20:43:09

您可以直接使用object_getInstanceVariable:(尚未测试)

void *ptr_to_result;
object_getInstanceVariable(obj, "intvarname", &ptr_to_result);
float result = *(float *)ptr_to_result;

you can use object_getInstanceVariable directly: (haven't tested it)

void *ptr_to_result;
object_getInstanceVariable(obj, "intvarname", &ptr_to_result);
float result = *(float *)ptr_to_result;
风追烟花雨 2024-08-06 20:43:09

获取

myFloat = 2.34f;

float myFloatValue;
object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue);

NSLog(@"%f", myFloatValue);

输出:

2.340000

设置

float newValue = 2.34f;
unsigned int addr = (unsigned int)&newValue;

object_setInstanceVariable(self, "myFloat", *(float**)addr);

NSLog(@"%f", myFloat);

输出:

2.340000

Getting:

myFloat = 2.34f;

float myFloatValue;
object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue);

NSLog(@"%f", myFloatValue);

Outputs:

2.340000

Setting:

float newValue = 2.34f;
unsigned int addr = (unsigned int)&newValue;

object_setInstanceVariable(self, "myFloat", *(float**)addr);

NSLog(@"%f", myFloat);

Outputs:

2.340000

凉月流沐 2024-08-06 20:43:09

对于 ARC:

受此答案启发:object_getIvar 无法读取BOOL iVar 的值
您必须对 object_getIvar 进行函数调用以获取基本类型 ivars。

typedef int (*XYIntGetVariableFunction)(id object, const char* variableName);
XYIntGetVariableFunction intVariableFunction = (XYIntGetVariableFunction)object_getIvar;
int result = intVariableFunction(object, intVarName);

我制作了一个有用的小宏来快速定义此类函数指针:

#define GET_IVAR_OF_TYPE_DEFININTION(type, capitalized_type) \
typedef type (*XY ## capitalized_type ## GetVariableFunctionType)(id object, Ivar ivar); \
XY ## capitalized_type ## GetVariableFunctionType XY ## capitalized_type ## GetVariableFunction = (XY ## capitalized_type ## GetVariableFunctionType)object_getIvar;

然后,对于基本类型,您需要指定对宏的调用(参数例如(long long,LongLong)将适合):

GET_IVAR_OF_TYPE_DEFININTION(int, Int)

之后是用于接收 int(或指定的)变量类型变得可用:

int result = XYIntGetVariableFunction(object, variableName)

For ARC:

Inspired by this answer: object_getIvar fails to read the value of BOOL iVar.
You have to cast function call for object_getIvar to get basic-type ivars.

typedef int (*XYIntGetVariableFunction)(id object, const char* variableName);
XYIntGetVariableFunction intVariableFunction = (XYIntGetVariableFunction)object_getIvar;
int result = intVariableFunction(object, intVarName);

I have made a small useful macro for fast definition of such function pointers:

#define GET_IVAR_OF_TYPE_DEFININTION(type, capitalized_type) \
typedef type (*XY ## capitalized_type ## GetVariableFunctionType)(id object, Ivar ivar); \
XY ## capitalized_type ## GetVariableFunctionType XY ## capitalized_type ## GetVariableFunction = (XY ## capitalized_type ## GetVariableFunctionType)object_getIvar;

Then, for basic types you need to specify calls to macro (params e.g. (long long, LongLong) will fit):

GET_IVAR_OF_TYPE_DEFININTION(int, Int)

And after that a function for receiving int(or specified) variable type become available:

int result = XYIntGetVariableFunction(object, variableName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文