Getting a error with implicit conversion of an poi

2019-05-29 22:31发布

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条回答
趁早两清
2楼-- · 2019-05-29 23:26

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.

查看更多
登录 后发表回答