NSMutableSet

Next

Overview


The NSMutableSet class declares the programmatic interface to a mutable, unordered collection of distinct objects.

The NSCountedSet class, which is a concrete subclass of NSMutableSet, supports mutable sets that can contain multiple instances of the same element. The NSSet class supports creating and managing immutable sets.

NSMutableSet is “toll-free bridged” with its Core Foundation counterpart, CFMutableSetRef. See “Toll-Free Bridging” for more information.

Subclassing Notes


There should be little need of subclassing. If you need to customize behavior, it is often better to consider composition instead of subclassing.

Methods to Override

In a subclass, you must override both of its primitive methods:

  • addObject:
  • removeObject:

You must also override the primitive methods of the NSSet class.

Tasks


Creating a Mutable Set


  • + setWithCapacity:
  • – initWithCapacity:
  • – init

Adding and Removing Entries


  • – addObject:
  • – filterUsingPredicate:
  • – removeObject:
  • – removeAllObjects
  • – addObjectsFromArray:

Combining and Recombining Sets


  • – unionSet:
  • – minusSet:
  • – intersectSet:
  • – setSet:

Class Methods

setWithCapacity:


Creates and returns a mutable set with a given initial capacity.

+ (instancetype)setWithCapacity:(NSUInteger)numItems

Parameters

numItems

The initial capacity of the new set.

Return Value

A mutable set with initial capacity to hold numItems members.

Discussion

Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

Availability

  • Available in OS X v10.0 and later.

See Also

Declared In

NSSet.h

Instance Methods


addObject:


Adds a given object to the set, if it is not already a member.

- (void)addObject:(id)object

Parameters

object

The object to add to the set.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – addObjectsFromArray:
  • – unionSet:

Related Sample Code

Declared In

NSSet.h

addObjectsFromArray:


Adds to the set each object contained in a given array that is not already a member.

- (void)addObjectsFromArray:(NSArray *)array

Parameters

array

An array of objects to add to the set.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – addObject:
  • – unionSet:

Declared In

NSSet.h

filterUsingPredicate:


Evaluates a given predicate against the set’s content and removes from the set those objects for which the predicate returns false.

- (void)filterUsingPredicate:(NSPredicate *)predicate

Parameters

predicate

A predicate.

Discussion

The following example illustrates the use of this method.

NSMutableSet *mutableSet =


    [NSMutableSet setWithObjects:@"One", @"Two", @"Three", @"Four", nil];


NSPredicate *predicate =


    [NSPredicate predicateWithFormat:@"SELF beginswith 'T'"];


[mutableSet filterUsingPredicate:predicate];


// mutableSet contains (Two, Three)


Availability

  • Available in OS X v10.5 and later.

Declared In

NSPredicate.h

init


Initializes a newly allocated set.

- (instancetype)init

Return Value

A set.

Discussion

This method is a designated initializer of NSMutableSet.

Availability

  • Available in OS X v10.9 and later.

See Also

  • – initWithCapacity:

Declared In

NSSet.h

initWithCapacity:


Returns an initialized mutable set with a given initial capacity.

- (instancetype)initWithCapacity:(NSUInteger)numItems

Parameters

numItems

The initial capacity of the set.

Return Value

An initialized mutable set with initial capacity to hold numItems members. The returned set might be different than the original receiver.

Discussion

Mutable sets allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

This method is a designated initializer for NSMutableSet.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – init
  • + setWithCapacity:

Declared In

NSSet.h

intersectSet:


Removes from the receiving set each object that isn’t a member of another given set.

- (void)intersectSet:(NSSet *)otherSet

Parameters

otherSet

The set with which to perform the intersection.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – removeObject:
  • – removeAllObjects
  • – minusSet:

Related Sample Code

Declared In

NSSet.h

minusSet:


Removes each object in another given set from the receiving set, if present.

- (void)minusSet:(NSSet *)otherSet

Parameters

otherSet

The set of objects to remove from the receiving set.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – removeObject:
  • – removeAllObjects
  • – intersectSet:

Related Sample Code

Declared In

NSSet.h

removeAllObjects


Empties the set of all of its members.

- (void)removeAllObjects

Availability

  • Available in OS X v10.0 and later.

See Also

  • – removeObject:
  • – minusSet:
  • – intersectSet:

Declared In

NSSet.h

removeObject:


Removes a given object from the set.

- (void)removeObject:(id)object

Parameters

object

The object to remove from the set.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – removeAllObjects
  • – minusSet:
  • – intersectSet:

Related Sample Code

Declared In

NSSet.h

setSet:


Empties the receiving set, then adds each object contained in another given set.

- (void)setSet:(NSSet *)otherSet

Parameters

otherSet

The set whose members replace the receiving set's content.

Availability

  • Available in OS X v10.0 and later.

Declared In

NSSet.h

unionSet:


Adds each object in another given set to the receiving set, if not present.

- (void)unionSet:(NSSet *)otherSet

Parameters

otherSet

The set of objects to add to the receiving set.

Availability

  • Available in OS X v10.0 and later.

See Also

  • – addObject:
  • – addObjectsFromArray:

Related Sample Code

Declared In

NSSet.h

Next




Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-10-22



Provide Feedback

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值