通过mutableCopy或arrayWithArray创建一个带有文字的NSMutableArray: [英] Creating an NSMutableArray with a literal via mutableCopy or arrayWithArray:

查看:199
本文介绍了通过mutableCopy或arrayWithArray创建一个带有文字的NSMutableArray:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


$

b


$ b

根据介绍ObjectiveC文字的WWDC视频, NSMutableArray 可以这样初始化: / p>

  [NSMutableArray arrayWithArray:@ []]; 

但是如果我们这样做的话:

  [@ [] mutableCopy]; 

我知道一个是初始化数组,另一个只是提供一个浅拷贝;因此内存管理是不同的。但是如果我们使用ARC,使用后者有什么缺点?它更贵吗? ARC可能处理的方式不同,但我只是想知道如果使用 mutableCopy 是'更糟糕'或不。

解决方案

在前一种情况下,最终会将两个对象添加到当前自动释放池中:NSArray文本和从中创建的NSMutableArray。在后者中,文本NSArray被添加到自动释放池,但是从它复制的NSMutableArray不会。因此,在排除自动释放池(例如,在运行循环周期结束时)的性能方面,第一种情况是略微更差,需要连续释放两个对象,而在第二种情况下复制将单独从自动释放的文本释放。在实践中,时间差将是无穷小的。


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.

解决方案

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.

这篇关于通过mutableCopy或arrayWithArray创建一个带有文字的NSMutableArray:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆