Creating an NSMutableArray with a literal via mutableCopy or arrayWithArray: [duplicate]

眉间皱痕 提交于 2019-12-22 03:41:26

问题


Possible Duplicate:
Is literal creation of an NSMutableDictionary less efficient than the class helper method?

According to the WWDC video that introduces ObjectiveC literals, NSMutableArrays can be initialized like so:

[NSMutableArray arrayWithArray:@[]];

but what if we were to do it like this:

[@[] mutableCopy];

I know that one is initializing the array, and the other is just providing a shallow copy; thus the memory management is different. But if we are using ARC, what are the disadvantages of using the latter? Is it more expensive? ARC probably handles both differently, but I'm just wondering if using mutableCopy is 'worse' or not.


回答1:


In the former case, you end up with two objects added to the current autorelease pool: the NSArray literal and the NSMutableArray that you’re creating from it. In the latter, the literal NSArray gets added to the autorelease pool but the NSMutableArray copied from it does not. So the first case is slightly worse, performance-wise, in that draining the autorelease pool (e.g. at the end of the run-loop cycle) requires releasing both objects in succession, while in the second case the copy will get released separately from the autoreleased literal. In practice, the time difference will be infinitesimal.



来源:https://stackoverflow.com/questions/12644341/creating-an-nsmutablearray-with-a-literal-via-mutablecopy-or-arraywitharray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!