经常看到用[@[] mutableCopy]创建NSMutableArray,但未必好
发布日期:2021-10-26 12:56:40 浏览次数:1 分类:技术文章

本文共 1222 字,大约阅读时间需要 4 分钟。

hot3.png

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://my.oschina.net/ioslighter/blog/419998

转载地址:https://blog.csdn.net/weixin_33782386/article/details/92460997 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:关于在 Laravel 5 中集成七牛云存储实现云存储功能
下一篇:bilibili 学习

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年03月24日 23时57分56秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章