Cocoa的NSMutableArray稀疏? [英] Is Cocoa's NSMutableArray sparse?
本文介绍了Cocoa的NSMutableArray稀疏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我创建一个NSMutableArray,可能有多达2 ^ 16个元素,但是大多数是空的,我会浪费空间还是NSMutableArray实现为稀疏数组?
NSArray 中的元素不能为空,并且没有默认值。为了表示
nil
,通常使用单例 [NSNull null]
,它仍然是对象的引用所以它消耗内存(指针)。我会考虑使用 NSDictionary
(或 NSMutableDictionary
)与数字( NSNumber
)键。 If I create an NSMutableArray that might have up to 2^16 elements, but will mostly be empty, will I be wasting space or is NSMutableArray implemented as a sparse array?
解决方案
Elements in an NSArray
can't be empty and there's no "default" value. To represent nil
, you'd usually use the singleton [NSNull null]
, which is still a reference to an object so it consumes memory (the pointer). I'd consider using NSDictionary
(or NSMutableDictionary
) with numeric (NSNumber
) keys instead.
这篇关于Cocoa的NSMutableArray稀疏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文