Why NSMutableSet can be added more objects than capacity

165 views Asked by At

Create one nsmutableset with capacity for example 3, but you can add more than 3 objects into it. So what is the real capacity ?

From Apple Docs, seems like this is okay, but what is the purpose of "capacity" ?

Return Value A mutable set with initial capacity to hold numItems members.

Discussion Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

1

There are 1 answers

1
Paulw11 On

The real capacity is only limited by the available memory. The initial capacity just allows you to avoid growing the set dynamically when you know the actual or approximate size of the set. This is more efficient than continually growing the set.

For example, if you wanted a set to store the letters in a string (i.e. attack = {atck}), you know there are at most 26 different letters, so you can initialise with initial capacity 26 - this is a "hint" to the initialiser. It may allocate a larger or smaller capacity.