- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
arrayWithCapacity
是在 NSArray.h 中定义并在 NSArray.m
当我查看 GNUStep 的代码时假设,我可以得到 arrayWithCapacity
是调用 initWithCapacity
的普通方法:
+ (id) arrayWithCapacity: (NSUInteger)numItems
{
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
initWithCapacity: numItems]);
}
而initWithCapacity
是一个只做自初始化的简单方法。
- (id) initWithCapacity: (NSUInteger)numItems
{
self = [self init];
return self;
}
与执行的项目数有关的内存分配无关。
使用arrayWithCapacity
方法有什么好处?简单地使用 [[NSArray alloc] init]
会更好吗?
最佳答案
期望提供明确的大小可以改善内存分配,因为在添加项目时无需调整数组的大小。在实践中,它只是一个提示,并且有一些证据表明它并未实际使用(请参阅基础集合类中的 objc.io article)。
关于ios - 使用 arrayWithCapacity 有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24957911/
创建数组时不需要指定数组大小,对吗?那么,为什么需要 arrayWithCapacity 呢?如果我将数组的大小设置得比实际需要的小,可以吗? 最佳答案 arrayWithCapacity 是一种优化
我很抱歉,好像这是一个愚蠢的问题。如果是,请发表评论,我会删除它,但下面是我的代码: NumberedArray = [NSMutableArray arrayWithCapacity:50]; NS
arrayWithCapacity 是在 NSArray.h 中定义并在 NSArray.m 中实现的方法 当我查看 GNUStep 的代码时假设,我可以得到 arrayWithCapacity 是调
我是 Objective C 新手(具有 C++ 背景)。我想知道使用 arrayWithCapacity 在这里实际上预留了多少内存。我还没有告诉它我要在数组中放入什么类型的对象。 例如,如果我要存
我是一名 iPhone/Objective-C 新手,具有丰富的 Java 背景。 我正在学习有关 objective-c 中内存管理的更多信息,并且正在阅读 Apple 关于内存管理的文档:http
我有两个函数,应该使用哪一个?请解释区别。 答: - (NSMutableArray *)FunctionA:(int)count { NSMutableArray *a = [[NSMuta
我是一名优秀的程序员,十分优秀!