简体   繁体   English

用ARC重新初始化/重新分配对象

[英]reinitialize/reallocate an object with ARC

Im wondering if calling [[MyClass alloc] init] on an already existing (allocated) object create a leak in ARC? 我想知道是否在已经存在的(分配的)对象上调用[[MyClass alloc] init]会在ARC中造成泄漏? or does it manage the release and reallocation for you? 还是为您管理发布和重新分配?

would doing the following be more proper: 将做以下更合适的方法:

myObject = nil;
myObject = [[MyClass alloc] init];

which is more appropriate? 哪个更合适? is there another way to do this properly? 还有另一种方法可以正确地做到这一点吗?

With ARC, you don't need to set the myObject to be nil before reallocating it,only this line of code will be fine: 使用ARC,您不需要在重新分配myObject之前将其设置为nil,只有以下代码行可以:

myObject = [[MyClass alloc] init];

When xcode compiles the code, it will automatically add the right release mechanism in. 当xcode编译代码时,它将自动在其中添加正确的释放机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM