从 Objective-C 中的 CGPoint 获取 Ivar 值
感谢您查看我的问题。
我一直在尝试环顾四周,并且一直在尝试使用低级别的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里真正的问题是 CGPoint 不是一个对象——它是一个普通的旧式 C 结构。 :-)
The real problem here is that a CGPoint is not an object -- it's a plain-old-C struct. :-)
这里指针是混合在一起的。
point
是一个实例,因此&point
是一个指针(类似于void *
),而不是指向指针的指针(void ** 类似)。你需要这样的东西:
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:object_getInstanceVariable()
仅复制 ivar 偏移量开头处的数据指针。毕竟,这就是您通过最后一个void **
参数为其提供的所有存储空间。当您知道自己拥有的数据不仅仅是一个指针大小的数据时(如本例所示),您可以使用 ivar_getOffset() 来查找数据开始的位置,然后根据需要复制出尽可能多的字节:在这种情况下,取消引用
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 finalvoid **
argument. When you know you have more than a pointer-worth of data, as in this case, you can useivar_getOffset()
to find where the data starts then copy out as many bytes as you need:In this case, dereferencing a
CGPoint *
causessizeof(CGPoint)
bytes to be copied from the referenced address; in the general case, you couldmemcopy()
orbcopy()
data from the computed address into an appropriately sized buffer.The next problem would be computing the correct size at runtime…