The following should be avoided:
NSMutableArray *aMutableArray = [@[] mutableCopy];
The problems with the previous notation are both of efficiency and readability. On the efficiency side, an unnecessarily immutable object is created and immediately thrown away; this unlikely will slow down your app (unless the method here is called
frequently) but there is really no reason to do this just to save some characters. Regarding the readability, we can see two problems here: the first is that when scanning through the code and seeing @[] your mind is immediately connected
to and instance of NSArray, but in this case you need to stop and check more thoughtfully. Another aspect to take into account is that it would be very likely that someone with less experience will see your code and depending on his background
he might not be very comfortable with the dichotomy between mutable and immutable objects. He or she could not be very familiar with the meaning of creating a mutable copy (obviously we are not suggesting that this knowledge is not necessary). Again, this
is not something absolutely wrong but is more about code usability (that includes readability).
https://github.com/objc-zen/objc-zen-book
本文探讨了Objective-C编程中创建NSMutableArray的一个常见误区,并强调了效率与代码可读性的重要性。文章指出使用简短语法创建不可变数组再转为可变数组的做法既不高效也降低了代码的易理解性。
2238

被折叠的 条评论
为什么被折叠?



