Class: NSMutableArray

Inherits:
NSArray show all

Overview

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray.

Direct Known Subclasses

Array

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSArray

#addObserver:forKeyPath:options:context:, #addObserver:toObjectsAtIndexes:forKeyPath:options:context:, array, #arrayByAddingObject:, #arrayByAddingObjectsFromArray:, arrayWithArray:, arrayWithContentsOfFile:, arrayWithContentsOfURL:, arrayWithObject:, arrayWithObjects:, arrayWithObjects:count:, #componentsJoinedByString:, #containsObject:, #count, #description, #descriptionWithLocale:, #descriptionWithLocale:indent:, #enumerateObjectsAtIndexes:options:usingBlock:, #enumerateObjectsUsingBlock:, #enumerateObjectsWithOptions:usingBlock:, #filteredArrayUsingPredicate:, #firstObjectCommonWithArray:, #getObjects:range:, #indexOfObject:, #indexOfObject:inRange:, #indexOfObject:inSortedRange:options:usingComparator:, #indexOfObjectAtIndexes:options:passingTest:, #indexOfObjectIdenticalTo:, #indexOfObjectIdenticalTo:inRange:, #indexOfObjectPassingTest:, #indexOfObjectWithOptions:passingTest:, #indexesOfObjectsAtIndexes:options:passingTest:, #indexesOfObjectsPassingTest:, #indexesOfObjectsWithOptions:passingTest:, #initWithArray:, #initWithArray:copyItems:, #initWithContentsOfFile:, #initWithContentsOfURL:, #initWithObjects:, #initWithObjects:count:, #isEqualToArray:, #lastObject, #makeObjectsPerformSelector:, #makeObjectsPerformSelector:withObject:, #objectAtIndex:, #objectAtIndexedSubscript:, #objectEnumerator, #objectsAtIndexes:, #pathsMatchingExtensions:, #removeObserver:forKeyPath:, #removeObserver:forKeyPath:context:, #removeObserver:fromObjectsAtIndexes:forKeyPath:, #removeObserver:fromObjectsAtIndexes:forKeyPath:context:, #reverseObjectEnumerator, #setValue:forKey:, #sortedArrayHint, #sortedArrayUsingComparator:, #sortedArrayUsingDescriptors:, #sortedArrayUsingFunction:context:, #sortedArrayUsingFunction:context:hint:, #sortedArrayUsingSelector:, #sortedArrayWithOptions:usingComparator:, #subarrayWithRange:, #valueForKey:, #writeToFile:atomically:, #writeToURL:atomically:

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (Object) arrayWithCapacity(numItems)

Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects. Mutable arrays expand as needed; numItems simply establishes the object’s initial capacity.

Parameters:

  • numItems (Integer)

    The initial capacity of the new array.

Returns:

Instance Method Details

- (Object) addObject(anObject)

Inserts a given object at the end of the array.

Parameters:

  • anObject (Object)

    The object to add to the end of the array’s content. This value must not be nil.Important: Raises an NSInvalidArgumentException if anObject is nil.

Returns:

- (Object) addObjectsFromArray(otherArray)

Adds the objects contained in another given array to the end of the receiving array’s content.

Parameters:

  • otherArray (Array)

    An array of objects to add to the end of the receiving array’s content.

Returns:

- (Object) exchangeObjectAtIndex(idx1, withObjectAtIndex:idx2)

Exchanges the objects in the array at given indices.

Parameters:

  • idx1 (Integer)

    The index of the object with which to replace the object at index idx2.

  • idx2 (Integer)

    The index of the object with which to replace the object at index idx1.

Returns:

- (Object) filterUsingPredicate(predicate)

Evaluates a given predicate against the array’s content and leaves only objects that match

Parameters:

  • predicate (NSPredicate)

    The predicate to evaluate against the array’s elements.

Returns:

- (Object) initWithCapacity(numItems)

Returns an array, initialized with enough memory to initially hold a given number of objects. Mutable arrays expand as needed; numItems simply establishes the object’s initial capacity.

Parameters:

  • numItems (Integer)

    The initial capacity of the new array.

Returns:

  • (Object)

    An array initialized with enough memory to hold numItems objects. The returned object might be different than the original receiver.

- (Object) insertObject(anObject, atIndex:index)

Inserts a given object into the array’s contents at a given index. If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room. Note that NSArray objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array 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), NSMutableArray raises an exception.

Parameters:

  • anObject (Object)

    The object to add to the array’s content. This value must not be nil.Important: Raises an NSInvalidArgumentException if anObject is nil.

  • index (Integer)

    The index in the array at which to insert anObject. This value must not be greater than the count of elements in the array.Important: Raises an NSRangeException if index is greater than the number of elements in the array.

Returns:

- (Object) insertObjects(objects, atIndexes:indexes)

Inserts the objects in the provided array into the receiving array at the specified indexes. Each object in objects is inserted into the receiving array in turn at the corresponding location specified in indexes after earlier insertions have been made. The implementation is conceptually similar to that illustrated in the following example. The resulting behavior is illustrated by the following example. The locations specified by indexes may therefore only exceed the bounds of the receiving array if one location specifies the count of the array or the count of the array after preceding insertions, and other locations exceeding the bounds do so in a contiguous fashion from that location, as illustrated in the following examples.In this example, both new objects are appended to the end of the array. If you replace [indexes addIndex:4] with [indexes addIndex:6] (so that the indexes are 5 and 6), then the application will fail with an out of bounds exception.In this example, two objects are added into the middle of the array, and another at the current end of the array (index 4) which means that it is third from the end of the modified array. If you replace [indexes addIndex:4] with [indexes addIndex:6] (so that the indexes are 1, 2, and 6), then the output is (one, a, b, two, three, four, c).If objects or indexes is nil this method will raise an exception.

Parameters:

  • objects (Array)

    An array of objects to insert into the receiving array.

  • indexes (NSIndexSet)

    The indexes at which the objects in objects should be inserted. The count of locations in indexes must equal the count of objects. For more details, see the Discussion.

Returns:

- (Object) removeAllObjects

Empties the array of all its elements.

Returns:

- (Object) removeLastObject

Removes the object with the highest-valued index in the array

removeLastObject raises an NSRangeException if there are no objects in the array.

Returns:

- (Object) removeObject(anObject)

Removes all occurrences in the array of a given object. This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined on the basis of an object’s response to the isEqual: message. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).

Parameters:

  • anObject (Object)

    The object to remove from the array.

Returns:

- (Object) removeObject(anObject, inRange:aRange)

Removes all occurrences within a specified range in the array of a given object. Matches are determined on the basis of an object’s response to the isEqual: message. If the array does not contain anObject within aRange, the method has no effect (although it does incur the overhead of searching the contents).

Parameters:

  • anObject (Object)

    The object to remove from the array’s content.

  • aRange (NSRange)

    The range from which to remove anObject. Important: Raises an NSRangeException if aRange exceeds the bounds of the array.

Returns:

- (Object) removeObjectAtIndex(index)

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

Parameters:

  • index (Integer)

    The index from which to remove the object in the array. The value must not exceed the bounds of the array.Important: Raises an NSRangeException if index is beyond the end of the array.

Returns:

- (Object) removeObjectIdenticalTo(anObject)

Removes all occurrences of a given object in the array. This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).

Parameters:

  • anObject (Object)

    The object to remove from the array.

Returns:

- (Object) removeObjectIdenticalTo(anObject, inRange:aRange)

Removes all occurrences of anObject within the specified range in the array.

This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses. If the array does not contain anObject within aRange, the method has no effect (although it does incur the overhead of searching the contents).

Parameters:

  • anObject (Object)

    The object to remove from the array within aRange.

  • aRange (NSRange)

    The range in the array from which to remove anObject.Important: Raises an NSRangeException if aRange exceeds the bounds of the array.

Returns:

- (Object) removeObjectsAtIndexes(indexes)

Removes the objects at the specified indexes from the array. This method is similar to removeObjectAtIndex:, but allows you to efficiently remove multiple objects with a single operation. indexes specifies the locations of objects to be removed given the state of the array when the method is invoked, as illustrated in the following example. If indexes is nil this method will raise an exception.

Parameters:

  • indexes (NSIndexSet)

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

Returns:

- (Object) removeObjectsInArray(otherArray)

Removes from the receiving array the objects in another given array. This method is similar to removeObject:, but allows you to efficiently remove large sets of objects with a single operation. If the receiving array does not contain objects in otherArray, the method has no effect (although it does incur the overhead of searching the contents).This method assumes that all elements in otherArray respond to hash and isEqual:.

Parameters:

  • otherArray (Array)

    An array containing the objects to be removed from the receiving array.

Returns:

- (Object) removeObjectsInRange(aRange)

Removes from the array each of the objects within a given range. The objects are removed using removeObjectAtIndex:.

Parameters:

  • aRange (NSRange)

    The range of the objects to remove from the array.

Returns:

- (Object) replaceObjectAtIndex(index, withObject:anObject)

Replaces the object at index with anObject.

Parameters:

  • index (Integer)

    The index of the object to be replaced. This value must not exceed the bounds of the array.Important: Raises an NSRangeException if index is beyond the end of the array.

  • anObject (Object)

    The object with which to replace the object at index index in the array. This value must not be nil.Important: Raises an NSInvalidArgumentException if anObject is nil.

Returns:

- (Object) replaceObjectsAtIndexes(indexes, withObjects:objects)

Replaces the objects in the receiving array at specified locations specified with the objects from a given array. The indexes in indexes are used in the same order as the objects in objects. If objects or indexes is nil this method will raise an exception.

Parameters:

  • indexes (NSIndexSet)

    The indexes of the objects to be replaced.

  • objects (Array)

    The objects with which to replace the objects in the receiving array at the indexes specified by indexes. The count of locations in indexes must equal the count of objects.

Returns:

- (Object) replaceObjectsInRange(aRange, withObjectsFromArray:otherArray)

Replaces the objects in the receiving array specified by a given range with all of the objects from a given array. If otherArray has fewer objects than are specified by aRange, the extra objects in the receiving array are removed. If otherArray has more objects than are specified by aRange, the extra objects from otherArray are inserted into the receiving array.

Parameters:

  • aRange (NSRange)

    The range of objects to replace in (or remove from) the receiving array.

  • otherArray (Array)

    The array of objects from which to select replacements for the objects in aRange.

Returns:

- (Object) replaceObjectsInRange(aRange, withObjectsFromArray:otherArray, range:otherRange)

Replaces the objects in the receiving array specified by one given range with the objects in another array specified by another range. The lengths of aRange and otherRange don’t have to be equal: if aRange is longer than otherRange, the extra objects in the receiving array are removed; if otherRange is longer than aRange, the extra objects from otherArray are inserted into the receiving array.

Parameters:

  • aRange (NSRange)

    The range of objects to replace in (or remove from) the receiving array.

  • otherArray (Array)

    The array of objects from which to select replacements for the objects in aRange.

  • otherRange (NSRange)

    The range of objects to select from otherArray as replacements for the objects in aRange.

Returns:

- (Object) setArray(otherArray)

Sets the receiving array’s elements to those in another given array.

Parameters:

  • otherArray (Array)

    The array of objects with which to replace the receiving array’s content.

Returns:

- (Object) setObject(anObject, atIndexedSubscript:index)

Replaces the object at the index with the new object, possibly adding the object. If the index is equal to count the element is added to the end of the array, growing the array.

Parameters:

  • anObject (Object)

    The object with which to replace the object at index index in the array. This value must not be nil.Important: Raises an NSInvalidArgumentException if anObject is nil.

  • index (Integer)

    The index of the object to be replaced. This value must not exceed the bounds of the array.Important: Raises an NSRangeException if index is beyond the end of the array.

Returns:

- (Object) sortUsingComparator(cmptr)

Sorts the array using the comparison method specified by a given NSComparator Block.

Parameters:

  • cmptr (NSComparator)

    A comparator block.

Returns:

- (Object) sortUsingDescriptors(sortDescriptors)

Sorts the receiving array using a given array of sort descriptors. See NSSortDescriptor for additional information.

Parameters:

  • sortDescriptors (Array)

    An array containing the NSSortDescriptor objects to use to sort the receiving array's contents.

Returns:

- (Object) sortUsingFunction(compare, context:context)

Sorts the array’s elements in ascending order as defined by the comparison function compare. This approach allows the comparison to be based on some outside parameter, such as whether character sorting is case-sensitive or case-insensitive.

Parameters:

  • compare

    The comparison function to use to compare two elements at a time.The function’s parameters are two objects to compare and the context parameter, context. The function should return NSOrderedAscending if the first element is smaller than the second, NSOrderedDescending if the first element is larger than the second, and NSOrderedSame if the elements are equal.

  • context

    The context argument to pass to the compare function.

Returns:

- (Object) sortUsingSelector(comparator)

Sorts the array’s elements in ascending order, as determined by the comparison method specified by a given selector.

Parameters:

  • comparator (Symbol)

    A selector that specifies the comparison method to use to compare elements in the array.The comparator message is sent to each object in the array and has as its single argument another object in the array. The comparator method should return NSOrderedAscending if the array is smaller than the argument, NSOrderedDescending if the array is larger than the argument, and NSOrderedSame if they are equal.

Returns:

- (Object) sortWithOptions(opts, usingComparator:cmptr)

Sorts the array using the specified options and the comparison method specified by a given NSComparator Block.

Parameters:

  • opts (NSSortOptions)

    A bitmask that specifies the options for the sort (whether it should be performed concurrently and whether it should be performed stably).

  • cmptr (NSComparator)

    A comparator block.

Returns: