When is it better to use an NSSet over an NSArray?

前端 未结 11 1571
-上瘾入骨i
-上瘾入骨i 2020-12-12 10:27

I have used NSSets many times in my apps, but I have never created one myself.

When is it better to use an NSSet as opposed to an NSArray and why?

11条回答
  •  粉色の甜心
    2020-12-12 11:08

    The main differences have already been given in other answers.

    I'd just like to note that because of the way sets and dictionaries are implemented (i.e. using hashes),one should be careful not to use mutable objects for the keys.

    If a key is mutated then the hash will (probably) change too, pointing to a different index/bucket in the hash table. The original value won't be deleted and will actually be taken into account when enumerating or asking the structure for its size/count.

    This can lead to some really hard to locate bugs.

提交回复
热议问题