ios [NSMutableArray array] 与 [[NSMutableArray alloc] init] 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5423211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:21:29  来源:igfitidea点击:

Difference between [NSMutableArray array] vs [[NSMutableArray alloc] init]

iphoneobjective-ciosipadmemory-leaks

提问by Adelino

can someone tell me the difference in declare an mutable array with:

有人可以告诉我声明可变数组的区别:

NSMutableArray *array = [NSMutableArray array];

and

NSMutableArray *array = [[NSMutableArray alloc] init];

Because in the beginning I was declaring all my arrays with alloc, and if in the end of a certain function I returned the array created with alloc, I had to autorelease that array, because of memory leak problems.

因为一开始我用 alloc 声明了我的所有数组,如果在某个函数结束时我返回了用 alloc 创建的数组,由于内存泄漏问题,我不得不自动释放该数组。

Now using the first declaration I don't need to release anything.

现在使用第一个声明我不需要发布任何东西。

Thanks

谢谢

采纳答案by matt

Because in the beginning i was declaring all my arrays with alloc and if in the end of a certain function i returned the array created with alloc i had to autorelease that array, because memory leak problems. Now using the first declaration i don't need to release anything

因为一开始我用 alloc 声明了我的所有数组,如果在某个函数结束时我返回了用 alloc 创建的数组,我必须自动释放该数组,因为内存泄漏问题。现在使用第一个声明我不需要发布任何东西

That is exactly correct when you "vend" an object. But in other cases, when you create an object on iOS, where you have a choice between obtaining a ready-made autoreleased object and calling alloc followed by release, Apple wants you to use alloc and release, because this keeps the lifetime of the object short and under your control.

当您“出售”一个对象时,这是完全正确的。但在其他情况下,当你在 iOS 上创建对象时,你可以选择获取现成的自动释放对象和调用 alloc 后调用释放,苹果希望你使用 alloc 和释放,因为这样可以保持对象的生命周期简短且在您的控制之下。

The problem here is that autoreleased objects live in the autorelease pool and can pile up until the pool is drained, whenever that may be.

这里的问题是自动释放的对象存在于自动释放池中,并且可以堆积直到池耗尽,无论何时。

Another thing to watch out for is loops. You may generate autoreleased objects without being aware of it, and they just pile up in the pool. The solution is to create your own autorelease pool at the start of the loop and release it at the end of the loop, so that the objects are released each time thru the loop.

另一件需要注意的事情是循环。您可能会在不知道的情况下生成自动释放的对象,并且它们只是堆积在池中。解决方案是在循环开始时创建您自己的自动释放池并在循环结束时释放它,以便每次通过循环释放对象。

EDIT - 12/18/2011:But with iOS 5 and the coming of ARC, the autorelease mechanism is far more efficient, and there is no such thing as release, so the distinction between alloc-init and a convenience constructor vending an autoreleased object becomes moot. (Also it's now an @autoreleasepoolblock rather than an autorelease pool (pseudo-)object.)

编辑 - 2011 年 12 月 18 日:但是随着 iOS 5 和 ARC 的到来,自动释放机制的效率要高得多,并且没有这样的东西release,所以 alloc-init 和提供自动释放对象的便利构造函数之间的区别就变成了没有实际意义。(而且它现在是一个@autoreleasepool块而不是一个自动释放池(伪)对象。)

回答by BoltClock

The arrayclass method by itself produces an autoreleased array, meaning you don't have to (and should not) release it manually.

array本身的类方法产生自动释放的数组,这意味着你不必(也不应该)手动释放它。