使用自动引用计数问题的指针的隐式转换获取错误 [英] Getting a error with implicit conversion of an pointer using Automatic reference Counting Issue

查看:77
本文介绍了使用自动引用计数问题的指针的隐式转换获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误使用ARC不允许将Objective-C指针隐式转换为void。

I am getting an error Implicit conversion of an Objective-C pointer to 'void' is disallowed with ARC

-(void)openAnimation {

     NSValue *contextPoint =[NSValue valueWithCGPoint:self.view.center];

    [UIView beginAnimation:nil context:contextPoint]; // getting error here
}

任何人都可以帮我解决这个错误

Can anyone help me to solve this error

谢谢

推荐答案

首先,我要指出,事情是更容易做基于块的动画方法。对于iOS4和iOS5, Apple建议您使用这些新的方法代替

So first, I would point out that this type of thing is easier to do with the block based animation methods. For iOS4 and iOS5, Apple recommends that you use those newer methods instead.

但是要回答你的具体问题,上下文参数是一个void *。 ARC不会(不能)维护任何void *指针的管理,所以当你传递你的'contextPoint'(这是一个NSValue *)到这个方法,ARC实际上失去了它的轨迹。

But to answer your specific question, the context parameter is a void *. ARC doesn't (can't) maintain any management of void * pointers, so when you pass your 'contextPoint' (which is an NSValue *) to this method, ARC is effectively losing track of it.

编译器将允许使用桥接转换。但是你还必须确保你的contextPoint超越了这个方法,所以你需要的特定转换是__bridge_retained,这是一个带有+1净保留计数的转型:

The compiler will allow this with a bridge cast. But you also have to make sure that your 'contextPoint' survives beyond this method, so the specific cast you need is __bridge_retained, which is a cast with a +1 net retain count:

[UIView beginAnimation:nil context:(__bridge_retained void *)contextPoint];

这解决了直接的问题,希望你现在泄露的contextPoint。所以在你的 animationDidStart:context: animationDidStop:finished:context:(无论你打算使用这个contextPoint)需要平衡那个+1计数与这样的:

That solves the immediate problem, expect that you will now be leaking that contextPoint. So in your animationDidStart:context: or animationDidStop:finished:context: (wherever you intended to use this contextPoint) you need to balance that +1 count with something like this:

NSValue *contextPoint = (__bridge_transfer NSValue *)context;

这会将对象桥接到ARC的控制之下,__bridge_transfer会告诉ARC释放该对象以平衡__bridge_retained

This bridges that object back under ARC's control and the __bridge_transfer tells ARC to release that object to balance the __bridge_retained from earlier.

同样,使用基于块的方法,让ARC和块为你处理这些事情。他们已经知道如何正确捕获和保留动画或完成块中所需的对象。

Again, use the block based methods instead, and let ARC and blocks take care of these things for you. They already know how to correctly capture and retain objects that you need in your animation or completion blocks.

我希望这样有意义。

这篇关于使用自动引用计数问题的指针的隐式转换获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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