这个问题与现有问题部分类似,但我仍然会遇到内存管理错误。
以下非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.
更多推荐
发布评论