NSMutableArray,alloc或capacity? [英] NSMutableArray, alloc or capacity?
问题描述
除了返回自动释放对象和其他需要手动释放的对象之外,是否有任何理由去使用这两个对象?
Is there any reason to go with either of these, other than one returning an autoreleased object and the other needing a manual release?
NSMutableDictionary *drink = [[NSMutableDictionary alloc] init];
// do things ...
[drink release];
或
NSMutableDictionary *drink = [NSMutableDictionary dictionaryWithCapacity:10];
gary
推荐答案
这些是方便的方法,有助于减少你必须写的代码量,并在某些情况下有帮助。
These are convenience methods that are useful for reducing the amount of code you have to write, and are there to help in certain circumstances.
除了+ dictionaryWithCapacity:和+字典方法将被自动释放外,所获得的对象没有真正的区别,而-init返回的对象不会。
The object you get will have no real differences other than that the +dictionaryWithCapacity: and +dictionary methods will be autoreleased, and the one returned by -init won't.
另外,如果你知道有多少项将被放入字典,+ dictionaryWithCapacity:和-initWithCapacity:将提高效率,因为字典会能够在蝙蝠上分配正确的内存量。
As an aside, if you know how many items will be put into the dictionary, +dictionaryWithCapacity: and -initWithCapacity: will provide a slight boost in efficiency since the dictionary will be able to allocate the right amount of memory right off the bat.
这篇关于NSMutableArray,alloc或capacity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!