从 Objective-C 中的 CGPoint 获取 Ivar 值

发布于 2024-09-03 13:46:10 字数 358 浏览 3 评论 0原文

感谢您查看我的问题。

我一直在尝试环顾四周,并且一直在尝试使用低级别的 IVar 以从课程中获取信息。我在尝试加载来自 CGPoint 的值时遇到问题。

CGPoint point;   
object_getInstanceVariable(obj, [columnName UTF8String], (void **)&point);
NSLog(@"%@", NSStringFromCGPoint(point)); 

其输出应该是 {220,180}

我实际上得到 {220, 1.72469e-38}

谁能帮我解释一下为什么会发生这种情况?

Thanks for looking at my question.

I have been trying to look around and I have been playing around with low level IVars to get information from the classes. I am having trouble trying to load the values that come from CGPoint.

CGPoint point;   
object_getInstanceVariable(obj, [columnName UTF8String], (void **)&point);
NSLog(@"%@", NSStringFromCGPoint(point)); 

The output of this should be {220,180}

The i am actually getting {220, 1.72469e-38}

Could anyone help me out and explain why this might be happening?

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

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

发布评论

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

评论(3

不再让梦枯萎 2024-09-10 13:46:10

这里真正的问题是 CGPoint 不是一个对象——它是一个普通的旧式 C 结构。 :-)

The real problem here is that a CGPoint is not an object -- it's a plain-old-C struct. :-)

扮仙女 2024-09-10 13:46:10

这里指针是混合在一起的。 point 是一个实例,因此 &point 是一个指针(类似于 void *),而不是指向指针的指针(void ** 类似)。你需要这样的东西:

CGPoint *point;   
object_getInstanceVariable(obj, [columnName UTF8String], (void **)&point);
NSLog(@"%@", NSStringFromCGPoint(*point)); 

Pointers are mixed up here. point is an instance, so &point is a pointer (void *-like), not a pointer to a pointer (void **-like). You need something like this:

CGPoint *point;   
object_getInstanceVariable(obj, [columnName UTF8String], (void **)&point);
NSLog(@"%@", NSStringFromCGPoint(*point)); 
浮华 2024-09-10 13:46:10

object_getInstanceVariable() 仅复制 ivar 偏移量开头处的数据指针。毕竟,这就是您通过最后一个 void ** 参数为其提供的所有存储空间。当您知道自己拥有的数据不仅仅是一个指针大小的数据时(如本例所示),您可以使用 ivar_getOffset() 来查找数据开始的位置,然后根据需要复制出尽可能多的字节

Ivar iv = object_getInstanceVariable(obj, [columnName UTF8String], NULL);
ptrdiff_t offset = ivar_getOffset(iv);
CGPoint point = *(CGPoint *)((char *)obj + offset);

:在这种情况下,取消引用 CGPoint * 会导致从引用的地址复制 sizeof(CGPoint) 字节;在一般情况下,您可以将计算出的地址中的 memcopy()bcopy() 数据放入适当大小的缓冲区中。

下一个问题是在运行时计算正确的大小......

object_getInstanceVariable() only copies a pointer-worth of data at the start of the ivar's offset. That's all the storage you give it, after all, with that final void ** argument. When you know you have more than a pointer-worth of data, as in this case, you can use ivar_getOffset() to find where the data starts then copy out as many bytes as you need:

Ivar iv = object_getInstanceVariable(obj, [columnName UTF8String], NULL);
ptrdiff_t offset = ivar_getOffset(iv);
CGPoint point = *(CGPoint *)((char *)obj + offset);

In this case, dereferencing a CGPoint * causes sizeof(CGPoint) bytes to be copied from the referenced address; in the general case, you could memcopy() or bcopy() data from the computed address into an appropriately sized buffer.

The next problem would be computing the correct size at runtime…

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