我如何从object_getIvar(self,myIntVar)获取int值,因为它返回了一个指针 [英] How do I get the int value from object_getIvar(self, myIntVar) as it returns a pointer

查看:87
本文介绍了我如何从object_getIvar(self,myIntVar)获取int值,因为它返回了一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果object_getIvar中的变量是基本数据类型(例如float,int,bool),那么根据文档,该函数返回指针(id)时如何获取值.我已经尝试过将类型转换为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.

推荐答案

获取:

myFloat = 2.34f;

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

NSLog(@"%f", myFloatValue);

输出:

2.340000

2.340000

设置:

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

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

NSLog(@"%f", myFloat);

输出:

2.340000

2.340000

这篇关于我如何从object_getIvar(self,myIntVar)获取int值,因为它返回了一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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