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

前端 未结 5 706
感情败类
感情败类 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条回答
  •  别跟我提以往
    2021-02-12 13:31

    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.

提交回复
热议问题