ARC 不允许隐式转换 Objective-C 指针

发布于 2024-12-23 09:27:30 字数 1003 浏览 1 评论 0原文

我正在玩这个花栗鼠教程,我遇到了一个问题以下代码(第 5 节):

// Create our shape associated with the ball's body    
cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);  
ballShape->e = 0.5; // Elasticity  
ballShape->u = 0.8; // Friction  
ballShape->data = ball; // Associate with out ball's UIImageView  

在最后一行 ballShape->data = ball;我试图将 ballShape 对象的 data 属性与 UIImageView 对象 ball 链接起来。如果我关闭 ARC 处理,这可以正常工作,但使用 ARC 我不能这样做,会出现错误:

"Implicit conversion of an Objective-C pointer to 'cpDataPointer' (aka 'void *') is disallowed with ARC"

由于 ballShape 是一个指针,并且原始对象具有数据属性,因此有什么方法可以将球对象分配给那个财产让 ARC 高兴吗?我正在尝试以下代码:

ballShape->data = (__bridge cpDataPointer)ball; // Associate with out ball's UIImageView  

这使错误消失,但这是否是此问题的正确解决方案?我已经看过 Apple 的 ARC 文档,但其中很多内容目前都超出了我的理解范围。如果这是一个非常基本的问题,请提前抱歉,但是“->” C 中的运算符让我感到困惑和愤怒。 :)

I'm playing around with this Chipmunk Tutorial and I'm running into a problem with the following code (in section 5):

// Create our shape associated with the ball's body    
cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);  
ballShape->e = 0.5; // Elasticity  
ballShape->u = 0.8; // Friction  
ballShape->data = ball; // Associate with out ball's UIImageView  

In the final line ballShape->data = ball; I'm trying to link the data property of the ballShape object with the UIImageView object ball. If I turn off ARC processing this works fine, but with ARC I can't do this, getting the error:

"Implicit conversion of an Objective-C pointer to 'cpDataPointer' (aka 'void *') is disallowed with ARC"

Since ballShape is a pointer, and the original object has a data property, is there any way I can assign the ball object to that property and make ARC happy? I'm trying the following code:

ballShape->data = (__bridge cpDataPointer)ball; // Associate with out ball's UIImageView  

This makes the error vanish but is this the correct fix for this problem? I've looked at Apple's ARC documentation but a lot of it is pretty much over my head at the moment. Sorry in advance if this is a pretty basic question, but the "->" operator in C confuses and angers me. :)

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

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

发布评论

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

评论(1

李不 2024-12-30 09:27:30

如果您只想存储指针,只要保留对的引用就是正确的。

If you just want to store pointer, it is correct as long as you keep your reference to your ball.

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