使用NSMutableArray vs NSArray的缺点? [英] Disadvantage of using NSMutableArray vs NSArray?
问题描述
我使用数组来存储从我的iPhone应用程序中的数据库加载的缓存对象,并且想知道:使用NSMutableArray,我应该知道有什么重大缺点吗?
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?
编辑:我知道NSMutableArray可以修改,但我正在寻找特定的原因(性能,等..)为什么会使用NSArray代替。
edit: I know that NSMutableArray can be modified, but I'm looking for specific reasons (performance, etc..) why one would use NSArray instead. I assume there would be a performance difference, but I have no idea whether it's significant or not.
推荐答案
如果你正在加载从数据库中获取对象,你已经知道了多少对象,你可能会从 NSMutableArray
s 获得最佳性能。 arrayWithCapacity:
方法,并添加对象,直到完全,所以它分配所有的内存一次,如果可以。
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.
场景,他们秘密地是同样的事情 - * NSArray
和 NSMutableArray
都实现与 CFArray
通过免费桥接( CFMutableArrayRef
和 CFArrayRef
是typedef的 __ CFArray *
)
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
应该具有相同的性能/复杂性(访问时间在最坏的情况下为O(lg N),最好为O(1)内存两个对象会使用 - NSArray
有一个固定的限制,而 NSMutableArray
可以使用尽可能多的空间自由。
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.
CFArray.h 有更多的细节。
*:正如Catfish_Man指出的,这不是真的了。
*: As Catfish_Man points out below, this isn't true anymore.
这篇关于使用NSMutableArray vs NSArray的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!