iPhone Foundation - performance implications of mutable and xxxWithCapacity:0

Posted by Adam Eberbach on Stack Overflow See other posts from Stack Overflow or by Adam Eberbach
Published on 2010-05-05T00:08:12Z Indexed on 2010/05/05 0:18 UTC
Read the original article Hit count: 528

Filed under:
|
|

All of the collection classes have two versions - mutable and immutable, such as NSArray and NSMutableArray. Is the distinction merely to promote careful programming by providing a const collection or is there some performance hit when using a mutable object as opposed to immutable?

Similarly each of the collection classes has a method xxxxWithCapacity, like [NSMutableArray arrayWithCapacity:0]. I often use zero as the argument because it seems a better choice than guessing wrongly how many objects might be added. Is there some performance advantage to creating a collection with capacity for enough objects in advance? If not why isn't the function something like + (id)emptyArray?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk