[英]Disadvantage of using NSMutableArray vs NSArray?
I'm using an array to store cached objects loaded from a database in my iPhone app, and was wondering: are there any significant disadvantages to using NSMutableArray that I should know of? 我正在使用数组存储从iPhone应用程序中的数据库加载的缓存对象,并且想知道:使用NSMutableArray是否有我应该知道的明显缺点?
edit: I know that NSMutableArray can be modified, but I'm looking for specific reasons (performance, etc..) why one would use NSArray instead. 编辑:我知道NSMutableArray可以修改,但我正在寻找特定的原因(性能等。)为什么人们会改用NSArray。 I assume there would be a performance difference, but I have no idea whether it's significant or not. 我认为会有性能上的差异,但是我不知道它是否重要。
If you're loading objects from a database and you know exactly how many objects you have, you would likely get the best performance from NSMutableArray
s arrayWithCapacity:
method, and adding objects to it until full, so it allocates all the memory at once if it can. 如果要从数据库加载对象,并且确切知道有多少个对象,则可能会从NSMutableArray
的arrayWithCapacity:
方法中获得最佳性能,并向其中添加对象直到满,因此,如果它可以。
Behind the scenes, they're secretly the same thing - * NSArray
and NSMutableArray
are both implemented with CFArray
s via toll free bridging (a CFMutableArrayRef
and a CFArrayRef
are typedef's of the same thing, __CFArray *
)在幕后,他们是偷偷同样的事情- NSArray
和NSMutableArray
都与实施CFArray
s可通过免费电话进行桥接(一CFMutableArrayRef
和CFArrayRef
是类型定义是一样的东西, __CFArray *
)*
NSArray
and NSMutableArray
should have the same performance/complexity (access time being O(lg N) at worst and O(1) at best) and the only difference being how much memory the two objects would use - NSArray
has a fixed limit, while NSMutableArray
can use up as much space as you have free. NSArray
和NSMutableArray
应该具有相同的性能/复杂度(访问时间最差为O(lg N),最好为O(1)),唯一的区别是两个对象将使用多少内存NSArray
有固定的限制,而NSMutableArray
可以用尽所有可用空间。
The comments in CFArray.h have much more detail about this. CFArray.h中的注释对此有更多详细信息。
*: As Catfish_Man points out below, this isn't true anymore. *:正如Catfish_Man在下面指出的那样,这不再是事实。
The performance difference of using NSArray versus NSMutable array arises primarily when you use an API that wants to copy the array. 使用NSArray与NSMutable数组的性能差异主要是在您使用要复制该数组的API时出现的。 if you send -copy to an immutable array, it just bumps the retain count, but sending -copy to a mutable array will allocate heap memory. 如果将-copy发送到不可变数组,则只会增加保留计数,但是将-copy发送到可变数组将分配堆内存。
In addition, NSMutableArray is not threadsafe, while NSArray is (same with all the mutable vs. "immutable" objects). 另外,NSMutableArray不是线程安全的,而NSArray是(与所有可变对象和“不可变”对象相同)。 This could be a huge problem if you're multithreading. 如果您是多线程的,这可能是一个巨大的问题。
The main disadvantage to NSMutableArray
is that an object owning a NSMutableArray
may have the array changed behind its back if another object also owns it. NSMutableArray
的主要缺点是,拥有NSMutableArray
的对象可能在另一个对象也拥有的情况下更改了数组。 This may require you to code your object more defensively, less aggressively chasing performance. 这可能需要您更防御性地编码对象,而不太积极地追求性能。
If the NSMutableArray
is not exposed outside of the object, this isn't a concern. 如果NSMutableArray
没有暴露在对象外部,则不必担心。
NSArray
is a better choice for sharing, precisely because it is immutable. 正是因为它是不可变的,所以NSArray
是更好的共享选择。 Every object using it can assume it won't change, and doesn't need to defensively make a copy of it. 每个使用它的对象都可以假定它不会改变,并且不需要防御性地复制它。
This is probably the same reason that NSDictionary
copies its keys, rather than simply retaining them: it needs to be sure that they won't mutate, and copying is the only way to guarantee that. 这可能与NSDictionary
复制其密钥而不是简单地保留它们的原因相同:它需要确保它们不会突变,并且复制是保证密钥唯一的方法。
You can't modify NSArray
once it is created. 创建NSArray
后,您将无法对其进行修改。 If you need to add/remove objects from your array then you will use NSMutableArray
- not much options for that. 如果您需要从数组中添加/删除对象,则可以使用NSMutableArray
没有太多选择。 I assume NSArray
is optimized for fixed array operations. 我假设NSArray
针对固定数组操作进行了优化。 Mutable array provides flexibly of being modifiable. 可变数组提供了可修改的灵活性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.