I feel that when I use NSArray, I might constraint myself some time in future. Are there any good reasons to prefer this "primitive" one instead of the "complex" one, which is mutable?
我觉得当我使用NSArray时,我可能会在将来的某个时候限制自己。是否有任何充分的理由选择这种“原始”而不是“复杂”的,这是可变的?
3 个解决方案
#1
Using a non-mutable structure in your code is not much of a constraint - if it turns out that you need to modify it later on you can change to use a mutable one. It can be more of a constraint if you have it in an external interface mind you. The main advantages of non-mutable is that you gain thread safety and it is more easy to conceptualize the code - there is less to concern you if mutability is taken out of the equation. This is why we have const
constraints and so on - if you don't have to modify the data it is better to say so up front.
在代码中使用不可变的结构并不是一个约束 - 如果事后证明你需要修改它,你可以改为使用可变的结构。如果你在外部界面中考虑到这一点,它可能更具约束力。不可变的主要优点是你获得了线程安全性,并且更容易将代码概念化 - 如果从等式中取出可变性,则不太关心你。这就是为什么我们有const约束等等 - 如果你不必修改数据,最好事先说出来。
#2
One reason is the YAGNI principle. If you don't need a mutable array, don't use it. So your code will be easier to understand, test and maintain.
一个原因是YAGNI原则。如果您不需要可变数组,请不要使用它。因此,您的代码将更易于理解,测试和维护。
Remember that code is not only processed by a compiler but also read by coders or testers. Using NSMutableArray
instead of NSArray
has a meaning for them. It may be counterproductive to give them false information about your intentions.
请记住,代码不仅由编译器处理,还由编码器或测试人员读取。使用NSMutableArray而不是NSArray对它们有意义。向他们提供有关您意图的虚假信息可能会适得其反。
#3
In addition to the answers from Mssrs INFORMATION and mouviciel NSArray is faster than NSMutableArray. This is true for all the stati/mutable collections.
除了来自Mssrs INFORMATION和mouviciel的答案,NSArray比NSMutableArray更快。对于所有stati / mutable集合都是如此。
#1
Using a non-mutable structure in your code is not much of a constraint - if it turns out that you need to modify it later on you can change to use a mutable one. It can be more of a constraint if you have it in an external interface mind you. The main advantages of non-mutable is that you gain thread safety and it is more easy to conceptualize the code - there is less to concern you if mutability is taken out of the equation. This is why we have const
constraints and so on - if you don't have to modify the data it is better to say so up front.
在代码中使用不可变的结构并不是一个约束 - 如果事后证明你需要修改它,你可以改为使用可变的结构。如果你在外部界面中考虑到这一点,它可能更具约束力。不可变的主要优点是你获得了线程安全性,并且更容易将代码概念化 - 如果从等式中取出可变性,则不太关心你。这就是为什么我们有const约束等等 - 如果你不必修改数据,最好事先说出来。
#2
One reason is the YAGNI principle. If you don't need a mutable array, don't use it. So your code will be easier to understand, test and maintain.
一个原因是YAGNI原则。如果您不需要可变数组,请不要使用它。因此,您的代码将更易于理解,测试和维护。
Remember that code is not only processed by a compiler but also read by coders or testers. Using NSMutableArray
instead of NSArray
has a meaning for them. It may be counterproductive to give them false information about your intentions.
请记住,代码不仅由编译器处理,还由编码器或测试人员读取。使用NSMutableArray而不是NSArray对它们有意义。向他们提供有关您意图的虚假信息可能会适得其反。
#3
In addition to the answers from Mssrs INFORMATION and mouviciel NSArray is faster than NSMutableArray. This is true for all the stati/mutable collections.
除了来自Mssrs INFORMATION和mouviciel的答案,NSArray比NSMutableArray更快。对于所有stati / mutable集合都是如此。