What is the logic behind having a mutable and immutable versions of classes like NSArray, NSDictionary etc in Objective C?

前端 未结 5 1135
半阙折子戏
半阙折子戏 2021-02-12 13:11

Why do common collection classes in Objective C like NSString, NSArray, NSDictionary etc have a mutable as well as an immutable version. What is the logic behind defining them s

5条回答
  •  鱼传尺愫
    2021-02-12 13:38

    Basically, when you know that a data structure is immutable, you can make many optimizations around that. For example, if an array is immutable, you can leave out all the code that would "grow" the array whenever you attempted to add an object, and you can simply have your immutable array be a wrapper around an id[].

提交回复
热议问题