NSMutableArray 与 NSArray 哪个更好 [英] NSMutableArray vs NSArray which is better

查看:49
本文介绍了NSMutableArray 与 NSArray 哪个更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有点愚蠢的问题,但是如果我想将一个对象添加到数组中,我可以使用 NSMutableArrayNSArray 来实现,我应该这样做用吗?

This is a bit of a silly question, but if I want to add an object to an array I can do it with both NSMutableArray and NSArray, which should I use?

NSMutableArray * array1;
[array1 addObject:obj];
NSArray * array2;
array2 = [array2 arrayByAddingObject:obj];

推荐答案

使用 NSMutableArray,这就是它的用途.如果我在查看代码并且看到 NSArray,我希望它的集合永远保持不变,而如果我看到 NSMuteableArray,我知道该集合注定要改变.

Use NSMutableArray, that is what it is there for. If I was looking at code and I saw NSArray I would expect it's collection to stay constant forever, whereas if I see NSMuteableArray I know that the collection is destined to change.

现在听起来可能并不多,但是随着您的项目的发展以及您在它上面花费的时间越来越多,您最终会看到它的价值.

It might not sound like much right now, but as your project grows and as you spend more time on it you will see the value of this eventually.

这篇关于NSMutableArray 与 NSArray 哪个更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆