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

前端 未结 5 1132
半阙折子戏
半阙折子戏 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:26

    In general for an API, an immutable class is going to be thread safe so you can read it directly in a background thread without worrying the contents will change...

    That matters more for things like a collection where contents can shift and you might be in the middle of enumerating them.

提交回复
热议问题