Getting a error with implicit conversion of an pointer using Automatic reference Counting Issue

僤鯓⒐⒋嵵緔 提交于 2020-01-03 02:54:10

问题


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

Thank you


回答1:


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.

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.

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];

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;

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.

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.

I hope that makes sense.



来源:https://stackoverflow.com/questions/8515475/getting-a-error-with-implicit-conversion-of-an-pointer-using-automatic-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!