码迷,mamicode.com
首页 > 其他好文 > 详细

经常看到用[@[] mutableCopy]创建NSMutableArray,但未必好

时间:2015-05-26 12:53:33      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

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

经常看到用[@[] mutableCopy]创建NSMutableArray,但未必好

标签:

原文地址:http://my.oschina.net/ioslighter/blog/419998

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!