Class: NSArray
Overview
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects.
Direct Known Subclasses
Class Method Summary (collapse)
-
+ array
Creates and returns an empty array.
-
+ arrayWithArray:
Creates and returns an array containing the objects in another given array.
-
+ arrayWithContentsOfFile:
Creates and returns an array containing the contents of the file specified by a given path.
-
+ arrayWithContentsOfURL:
Creates and returns an array containing the contents specified by a given URL.
-
+ arrayWithObject:
Creates and returns an array containing a given object.
-
+ arrayWithObjects:
Creates and returns an array containing the objects in the argument list.
-
+ arrayWithObjects:count:
Creates and returns an array that includes a given number of objects from a given C array.
Instance Method Summary (collapse)
-
- addObserver:forKeyPath:options:context:
Raises an exception.
-
- addObserver:toObjectsAtIndexes:forKeyPath:options:context:
Registers an observer to receive key value observer notifications for the specified key-path relative to the objects at the indexes.
-
- arrayByAddingObject:
Returns a new array that is a copy of the receiving array with a given object added to the end.
-
- arrayByAddingObjectsFromArray:
Returns a new array that is a copy of the receiving array with the objects contained in another array added to the end.
-
- componentsJoinedByString:
Constructs and returns an NSString object that is the result of interposing a given separator between the elements of the array.
-
- containsObject:
Returns a Boolean value that indicates whether a given object is present in the array.
-
- count
Returns the number of objects currently in the array.
-
- description
Returns a string that represents the contents of the array, formatted as a property list.
-
- descriptionWithLocale:
Returns a string that represents the contents of the array, formatted as a property list.
-
- descriptionWithLocale:indent:
Returns a string that represents the contents of the array, formatted as a property list.
-
- enumerateObjectsAtIndexes:options:usingBlock:
Executes a given block using the objects in the array at the specified indexes.
-
- enumerateObjectsUsingBlock:
Executes a given block using each object in the array, starting with the first object and continuing through the array to the last object.
-
- enumerateObjectsWithOptions:usingBlock:
Executes a given block using each object in the array.
-
- filteredArrayUsingPredicate:
Evaluates a given predicate against each object in the receiving array and returns a new array containing the objects for which the predicate returns true.
-
- firstObjectCommonWithArray:
Returns the first object contained in the receiving array that’s equal to an object in another given array.
-
- getObjects:range:
Copies the objects contained in the array that fall within the specified range to aBuffer.
-
- indexesOfObjectsAtIndexes:options:passingTest:
Returns the indexes, from a given set of indexes, of objects in the array that pass a test in a given Block for a given set of enumeration options.
-
- indexesOfObjectsPassingTest:
Returns the indexes of objects in the array that pass a test in a given Block.
-
- indexesOfObjectsWithOptions:passingTest:
Returns the indexes of objects in the array that pass a test in a given Block for a given set of enumeration options.
-
- indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object.
-
- indexOfObject:inRange:
Returns the lowest index within a specified range whose corresponding array value is equal to a given object .
-
- indexOfObject:inSortedRange:options:usingComparator:
Returns the index, within a specified range, of an object compared with elements in the array using a given NSComparator block.
-
- indexOfObjectAtIndexes:options:passingTest:
Returns the index, from a given set of indexes, of the first object in the array that passes a test in a given Block for a given set of enumeration options.
-
- indexOfObjectIdenticalTo:
Returns the lowest index whose corresponding array value is identical to a given object.
-
- indexOfObjectIdenticalTo:inRange:
Returns the lowest index within a specified range whose corresponding array value is equal to a given object .
-
- indexOfObjectPassingTest:
Returns the index of the first object in the array that passes a test in a given Block.
-
- indexOfObjectWithOptions:passingTest:
Returns the index of an object in the array that passes a test in a given Block for a given set of enumeration options.
-
- initWithArray:
Initializes a newly allocated array by placing in it the objects contained in a given array.
-
- initWithArray:copyItems:
Initializes a newly allocated array using anArray as the source of data objects for the array.
-
- initWithContentsOfFile:
Initializes a newly allocated array with the contents of the file specified by a given path.
-
- initWithContentsOfURL:
Initializes a newly allocated array with the contents of the location specified by a given URL.
-
- initWithObjects:
Initializes a newly allocated array by placing in it the objects in the argument list.
-
- initWithObjects:count:
Initializes a newly allocated array to include a given number of objects from a given C array.
-
- isEqualToArray:
Compares the receiving array to another array.
-
- lastObject
Returns the object in the array with the highest index value.
-
- makeObjectsPerformSelector:
Sends to each object in the array the message identified by a given selector, starting with the first object and continuing through the array to the last object.
-
- makeObjectsPerformSelector:withObject:
Sends the aSelector message to each object in the array, starting with the first object and continuing through the array to the last object.
-
- objectAtIndex:
Returns the object located at index.
-
- objectAtIndexedSubscript:
Returns the object at the specified index.
-
- objectEnumerator
Returns an enumerator object that lets you access each object in the array.
-
- objectsAtIndexes:
Returns an array containing the objects in the array at the indexes specified by a given index set.
-
- pathsMatchingExtensions:
Returns an array containing all the pathname elements in the receiving array that have filename extensions from a given array.
-
- removeObserver:forKeyPath:
Raises an exception.
-
- removeObserver:forKeyPath:context:
Raises an exception.
-
- removeObserver:fromObjectsAtIndexes:forKeyPath:
Removes anObserver from all key value observer notifications associated with the specified keyPath relative to the array’s objects at indexes.
-
- removeObserver:fromObjectsAtIndexes:forKeyPath:context:
Raises an exception.
-
- reverseObjectEnumerator
Returns an enumerator object that lets you access each object in the array, in reverse order.
-
- setValue:forKey:
Invokes setValue:forKey: on each of the array's items using the specified value and key.
-
- sortedArrayHint
Analyzes the array and returns a “hint” that speeds the sorting of the array when the hint is supplied to sortedArrayUsingFunction:context:hint:.
-
- sortedArrayUsingComparator:
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator Block.
-
- sortedArrayUsingDescriptors:
Returns a copy of the receiving array sorted as specified by a given array of sort descriptors.
-
- sortedArrayUsingFunction:context:
Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.
-
- sortedArrayUsingFunction:context:hint:
Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.
-
- sortedArrayUsingSelector:
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given selector.
-
- sortedArrayWithOptions:usingComparator:
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator Block.
-
- subarrayWithRange:
Returns a new array containing the receiving array’s elements that fall within the limits specified by a given range.
-
- valueForKey:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
-
- writeToFile:atomically:
Writes the contents of the array to a file at a given path.
-
- writeToURL:atomically:
Writes the contents of the array to the location specified by a given URL.
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) array
Creates and returns an empty array. This method is used by mutable subclasses of NSArray.
+ (Object) arrayWithArray(anArray)
Creates and returns an array containing the objects in another given array.
+ (Object) arrayWithContentsOfFile(aPath)
Creates and returns an array containing the contents of the file specified by a given path. The array representation in the file identified by aPath must contain only property list objects (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this array are immutable, even if the array is mutable.
+ (Object) arrayWithContentsOfURL(aURL)
Creates and returns an array containing the contents specified by a given URL. The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.
+ (Object) arrayWithObject(anObject)
Creates and returns an array containing a given object.
+ (Object) arrayWithObjects(firstObj, ...)
Creates and returns an array containing the objects in the argument list. This code example creates an array containing three different types of element:
+ (Object) arrayWithObjects(objects, count:count)
Creates and returns an array that includes a given number of objects from a given C array. Elements are added to the new array in the same order they appear in objects, up to but not including index count. For example:
Instance Method Details
- (Object) addObserver(observer, forKeyPath:keyPath, options:options, context:context)
Raises an exception.
- (Object) addObserver(anObserver, toObjectsAtIndexes:indexes, forKeyPath:keyPath, options:options, context:context)
Registers an observer to receive key value observer notifications for the specified key-path relative to the objects at the indexes. The options determine what is included in the notifications, and the context is passed in the notifications. This is not merely a convenience method; invoking this method is potentially much faster than repeatedly invoking addObserver:forKeyPath:options:context:.
- (Array) arrayByAddingObject(anObject)
Returns a new array that is a copy of the receiving array with a given object added to the end. If anObject is nil, an NSInvalidArgumentException is raised.
- (Array) arrayByAddingObjectsFromArray(otherArray)
Returns a new array that is a copy of the receiving array with the objects contained in another array added to the end.
- (String) componentsJoinedByString(separator)
Constructs and returns an NSString object that is the result of interposing a given separator between the elements of the array. For example, this code excerpt writes "here be dragons" to the console:
- (Boolean) containsObject(anObject)
Returns a Boolean value that indicates whether a given object is present in the array. This method determines whether anObject is present in the array by sending an isEqual: message to each of the array’s objects (and passing anObject as the parameter to each isEqual: message).
- (Integer) count
Returns the number of objects currently in the array.
- (String) description
Returns a string that represents the contents of the array, formatted as a property list.
- (String) descriptionWithLocale(locale)
Returns a string that represents the contents of the array, formatted as a property list. For a description of how locale is applied to each element in the receiving array, see descriptionWithLocale:indent:.
- (String) descriptionWithLocale(locale, indent:level)
Returns a string that represents the contents of the array, formatted as a property list. The returned NSString object contains the string representations of each of the array’s elements, in order, from first to last. To obtain the string representation of a given element, descriptionWithLocale:indent: proceeds as follows:If the element is an NSString object, it is used as is. If the element responds to descriptionWithLocale:indent:, that method is invoked to obtain the element’s string representation. If the element responds to descriptionWithLocale:, that method is invoked to obtain the element’s string representation. If none of the above conditions is met, the element’s string representation is obtained by invoking its description method.
- (Object) enumerateObjectsAtIndexes(indexSet, options:opts, usingBlock:block)
Executes a given block using the objects in the array at the specified indexes. By default, the enumeration starts with the first object and continues serially through the array to the last element specified by indexSet. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter or the indexSet is nil this method will raise an exception.
- (Object) enumerateObjectsUsingBlock(block)
Executes a given block using each object in the array, starting with the first object and continuing through the array to the last object. If the Block parameter is nil this method will raise an exception.
- (Object) enumerateObjectsWithOptions(opts, usingBlock:block)
Executes a given block using each object in the array. By default, the enumeration starts with the first object and continues serially through the array to the last object. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter is nil this method will raise an exception.
- (Array) filteredArrayUsingPredicate(predicate)
Evaluates a given predicate against each object in the receiving array and returns a new array containing the objects for which the predicate returns true. For more details, see Predicate Programming Guide.
- (Object) firstObjectCommonWithArray(otherArray)
Returns the first object contained in the receiving array that’s equal to an object in another given array. This method uses isEqual: to check for object equality.
- (Object) getObjects(aBuffer, range:aRange)
Copies the objects contained in the array that fall within the specified range to aBuffer. The method copies into aBuffer the objects in the array in the range specified by aRange; the size of the buffer must therefore be at least the length of the range multiplied by the size of an object reference, as shown in the following example (this is solely for illustration—you should typically not create a buffer simply to iterate over the contents of an array):
- (NSIndexSet) indexesOfObjectsAtIndexes(indexSet, options:opts, passingTest:predicate)
Returns the indexes, from a given set of indexes, of objects in the array that pass a test in a given Block for a given set of enumeration options. By default, the enumeration starts with the first object and continues serially through the array to the last element specified by indexSet. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter or the indexSet is nil this method will raise an exception.
- (NSIndexSet) indexesOfObjectsPassingTest(predicate)
Returns the indexes of objects in the array that pass a test in a given Block.
- (NSIndexSet) indexesOfObjectsWithOptions(opts, passingTest:predicate)
Returns the indexes of objects in the array that pass a test in a given Block for a given set of enumeration options. By default, the enumeration starts with the first object and continues serially through the array to the last object. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter is nil this method will raise an exception.
- (Integer) indexOfObject(anObject)
Returns the lowest index whose corresponding array value is equal to a given object. Starting at index 0, each element of the array is sent an isEqual: message until a match is found or the end of the array is reached. This method passes the anObject parameter to each isEqual: message. Objects are considered equal if isEqual: (declared in the NSObject protocol) returns YES.
- (Integer) indexOfObject(anObject, inRange:range)
Returns the lowest index within a specified range whose corresponding array value is equal to a given object . Starting at index 0, each element of the array is sent an isEqual: message until a match is found or the end of the array is reached. This method passes the anObject parameter to each isEqual: message. Objects are considered equal if isEqual: returns YES.This method raises an NSRangeException exception if the range parameter represents a range that doesn’t exist in the array.
- (Integer) indexOfObject(obj, inSortedRange:r, options:opts, usingComparator:cmp)
Returns the index, within a specified range, of an object compared with elements in the array using a given NSComparator block.
- (Integer) indexOfObjectAtIndexes(indexSet, options:opts, passingTest:predicate)
Returns the index, from a given set of indexes, of the first object in the array that passes a test in a given Block for a given set of enumeration options. By default, the enumeration starts with the first object and continues serially through the array to the last element specified by indexSet. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter or indexSet is nil this method will raise an exception.
- (Integer) indexOfObjectIdenticalTo(anObject)
Returns the lowest index whose corresponding array value is identical to a given object. Objects are considered identical if their object addresses are the same.
- (Integer) indexOfObjectIdenticalTo(anObject, inRange:range)
Returns the lowest index within a specified range whose corresponding array value is equal to a given object . Objects are considered identical if their object addresses are the same.
- (Integer) indexOfObjectPassingTest(predicate)
Returns the index of the first object in the array that passes a test in a given Block. If the Block parameter is nil this method will raise an exception.
- (Integer) indexOfObjectWithOptions(opts, passingTest:predicate)
Returns the index of an object in the array that passes a test in a given Block for a given set of enumeration options. By default, the enumeration starts with the first object and continues serially through the array to the last object. You can specify NSEnumerationConcurrent and/or NSEnumerationReverse as enumeration options to modify this behavior.Important: If the Block parameter is nil this method will raise an exception.
- (Object) initWithArray(anArray)
Initializes a newly allocated array by placing in it the objects contained in a given array. After an immutable array has been initialized in this way, it cannot be modified.
- (Object) initWithArray(array, copyItems:flag)
Initializes a newly allocated array using anArray as the source of data objects for the array. After an immutable array has been initialized in this way, it cannot be modified.The copyWithZone: method performs a shallow copy. If you have a collection of arbitrary depth, passing YES for the flag parameter will perform an immutable copy of the first level below the surface. If you pass NO the mutability of the first level is unaffected. In either case, the mutability of all deeper levels is unaffected.
- (Object) initWithContentsOfFile(aPath)
Initializes a newly allocated array with the contents of the file specified by a given path. The array representation in the file identified by aPath must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.
- (Object) initWithContentsOfURL(aURL)
Initializes a newly allocated array with the contents of the location specified by a given URL. The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.
- (Object) initWithObjects(firstObj, ...)
Initializes a newly allocated array by placing in it the objects in the argument list. After an immutable array has been initialized in this way, it can’t be modified.
- (Object) initWithObjects(objects, count:count)
Initializes a newly allocated array to include a given number of objects from a given C array. Elements are added to the new array in the same order they appear in objects, up to but not including index count.After an immutable array has been initialized in this way, it can’t be modified.
- (Boolean) isEqualToArray(otherArray)
Compares the receiving array to another array. Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.
- (Object) lastObject
Returns the object in the array with the highest index value.
- (Object) makeObjectsPerformSelector(aSelector)
Sends to each object in the array the message identified by a given selector, starting with the first object and continuing through the array to the last object. This method raises an NSInvalidArgumentException if aSelector is NULL.
- (Object) makeObjectsPerformSelector(aSelector, withObject:anObject)
Sends the aSelector message to each object in the array, starting with the first object and continuing through the array to the last object. This method raises an NSInvalidArgumentException if aSelector is NULL.
- (Object) objectAtIndex(index)
Returns the object located at index. If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.
- (Object) objectAtIndexedSubscript(idx)
Returns the object at the specified index. If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.This method is identical to objectAtIndex:.
- (NSEnumerator) objectEnumerator
Returns an enumerator object that lets you access each object in the array. Returns an enumerator object that lets you access each object in the array, in order, starting with the element at index 0, as in:
- (Array) objectsAtIndexes(indexes)
Returns an array containing the objects in the array at the indexes specified by a given index set. The returned objects are in the ascending order of their indexes in indexes, so that object in returned array with higher index in indexes will follow the object with smaller index in indexes.Raises an NSRangeException if any location in indexes exceeds the bounds of the array, of if indexes is nil.
- (Array) pathsMatchingExtensions(filterTypes)
Returns an array containing all the pathname elements in the receiving array that have filename extensions from a given array.
- (Object) removeObserver(observer, forKeyPath:keyPath)
Raises an exception.
- (Object) removeObserver(observer, forKeyPath:keyPath, context:context)
Raises an exception.
- (Object) removeObserver(anObserver, fromObjectsAtIndexes:indexes, forKeyPath:keyPath)
Removes anObserver from all key value observer notifications associated with the specified keyPath relative to the array’s objects at indexes. This is not merely a convenience method; invoking this method is potentially much faster than repeatedly invoking removeObserver:forKeyPath:.
- (Object) removeObserver(observer, fromObjectsAtIndexes:indexes, forKeyPath:keyPath, context:context)
Raises an exception.
- (NSEnumerator) reverseObjectEnumerator
Returns an enumerator object that lets you access each object in the array, in reverse order.
- (Object) setValue(value, forKey:key)
Invokes setValue:forKey: on each of the array's items using the specified value and key.
- (NSData) sortedArrayHint
Analyzes the array and returns a “hint” that speeds the sorting of the array when the hint is supplied to sortedArrayUsingFunction:context:hint:.
- (Array) sortedArrayUsingComparator(cmptr)
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator Block.
- (Array) sortedArrayUsingDescriptors(sortDescriptors)
Returns a copy of the receiving array sorted as specified by a given array of sort descriptors. The first descriptor specifies the primary key path to be used in sorting the receiving array’s contents. Any subsequent descriptors are used to further refine sorting of objects with duplicate values. See NSSortDescriptor for additional information.
- (Array) sortedArrayUsingFunction(comparator, context:context)
Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator. The new array contains references to the receiving array’s elements, not copies of them.The comparison function is used to compare two elements at a time and 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. Each time the comparison function is called, it’s passed context as its third argument. This allows the comparison to be based on some outside parameter, such as whether character sorting is case-sensitive or case-insensitive.Given anArray (an array of NSNumber objects) and a comparison function of this type: A sorted version of anArray is created in this way:
- (Array) sortedArrayUsingFunction(comparator, context:context, hint:hint)
Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator. The new array contains references to the receiving array’s elements, not copies of them.This method is similar to sortedArrayUsingFunction:context:, except that it uses the supplied hint to speed the sorting process. When you know the array is nearly sorted, this method is faster than sortedArrayUsingFunction:context:. If you sorted a large array (N entries) once, and you don’t change it much (P additions and deletions, where P is much smaller than N), then you can reuse the work you did in the original sort by conceptually doing a merge sort between the N “old” items and the P “new” items.To obtain an appropriate hint, use sortedArrayHint. You should obtain this hint when the original array has been sorted, and keep hold of it until you need it, after the array has been modified. The hint is computed by sortedArrayHint in O(N) (where N is the number of items). This assumes that items in the array implement a -hash method. Given a suitable hint, and assuming that the hash function is a “good” hash function, -sortedArrayUsingFunction:context:hint: sorts the array in O(P*LOG(P)+N) where P is the number of adds or deletes. This is an improvement over the un-hinted sort, O(N*LOG(N)), when P is small.The hint is simply an array of size N containing the N hashes. To re-sort you need internally to create a map table mapping a hash to the index. Using this map table on the new array, you can get a first guess for the indices, and then sort that. For example, a sorted array B, D, E, F with corresponding hash values 96, 78, 32, 17, may be subject to small changes that result in contents A, C, B, F. The mapping table maps the hashes 96, 78, 32, 17 to the indices #1, #2, #3, #4. If the hashes for A, C, B, F are 25, 99, 96, 17, then by using the mapping table you can get a first order sort #0, ?, #1, #4, so therefore create an initial semi-sorted array B, E, F, and then perform a cheap merge sort with C that yields B, C, E, F.
- (Array) sortedArrayUsingSelector(comparator)
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given selector. The new array contains references to the receiving array’s elements, not copies of them.The comparator message is sent to each object in the array and has as its single argument another object in the array. For example, an array of NSString objects can be sorted by using the caseInsensitiveCompare: method declared in the NSString class. Assuming anArray exists, a sorted version of the array can be created in this way:
- (Array) sortedArrayWithOptions(opts, usingComparator:cmptr)
Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator Block.
- (Array) subarrayWithRange(range)
Returns a new array containing the receiving array’s elements that fall within the limits specified by a given range. If range isn’t within the receiving array’s range of elements, an NSRangeException is raised.For example, the following code example creates an array containing the elements found in the first half of wholeArray (assuming wholeArray exists).
- (Object) valueForKey(key)
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.
The returned array contains NSNull elements for each object that returns nil.
- (Boolean) writeToFile(path, atomically:flag)
Writes the contents of the array to a file at a given path. If the array’s contents are all property list objects (NSString, NSData, NSArray, or NSDictionary objects), the file written by this method can be used to initialize a new array with the class method arrayWithContentsOfFile: or the instance method initWithContentsOfFile:. This method recursively validates that all the contained objects are property list objects before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.
- (Boolean) writeToURL(aURL, atomically:flag)
Writes the contents of the array to the location specified by a given URL. If the array’s contents are all property list objects (NSString, NSData, NSArray, or NSDictionary objects), the location written by this method can be used to initialize a new array with the class method arrayWithContentsOfURL: or the instance method initWithContentsOfURL:.