简体   繁体   English

使用ARC重新分配对象

[英]Reassigning an object with ARC

If I've created an NSArray in the init of an object. 如果我在对象的init中创建了一个NSArray。

Then later on I want to recreate a new NSArray to the same property should I set the old one to nil first? 然后我想重新创建一个新的NSArray到同一属性我应该先将旧的NSArray设置为nil吗?

ie

is it ok to just go... 可以去......

self.arrayProperty = [[NSArray alloc] init];

or should I do... 或者我应该......

self.arrayProperty = nil;
self.arrayProperty = [[NSArray alloc] init];

(I'm just using an array for the sake of this example but it's a general questions about properties). (我只是为了这个例子而使用数组,但这是关于属性的一般问题)。

If it makes any difference, it's a strong property. 如果它有任何区别,那就是一个强大的财产。

The first approach is fine, you don't need to set it explicitly to nil before assigning a new object, since the setter releases the backing object of the property before retaining and assigning the new one. 第一种方法很好,在分配新对象之前不需要将它显式设置为nil ,因为setter在保留和分配新属性之前释放属性的后备对象。 Just what you would do under MRC (except that here you don't autorelease the object). 正如你在MRC下所做的那样(除了在这里你没有自动释放对象)。

It's just the same, as with ARC an object dies when there are no more references to it. 它与ARC一样,当没有更多的引用时,对象就会死掉。 The only difference is that in the second code you're doing a useless extra operation. 唯一的区别是,在第二个代码中,您正在进行无用的额外操作。

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

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