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

前端 未结 5 725
感情败类
感情败类 2021-02-12 13:03

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条回答
  •  猫巷女王i
    2021-02-12 13:40

    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[].

提交回复
热议问题