在ARC中使用“context”变量的beginAnimations?(beginAnimations with “context”variable in ARC?)

编程入门 行业动态 更新时间:2024-10-25 20:22:03
在ARC中使用“context”变量的beginAnimations?(beginAnimations with “context”variable in ARC?)

这个问题与现有问题部分类似,但我仍然会遇到内存管理错误。

以下非ARC代码有效:

[UIView beginAnimations:... context:[[NSNumber numberWithInt:i] retain]];

和didStopSelector中的某个地方:

NSNumber * n = (NSNumber *)context; ... [n release];

我试图删除保留/释放并添加副本(并结合这些方式)但没有效果。

另外我看到另一个类似的问题:

在ARC中的多个UIImageViews上的UIView动画

它们将imageName变量作为context传递,但它们不描述它是保留还是自动释放。

问题:

1)如何正确地将我的代码转换为ARC?

2)如果你传递保留/自动释放的上下文(cousre,如果自动释放将在一般情况下工作),代码是否有任何区别?

The question partially similar to existing ones but I still get error with memory management.

The following non-ARC code work:

[UIView beginAnimations:... context:[[NSNumber numberWithInt:i] retain]];

and somewhere in didStopSelector:

NSNumber * n = (NSNumber *)context; ... [n release];

I tried to remove retain/release and to add copy (and combined these ways) but with no effect.

Additionally I saw another similar question:

UIView Animation on multiple UIImageViews in ARC

They pass imageName variable as context but they don't describe if it is retained or autoreleased.

Questions:

1)How to convert my code to ARC correctly?

2)Is there any difference in code if you pass retained/autoreleased context (of cousre, if autoreleased will work in general)?

最满意答案

尝试使用__bridge_retained来保留对象并将其__bridge_retained转换为void*

void *context = (__bridge_retained void *)( @1000 );

然后在animationDidStop您必须使用__bridge_transfer转移所有权。 此时ARC应该自然地释放当前自动释放池中的对象。

- (void)animationDidStop:(void *)context { NSNumber *n = (__bridge_transfer id)context; }

或者,您可以直接切换到基于块的API和引用视图。

Try __bridge_retained to retain object and cast it to void*

void *context = (__bridge_retained void *)( @1000 );

and then in animationDidStop you have to transfer ownership with __bridge_transfer. At this point ARC should naturally release the object in current autorelease pool.

- (void)animationDidStop:(void *)context { NSNumber *n = (__bridge_transfer id)context; }

Alternatively you can switch to block based API and reference views directly.

更多推荐

本文发布于:2023-08-05 20:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   context   ARC   beginAnimations   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!