ios developer tiny share-20161024

本文介绍了Objective-C中Set的概念及用法。Set是一种无序且不允许重复元素的集合,与数组相比,在成员测试方面提供了更好的性能。文章还展示了如何创建和使用NSSet,并通过实例说明了Set中不会存在重复的对象引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天讲Objective-C的Set,无序不可以重复的集合。


Sets Are Unordered Collections

An NSSet is similar to an array, but maintains an unordered group of distinct objects, as shown in Figure 6-2.

Figure 6-2  A Set of Objects


Because sets don’t maintain order, they offer a performance improvement over arrays when it comes to testing for membership.

The basic NSSet class is again immutable, so its contents must be specified at creation, using either allocation and initialization or a class factory method, like this:

NSSet *simpleSet =
  [NSSet setWithObjects:@"Hello, World!", @42, aValue, anObject, nil];

As with NSArray, the initWithObjects: and setWithObjects: methods both take a nil-terminated, variable number of arguments. The mutable NSSet subclass is NSMutableSet.

Sets only store one reference to an individual object, even if you try and add an object more than once:

NSNumber *number = @42;
NSSet *numberSet =
  [NSSet setWithObjects:number, number, number, number, nil];
// numberSet only contains one object

For more information on sets, see Sets: Unordered Collections of Objects.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值