Mac Developer Library

Developer

Foundation Framework Reference NSMutableOrderedSet Class Reference

Options
Deployment Target:

On This Page
Language:

NSMutableOrderedSet

NSMutableOrderedSet class declares the programmatic interface to a mutable, ordered collection of distinct objects.

NSMutableOrderedSet objects are not like C arrays. That is, even though you may specify a size when you create a mutable ordered set, the specified size is regarded as a “hint”; the actual size of the set is still 0. This means that you cannot insert an object at an index greater than the current count of an set. For example, if a set contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), NSMutableOrderedSet raises an exception.

Inheritance


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in OS X v10.7 and later.
  • Creates and returns an mutable ordered set with a given initial capacity.

    Declaration

    Objective-C

    + (instancetype)orderedSetWithCapacity:(NSUInteger)numItems

    Parameters

    numItems

    The initial capacity of the new ordered set.

    Return Value

    A mutable ordered set with initial capacity to hold numItems members.

    Discussion

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

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.7 and later.

  • init(capacity:) - initWithCapacity: Designated Initializer

    Returns an initialized mutable ordered set with a given initial capacity.

    Declaration

    Swift

    init(capacity numItems: Int)

    Objective-C

    - (instancetype)initWithCapacity:(NSUInteger)numItems

    Parameters

    numItems

    The initial capacity of the new ordered set.

    Return Value

    An initialized mutable ordered set with initial capacity to hold numItems members.

    Discussion

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

    This method is a designated initializer of NSMutableOrderedSet.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • init() - init Designated Initializer

    Initializes a newly allocated mutable ordered set.

    Declaration

    Swift

    init()

    Objective-C

    - (instancetype)init

    Return Value

    A mutable ordered set.

    Discussion

    This method is a designated initializer of NSMutableOrderedSet.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.9 and later.

  • Appends a given object to the end of the mutable ordered set, if it is not already a member.

    Declaration

    Swift

    func addObject(_ object: AnyObject)

    Objective-C

    - (void)addObject:(id)object

    Parameters

    object

    The object to add to the set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Appends the given number of objects from a given C array to the end of the mutable ordered set.

    Declaration

    Swift

    func addObjects(_ objects: UnsafePointer<AnyObject?>, count count: Int)

    Objective-C

    - (void)addObjects:(const id [])objects count:(NSUInteger)count

    Parameters

    objects

    A C array of objects.

    count

    The number of values from the objects C array to append to the mutable ordered set. This number will be the count of the new array—it must not be negative or greater than the number of elements in objects.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Appends to the end of the mutable ordered set each object contained in a given array that is not already a member.

    Declaration

    Swift

    func addObjectsFromArray(_ array: [AnyObject])

    Objective-C

    - (void)addObjectsFromArray:(NSArray *)array

    Parameters

    array

    An array of objects to add to the set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Inserts the given object at the specified index of the mutable ordered set.

    Declaration

    Swift

    func insertObject(_ object: AnyObject, atIndex idx: Int)

    Objective-C

    - (void)insertObject:(id)object atIndex:(NSUInteger)idx

    Parameters

    object

    The object to insert into to the set’s content. This value must not be nil.

    idx

    The index in the mutable ordered set at which to insert object. This value must not be greater than the count of elements in the array.

    Discussion

    If the index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Inserts the given object at the specified index of the mutable ordered set.

    Declaration

    Objective-C

    - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)idx

    Parameters

    object

    The object to insert into to the set’s content. This value must not be nil.

    idx

    The index in the mutable ordered set at which to insert object. This value must not be greater than the count of elements in the array.

    Discussion

    If the index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.

    This method is identical to insertObject:atIndex:.

    Import Statement

    Objective-C

    @import Foundation;

    Availability

    Available in OS X v10.8 and later.

  • Inserts the objects in the array at the specified indexes.

    Declaration

    Swift

    func insertObjects(_ objects: [AnyObject], atIndexes indexes: NSIndexSet)

    Objective-C

    - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes

    Parameters

    objects

    An array of objects to insert into the mutable ordered set.

    indexes

    The indexes at which the objects in objects should be inserted. The count of locations in indexes must equal the count of objects.

    Discussion

    Each object in objects is inserted into the receiving mutable ordered set in turn at the corresponding location specified in indexes after earlier insertions have been made.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes a given object from the mutable ordered set.

    Declaration

    Swift

    func removeObject(_ object: AnyObject)

    Objective-C

    - (void)removeObject:(id)object

    Parameters

    object

    The object to remove from the mutable ordered set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes a the object at the specified index from the mutable ordered set.

    Declaration

    Swift

    func removeObjectAtIndex(_ idx: Int)

    Objective-C

    - (void)removeObjectAtIndex:(NSUInteger)idx

    Parameters

    idx

    The index of the object to remove from the mutable ordered set. The value must not exceed the bounds of the set.

    Discussion

    To fill the gap, all elements beyond index are moved by subtracting 1 from their index.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes the objects at the specified indexes from the mutable ordered set.

    Declaration

    Swift

    func removeObjectsAtIndexes(_ indexes: NSIndexSet)

    Objective-C

    - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes

    Parameters

    indexes

    The indexes of the objects to remove from the mutable ordered set. The locations specified by indexes must lie within the bounds of the mutable ordered .

    Discussion

    This method is similar to removeObjectAtIndex:, but allows you to efficiently remove multiple objects with a single operation.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes the objects in the array from the mutable ordered set.

    Declaration

    Swift

    func removeObjectsInArray(_ array: [AnyObject])

    Objective-C

    - (void)removeObjectsInArray:(NSArray *)array

    Parameters

    array

    An array containing the objects to be removed from the receiving mutable ordered set.

    Discussion

    This method is similar to removeObject:, but allows you to efficiently remove large sets of objects with a single operation. If the receiving mutable ordered set does not contain objects in array, the method has no effect (although it does incur the overhead of searching the contents).

    This method assumes that all elements in array respond to hash and isEqual:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes from the mutable ordered set each of the objects within a given range.

    Declaration

    Swift

    func removeObjectsInRange(_ range: NSRange)

    Objective-C

    - (void)removeObjectsInRange:(NSRange)range

    Parameters

    range

    The range of the objects to remove from the mutable ordered set.

    Discussion

    The objects are removed using removeObjectAtIndex:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Removes all the objects from the mutable ordered set.

    Declaration

    Swift

    func removeAllObjects()

    Objective-C

    - (void)removeAllObjects

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Replaces the object at the specified index with the new object.

    Declaration

    Swift

    func replaceObjectAtIndex(_ idx: Int, withObject object: AnyObject)

    Objective-C

    - (void)replaceObjectAtIndex:(NSUInteger)idx withObject:(id)object

    Parameters

    idx

    The index of the object to be replaced. This value must not exceed the bounds of the mutable ordered set.

    object

    The object with which to replace the object at the index in the ordered set specified by idx. This value must not be nil.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Replaces the objects at the specified indexes with the new objects.

    Declaration

    Swift

    func replaceObjectsAtIndexes(_ indexes: NSIndexSet, withObjects objects: [AnyObject])

    Objective-C

    - (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects

    Parameters

    indexes

    The indexes of the objects to be replaced.

    objects

    The objects with which to replace the objects in the receiving mutable ordered set at the indexes specified by indexes.

    The count of locations in indexes must equal the count of objects.

    Discussion

    The indexes in indexes are used in the same order as the objects in objects.

    If objects or indexes is nil, this method raises an exception.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Replaces the objects in the receiving mutable ordered set at the range with the specified number of objects from a given C array.

    Declaration

    Swift

    func replaceObjectsInRange(_ range: NSRange, withObjects objects: UnsafePointer<AnyObject?>, count count: Int)

    Objective-C

    - (void)replaceObjectsInRange:(NSRange)range withObjects:(const id [])objects count:(NSUInteger)count

    Parameters

    range

    The range of the objects to replace.

    objects

    A C array of objects.

    count

    The number of values from the objects C array to insert in place of the objects in range. This number will be the count of the new array—it must not be negative or greater than the number of elements in objects.

    Discussion

    Elements are added to the new array in the same order they appear in objects, up to but not including index count.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Appends or replaces the object at the specified index.

    Declaration

    Swift

    func setObject(_ obj: AnyObject, atIndex idx: Int)

    Objective-C

    - (void)setObject:(id)obj atIndex:(NSUInteger)idx

    Parameters

    obj

    The object to insert or append.

    idx

    The index. If the index is equal to the length of the collection, then it inserts the object at that index, otherwise it replaces the object at that index with the given object.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Moves the objects at the specified indexes to the new location.

    Declaration

    Swift

    func moveObjectsAtIndexes(_ indexes: NSIndexSet, toIndex idx: Int)

    Objective-C

    - (void)moveObjectsAtIndexes:(NSIndexSet *)indexes toIndex:(NSUInteger)idx

    Parameters

    indexes

    The indexes of the objects to move.

    idx

    The index in the mutable ordered set at which to insert the objects. The objects being moved are first removed from the set, then this index is used to find the location at which to insert the moved objects.

    Discussion

    An example:

    • NSMutableIndexSet *movedObjectIndexes = [NSMutableIndexSet indexSet];
    • [movedObjectIndexes addIndex: 1];
    • [movedObjectIndexes addIndex: 3];
    • NSMutableOrderedSet *mySet = [NSMutableOrderedSet orderedSetWithCapacity:5];
    • [mySet addObject:@"a"];
    • [mySet addObject:@"b"];
    • [mySet addObject:@"c"];
    • [mySet addObject:@"d"];
    • [mySet addObject:@"e"];
    • [mySet moveObjectsAtIndexes:movedObjectIndexes toIndex:2];

    This code results in the contents of mySet being [@"a", @"c", @"b", @"d", @"e"].

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Exchanges the object at the specified index with the object at the other index.

    Declaration

    Swift

    func exchangeObjectAtIndex(_ idx1: Int, withObjectAtIndex idx2: Int)

    Objective-C

    - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2

    Parameters

    idx1

    The index of the first object.

    idx2

    The index of the second object.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

  • Evaluates a given predicate against the mutable ordered set’s content and leaves only objects that match.

    Declaration

    Swift

    func filterUsingPredicate(_ predicate: NSPredicate)

    Objective-C

    - (void)filterUsingPredicate:(NSPredicate *)predicate

    Parameters

    predicate

    The predicate to evaluate against the set’s elements.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

    See Also

    filteredOrderedSetUsingPredicate: (NSOrderedSet).

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

    Declaration

    Swift

    func intersectOrderedSet(_ other: NSOrderedSet)

    Objective-C

    - (void)intersectOrderedSet:(NSOrderedSet *)other

    Parameters

    other

    The ordered set with which to perform the intersection.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

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

    Declaration

    Swift

    func intersectSet(_ other: NSSet)

    Objective-C

    - (void)intersectSet:(NSSet *)other

    Parameters

    other

    The set with which to perform the intersection.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

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

    Declaration

    Swift

    func minusOrderedSet(_ other: NSOrderedSet)

    Objective-C

    - (void)minusOrderedSet:(NSOrderedSet *)other

    Parameters

    other

    The ordered set of objects to remove from the receiving set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

    See Also

    – minusSet:

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

    Declaration

    Swift

    func minusSet(_ other: NSSet)

    Objective-C

    - (void)minusSet:(NSSet *)other

    Parameters

    other

    The set of objects to remove from the receiving set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

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

    Declaration

    Swift

    func unionOrderedSet(_ other: NSOrderedSet)

    Objective-C

    - (void)unionOrderedSet:(NSOrderedSet *)other

    Parameters

    other

    The set of objects to add to the receiving mutable ordered set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.

    See Also

    – unionSet:

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

    Declaration

    Swift

    func unionSet(_ other: NSSet)

    Objective-C

    - (void)unionSet:(NSSet *)other

    Parameters

    other

    The set of objects to add to the receiving mutable ordered set.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in OS X v10.7 and later.