NSSet NSDictionary NSArray --集合类型比较

本文对比了NSSet、NSArray和NSDictionary的特点及使用场景。NSSet适合需要快速访问且不关注顺序的情况;NSArray保持元素的插入顺序,适合有序集合;NSDictionary通过键值对存储数据,检索速度快。

**

三者的异同

**
NSSet
NSArray
NSDictionary

一篇三者特点对比
一篇三种集合遍历效率比较的文章
NSHipster上有一篇文章解释了 可变/不可变类簇的缺点


简单总结:
1. 三者都是集合,只能存放对象类型,对于C语言基础数据类型(int, float, double, BOOL等),则需要封装成NSNumber类型,在保存到集合类中
2. 三者都可以混合存放不同的对象类型(NSString、NSData等等)
3. NSArray存放的是有序的对象

An NSArray can hold objects in a sorted order. So object1 is always object1, and object2 is always object2. You can retrieve the first and last object from the array.

NSSet跟NSArray很像,但它存放的是无序的

You would typically use a set when access speed is of the essence and order doesn’t matter, or is determined by other means (through a predicate or sort descriptor). Core Data for example uses sets when managed objects are accessed via a to-many relationship.

NSDictionary 也是无序的,但是它已key-value的方式存储,检索会很快

The NSDictionary class is a bit of a magical one: it stores objects as key value pairs. Objects are not ordered, but can be retrieved simply by addressing them with an arbitrary string value.

4.NSSet的强大之处在于,可以方便的进行集合的操作(求交集、并集、差集等),如:intersectSet:minusSet:unionSet:

When is it better to use an NSSet over an NSArray?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值