精华内容
下载资源
问答
  • NSArray

    2019-10-04 23:55:15
    获取NSArray中对象的个数 - (id)objectAtIndex:(NSUInteger)index; 获取NSArray中index位置的对象 - (NSArray*)arrayByAddingObject:(id)anObject; 把anObject添加到数组的末尾 - (...

    - (NSUInteger)count;

     

    获取NSArray中对象的个数

     

    - (id)objectAtIndex:(NSUInteger)index;

     

    获取NSArray中index位置的对象

     

    - (NSArray *)arrayByAddingObject:(id)anObject;

     

    把anObject添加到数组的末尾
     

    - (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;

     

    把otherArray数组中的对象添加盗数组的末尾

     

     

     - (NSString *)componentsJoinedByString:(NSString *)separator;

     

    把数组中的元素以separator为分割拼接起来组成一个NSString     

     

     

    - (BOOL)containsObject:(id)anObject;

     

     

    判断anObject是否在数组中     

     

    - (NSString *)description;

     

    对数组中对象的描述,可以输出数组中的对象(待进一步确定)    

     

     

     - (NSString *)descriptionWithLocale:(id)locale;

    - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;

     

    - (id)firstObjectCommonWithArray:(NSArray *)otherArray;

    返回NSArray和otherArray中第一个相同的对象

     

     

     

     

     

    + (id)arrayWithObjects:(constid [])objects count:(NSUInteger)cnt;

     
     
     
    1. NSString *string[5];  
    2. string[0] = @"First";  
    3. string[1] = @"Second";  
    4. string[2] = @"Third";  
    5. string[3] = @"Forth";  
    6.       

     

    1. NSArray *array = [NSArray arrayWithObjects:string count:3];  

     

    1. 输出结果:First, Second, Third  

     

     

     

     

    - (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range;

     

     

    从数组objects中取出对应NSRange的数据,range中的数据表示从range.location开始到range.length的元素
     
     

     
    1. NSArray *mArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];  

     

    1. id *objects;  

     

    1. NSRange range = NSMakeRange(2, 3);  

     

    1. objects = malloc(sizeof(id) * range.length);  

     

    1. [mArray getObjects:objects range:range];  

     

    1. for (NSInteger i = 0; i < range.length; i++)  

     

    1. {  
    2.     NSLog(@"objects: %@", objects[i]);  

     

    1. }  
    2. free(objects);  

     

    - (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;

     

     

    在整个数组中,在range范围内的anObject对象的索引位置

     

    - (NSEnumerator *)objectEnumerator;

    顺序获取数组的NSEnumerator

     

    - (NSEnumerator *)reverseObjectEnumerator;

     

    倒序获取数组的NSEnumerator

     

    [plain] view plain copy
     
    1. NSEnumerator *enumerator = [mArray objectEnumerator];  
    2.   
    3. id object;  

     

    1. while((object = [enumerator nextObject]))  
    2. {  
    3.     NSLog(@"object:%@",object);  
    4. }  

    转载于:https://www.cnblogs.com/LiLihongqiang/p/5543235.html

    展开全文
  • NSARRAY

    2014-09-18 14:42:27
    NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can u

    Next

    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.

    NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef. See “Toll-Free Bridging” for more information on toll-free bridging.

    Subclassing Notes


    There is typically little reason to subclass NSArray. The class does well what it is designed to do—maintain an ordered collection of objects. But there are situations where a custom NSArray object might come in handy. Here are a few possibilities:

    • Changing how NSArray stores the elements of its collection. You might do this for performance reasons or for better compatibility with legacy code.
    • Acquiring more information about what is happening to the collection (for example, statistics gathering).

    Methods to Override

    Any subclass of NSArray must override the primitive instance methods count and objectAtIndex:. These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism. You may also choose to override, partially or fully, any other NSArray method for which you want to provide an alternative implementation.

    You might want to implement an initializer for your subclass that is suited to the backing store that the subclass is managing. If you do, your initializer must invoke one of the designated initializers of the NSArray class, either init or initWithObjects:count:. The NSArray class adopts the NSCopying, NSMutableCopying, and NSCoding protocols; if you want instances of your own custom subclass created from copying or coding, override the methods in these protocols.

    Remember that NSArray is the public interface for a class cluster and what this entails for your subclass. You must provide the storage for your subclass and implement the primitive methods that directly act on that storage.

    Alternatives to Subclassing

    Before making a custom class of NSArray, investigate NSPointerArray and the corresponding Core Foundation type, CFArray Reference. Because NSArray and CFArray are “toll-free bridged,” you can substitute a CFArray object for a NSArray object in your code (with appropriate casting). Although they are corresponding types, CFArray and NSArray do not have identical interfaces or implementations, and you can sometimes do things with CFArray that you cannot easily do with NSArray. For example, CFArray provides a set of callbacks, some of which are for implementing custom retain-release behavior. If you specify NULL implementations for these callbacks, you can easily get a non-retaining array.

    If the behavior you want to add supplements that of the existing class, you could write a category on NSArray. Keep in mind, however, that this category will be in effect for all instances of NSArray that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.

    Adopted Protocols


    NSCoding

    NSCopying

    NSMutableCopying

    NSFastEnumeration

    Tasks


    Creating an Array


    • + array
    • + arrayWithArray:
    • + arrayWithContentsOfFile:
    • + arrayWithContentsOfURL:
    • + arrayWithObject:
    • + arrayWithObjects:
    • + arrayWithObjects:count:

    Initializing an Array


    • – init
    • – initWithArray:
    • – initWithArray:copyItems:
    • – initWithContentsOfFile:
    • – initWithContentsOfURL:
    • – initWithObjects:
    • – initWithObjects:count:

    Querying an Array


    • – containsObject:
    • – count
    • – getObjects:range:
    • – firstObject
    • – lastObject
    • – objectAtIndex:
    • – objectAtIndexedSubscript:
    • – objectsAtIndexes:
    • – objectEnumerator
    • – reverseObjectEnumerator
    • – getObjects: Deprecated in OS X v10.6

    Finding Objects in an Array


    • – indexOfObject:
    • – indexOfObject:inRange:
    • – indexOfObjectIdenticalTo:
    • – indexOfObjectIdenticalTo:inRange:
    • – indexOfObjectPassingTest:
    • – indexOfObjectWithOptions:passingTest:
    • – indexOfObjectAtIndexes:options:passingTest:
    • – indexesOfObjectsPassingTest:
    • – indexesOfObjectsWithOptions:passingTest:
    • – indexesOfObjectsAtIndexes:options:passingTest:
    • – indexOfObject:inSortedRange:options:usingComparator:

    Sending Messages to Elements


    • – makeObjectsPerformSelector:
    • – makeObjectsPerformSelector:withObject:
    • – enumerateObjectsUsingBlock:
    • – enumerateObjectsWithOptions:usingBlock:
    • – enumerateObjectsAtIndexes:options:usingBlock:

    Comparing Arrays


    • – firstObjectCommonWithArray:
    • – isEqualToArray:

    Deriving New Arrays


    • – arrayByAddingObject:
    • – arrayByAddingObjectsFromArray:
    • – filteredArrayUsingPredicate:
    • – subarrayWithRange:

    Sorting


    • – sortedArrayHint
    • – sortedArrayUsingFunction:context:
    • – sortedArrayUsingFunction:context:hint:
    • – sortedArrayUsingDescriptors:
    • – sortedArrayUsingSelector:
    • – sortedArrayUsingComparator:
    • – sortedArrayWithOptions:usingComparator:

    Working with String Elements


    • – componentsJoinedByString:

    Creating a Description


    • – description
    • – descriptionWithLocale:
    • – descriptionWithLocale:indent:
    • – writeToFile:atomically:
    • – writeToURL:atomically:

    Collecting Paths


    • – pathsMatchingExtensions:

    Key-Value Observing


    • – addObserver:forKeyPath:options:context:
    • – removeObserver:forKeyPath:
    • – removeObserver:forKeyPath:context:
    • – removeObserver:fromObjectsAtIndexes:forKeyPath:context:
    • – addObserver:toObjectsAtIndexes:forKeyPath:options:context:
    • – removeObserver:fromObjectsAtIndexes:forKeyPath:

    Key-Value Coding


    • – setValue:forKey:
    • – valueForKey:

    Class Methods

    array


    Creates and returns an empty array.

    + (instancetype)array

    Return Value

    An empty array.

    Discussion

    This method is used by mutable subclasses of NSArray.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithObject:
    • + arrayWithObjects:

    Related Sample Code

    Declared In

    NSArray.h

    arrayWithArray:


    Creates and returns an array containing the objects in another given array.

    + (instancetype)arrayWithArray:(NSArray *)anArray

    Parameters

    anArray

    An array.

    Return Value

    An array containing the objects in anArray.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithObjects:
    • – initWithObjects:

    Related Sample Code

    Declared In

    NSArray.h

    arrayWithContentsOfFile:


    Creates and returns an array containing the contents of the file specified by a given path.

    + (id)arrayWithContentsOfFile:(NSString *)aPath

    Parameters

    aPath

    The path to a file containing a string representation of an array produced by the writeToFile:atomically: method.

    Return Value

    An array containing the contents of the file specified by aPath. Returns nil if the file can’t be opened or if the contents of the file can’t be parsed into an array.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – writeToFile:atomically:

    Related Sample Code

    Declared In

    NSArray.h

    arrayWithContentsOfURL:


    Creates and returns an array containing the contents specified by a given URL.

    + (id)arrayWithContentsOfURL:(NSURL *)aURL

    Parameters

    aURL

    The location of a file containing a string representation of an array produced by the writeToURL:atomically: method.

    Return Value

    An array containing the contents specified by aURL. Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – writeToURL:atomically:

    Declared In

    NSArray.h

    arrayWithObject:


    Creates and returns an array containing a given object.

    + (instancetype)arrayWithObject:(id)anObject

    Parameters

    anObject

    An object.

    Return Value

    An array containing the single element anObject.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + array
    • + arrayWithObjects:

    Related Sample Code

    Declared In

    NSArray.h

    arrayWithObjects:


    Creates and returns an array containing the objects in the argument list.

    + (instancetype)arrayWithObjects:(id)firstObj,,...

    Parameters

    firstObj, ...

    A comma-separated list of objects ending with nil.

    Return Value

    An array containing the objects in the argument list.

    Discussion

    This code example creates an array containing three different types of element:

    NSArray *myArray;


    NSDate *aDate = [NSDate distantFuture];


    NSValue *aValue = [NSNumber numberWithInt:5];


    NSString *aString = @"a string";


     


    myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];


    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + array
    • + arrayWithObject:

    Related Sample Code

    Declared In

    NSArray.h

    arrayWithObjects:count:


    Creates and returns an array that includes a given number of objects from a given C array.

    + (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)count

    Parameters

    objects

    A C array of objects.

    count

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

    Return Value

    A new array including the first count objects from objects.

    Discussion

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

    NSString *strings[3];


    strings[0] = @"First";


    strings[1] = @"Second";


    strings[2] = @"Third";


     


    NSArray *stringsArray = [NSArray arrayWithObjects:strings count:2];


    // strings array contains { @"First", @"Second" }


    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – getObjects:range:

    Declared In

    NSArray.h

    Instance Methods


    addObserver:forKeyPath:options:context:


    Raises an exception.

    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context

    Parameters

    observer

    The object to register for KVO notifications. The observer must implement the key-value observing method observeValueForKeyPath:ofObject:change:context:.

    keyPath

    The key path, relative to the array, of the property to observe. This value must not be nil.

    options

    A combination of NSKeyValueObservingOptions values that specifies what is included in observation notifications.

    context

    Arbitrary data that is passed to observer in observeValueForKeyPath:ofObject:change:context:.

    Special Considerations

    NSArray objects are not observable, so this method raises an exception when invoked on an NSArray object. Instead of observing an array, observe the to-many relationship for which the array is the collection of related objects.

    Availability

    • Available in OS X v10.4 and later.

    See Also

    • – removeObserver:forKeyPath:
    • – addObserver:toObjectsAtIndexes:forKeyPath:options:context:

    Declared In

    NSKeyValueObserving.h

    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.

    - (void)addObserver:(NSObject *)anObserver toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context

    Parameters

    anObserver

    The observer.

    indexes

    The index set.

    keyPath

    The key path, relative to the array, to be observed.

    options

    The options to be included in the notification.

    context

    The context passed to the notifications.

    Discussion

    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:.

    Availability

    • Available in OS X v10.3 and later.

    See Also

    • – removeObserver:fromObjectsAtIndexes:forKeyPath:

    Related Sample Code

    Declared In

    NSKeyValueObserving.h

    arrayByAddingObject:


    Returns a new array that is a copy of the receiving array with a given object added to the end.

    - (NSArray *)arrayByAddingObject:(id)anObject

    Parameters

    anObject

    An object.

    Return Value

    A new array that is a copy of the receiving array with anObject added to the end.

    Discussion

    If anObject is nil, an NSInvalidArgumentException is raised.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    Related Sample Code

    Declared In

    NSArray.h

    arrayByAddingObjectsFromArray:


    Returns a new array that is a copy of the receiving array with the objects contained in another array added to the end.

    - (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray

    Parameters

    otherArray

    An array.

    Return Value

    A new array that is a copy of the receiving array with the objects contained in otherArray added to the end.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    Related Sample Code

    Declared In

    NSArray.h

    componentsJoinedByString:


    Constructs and returns an NSString object that is the result of interposing a given separator between the elements of the array.

    - (NSString *)componentsJoinedByString:(NSString *)separator

    Parameters

    separator

    The string to interpose between the elements of the array.

    Return Value

    An NSString object that is the result of interposing separator between the elements of the array. If the array has no elements, returns an NSString object representing an empty string.

    Discussion

    For example, this code excerpt writes "here be dragons" to the console:

    NSArray *pathArray = [NSArray arrayWithObjects:@"here", @"be", @"dragons", nil];


    NSLog(@"%@",[pathArray componentsJoinedByString:@" "]);


    Special Considerations

    Each element in the array must handle description.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    Related Sample Code

    Declared In

    NSArray.h

    containsObject:


    Returns a Boolean value that indicates whether a given object is present in the array.

    - (BOOL)containsObject:(id)anObject

    Parameters

    anObject

    An object.

    Return Value

    YES if anObject is present in the array, otherwise NO.

    Discussion

    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).

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – indexOfObject:
    • – indexOfObjectIdenticalTo:

    Related Sample Code

    Declared In

    NSArray.h

    count


    Returns the number of objects currently in the array.

    - (NSUInteger)count

    Return Value

    The number of objects currently in the array.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – objectAtIndex:

    Related Sample Code

    Declared In

    NSArray.h

    description


    Returns a string that represents the contents of the array, formatted as a property list.

    - (NSString *)description

    Return Value

    A string that represents the contents of the array, formatted as a property list.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – descriptionWithLocale:
    • – descriptionWithLocale:indent:

    Declared In

    NSArray.h

    descriptionWithLocale:


    Returns a string that represents the contents of the array, formatted as a property list.

    - (NSString *)descriptionWithLocale:(id)locale

    Parameters

    locale

    An NSLocale object or an NSDictionary object that specifies options used for formatting each of the array’s elements (where recognized). Specify nil if you don’t want the elements formatted.

    Return Value

    A string that represents the contents of the array, formatted as a property list.

    Discussion

    For a description of how locale is applied to each element in the receiving array, see descriptionWithLocale:indent:.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – description
    • – descriptionWithLocale:indent:

    Declared In

    NSArray.h

    descriptionWithLocale:indent:


    Returns a string that represents the contents of the array, formatted as a property list.

    - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level

    Parameters

    locale

    An NSLocale object or an NSDictionary object that specifies options used for formatting each of the array’s elements (where recognized). Specify nil if you don’t want the elements formatted.

    level

    A level of indent, to make the output more readable: set level to 0 to use four spaces to indent, or 1 to indent the output with a tab character.

    Return Value

    A string that represents the contents of the array, formatted as a property list.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – description
    • – descriptionWithLocale:

    Declared In

    NSArray.h

    enumerateObjectsAtIndexes:options:usingBlock:


    Executes a given block using the objects in the array at the specified indexes.

    - (void)enumerateObjectsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

    Parameters

    indexSet

    The indexes of the objects over which to enumerate.

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    block

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    Discussion

    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.

    This method executes synchronously.

    Important: If the Block parameter or the indexSet is nil this method will raise an exception.


    Availability

    • Available in OS X v10.6 and later.

    See Also

    • – enumerateObjectsUsingBlock:
    • – makeObjectsPerformSelector:
    • – makeObjectsPerformSelector:withObject:

    Declared In

    NSArray.h

    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.

    - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

    Parameters

    block

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    Discussion

    If the Block parameter is nil this method will raise an exception.

    This method executes synchronously.

    Availability

    • Available in OS X v10.6 and later.

    See Also

    • – enumerateObjectsWithOptions:usingBlock:
    • – makeObjectsPerformSelector:
    • – makeObjectsPerformSelector:withObject:

    Related Sample Code

    Declared In

    NSArray.h

    enumerateObjectsWithOptions:usingBlock:


    Executes a given block using each object in the array.

    - (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block

    Parameters

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    block

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    Discussion

    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.

    This method executes synchronously.

    Important: If the Block parameter is nil this method will raise an exception.


    Availability

    • Available in OS X v10.6 and later.

    See Also

    • – enumerateObjectsUsingBlock:
    • – makeObjectsPerformSelector:
    • – makeObjectsPerformSelector:withObject:

    Declared In

    NSArray.h

    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.

    - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

    Parameters

    predicate

    The predicate against which to evaluate the receiving array’s elements.

    Return Value

    A new array containing the objects in the receiving array for which predicate returns true.

    Discussion

    For more details, see Predicate Programming Guide.

    Availability

    • Available in OS X v10.4 and later.

    Declared In

    NSPredicate.h

    firstObject


    Returns the first object in the array.

    - (id)firstObject

    Return Value

    The first object in the array. If the array is empty, returns nil.

    Availability

    • Available in OS X v10.6 and later.

    See Also

    • – lastObject

    Declared In

    NSArray.h

    firstObjectCommonWithArray:


    Returns the first object contained in the receiving array that’s equal to an object in another given array.

    - (id)firstObjectCommonWithArray:(NSArray *)otherArray

    Parameters

    otherArray

    An array.

    Return Value

    Returns the first object contained in the receiving array that’s equal to an object in otherArray. If no such object is found, returns nil.

    Discussion

    This method uses isEqual: to check for object equality.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – containsObject:

    Declared In

    NSArray.h

    getObjects:range:


    Copies the objects contained in the array that fall within the specified range to aBuffer.

    - (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range

    Parameters

    aBuffer

    A C array of objects of size at least the length of the range specified by aRange.

    aRange

    A range within the bounds of the array.

    If the location plus the length of the range is greater than the count of the array, this method raises an NSRangeException.

    Discussion

    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):

    NSArray *mArray = // an array with at least six elements...;


    id *objects;


     


    NSRange range = NSMakeRange(2, 4);


    objects = malloc(sizeof(id) * range.length);


     


    [mArray getObjects:objects range:range];


     


    for (i = 0; i < range.length; i++) {


        NSLog(@"objects: %@", objects[i]);


    }


    free(objects);


    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithObjects:count:

    Declared In

    NSArray.h

    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.

    - (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    indexSet

    The indexes of the objects over which to enumerate.

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test.

    Return Value

    The indexes whose corresponding values in the array pass the test specified by predicate. If no objects in the array pass the test, returns an empty index set.

    Discussion

    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.


    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    indexesOfObjectsPassingTest:


    Returns the indexes of objects in the array that pass a test in a given Block.

    - (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test.

    Return Value

    The indexes whose corresponding values in the array pass the test specified by predicate. If no objects in the array pass the test, returns an empty index set.

    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    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.

    - (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test.

    Return Value

    The indexes whose corresponding values in the array pass the test specified by predicate. If no objects in the array pass the test, returns an empty index set.

    Discussion

    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.


    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    indexOfObject:


    Returns the lowest index whose corresponding array value is equal to a given object.

    - (NSUInteger)indexOfObject:(id)anObject

    Parameters

    anObject

    An object.

    Return Value

    The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – containsObject:
    • – indexOfObjectIdenticalTo:

    Related Sample Code

    Declared In

    NSArray.h

    indexOfObject:inRange:


    Returns the lowest index within a specified range whose corresponding array value is equal to a given object .

    - (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range

    Parameters

    anObject

    An object.

    range

    The range of indexes in the array within which to search for anObject.

    Return Value

    The lowest index within range whose corresponding array value is equal to anObject. If none of the objects within range is equal to anObject, returns NSNotFound.

    Discussion

    Starting at range.location, each element of the array is sent an isEqual: message until a match is found or the end of the range 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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – containsObject:
    • – indexOfObjectIdenticalTo:inRange:

    Declared In

    NSArray.h

    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.

    - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp

    Parameters

    obj

    An object for which to search in the array.

    If this value is nil, throws an NSInvalidArgumentException.

    r

    The range within the array to search for obj.

    If r exceeds the bounds of the array (if the location plus length of the range is greater than the count of the array), throws an NSRangeException.

    opts

    Options for the search. For possible values, see “NSBinarySearchingOptions.”

    If you specify both NSBinarySearchingFirstEqual and NSBinarySearchingLastEqual, throws an NSInvalidArgumentException.

    cmp

    A comparator block used to compare the object obj with elements in the array.

    If this value is NULL, throws an NSInvalidArgumentException.

    Return Value

    If the NSBinarySearchingInsertionIndex option is not specified:

    • If the obj is found and neither NSBinarySearchingFirstEqual nor NSBinarySearchingLastEqual is specified, returns an arbitrary matching object's index.
    • If the NSBinarySearchingFirstEqual option is also specified, returns the lowest index of equal objects.
    • If the NSBinarySearchingLastEqual option is also specified, returns the highest index of equal objects.
    • If the object is not found, returns NSNotFound.

    If the NSBinarySearchingInsertionIndex option is specified, returns the index at which you should insert obj in order to maintain a sorted array:

    • If the obj is found and neither NSBinarySearchingFirstEqual nor NSBinarySearchingLastEqual is specified, returns any equal or one larger index than any matching object’s index.
    • If the NSBinarySearchingFirstEqual option is also specified, returns the lowest index of equal objects.
    • If the NSBinarySearchingLastEqual option is also specified, returns the highest index of equal objects.
    • If the object is not found, returns the index of the least greater object, or the index at the end of the array if the object is larger than all other elements.

    Special Considerations

    The elements in the array must have already been sorted using the comparator cmp. If the array is not sorted, the result is undefined.

    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    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.

    - (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    indexSet

    The indexes of the objects over which to enumerate.

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test.

    Return Value

    The lowest index whose corresponding value in the array passes the test specified by predicate. If no objects in the array pass the test, returns NSNotFound.

    Discussion

    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.


    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    indexOfObjectIdenticalTo:


    Returns the lowest index whose corresponding array value is identical to a given object.

    - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject

    Parameters

    anObject

    An object.

    Return Value

    The lowest index whose corresponding array value is identical to anObject. If none of the objects in the array is identical to anObject, returns NSNotFound.

    Discussion

    Objects are considered identical if their object addresses are the same.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – containsObject:
    • – indexOfObject:

    Related Sample Code

    Declared In

    NSArray.h

    indexOfObjectIdenticalTo:inRange:


    Returns the lowest index within a specified range whose corresponding array value is equal to a given object .

    - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range

    Parameters

    anObject

    An object.

    range

    The range of indexes in the array within which to search for anObject.

    Return Value

    The lowest index within range whose corresponding array value is identical to anObject. If none of the objects within range is identical to anObject, returns NSNotFound.

    Discussion

    Objects are considered identical if their object addresses are the same.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – containsObject:
    • – indexOfObject:inRange:

    Declared In

    NSArray.h

    indexOfObjectPassingTest:


    Returns the index of the first object in the array that passes a test in a given Block.

    - (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test. Returning YES will stop further processing of the array.

    Return Value

    The lowest index whose corresponding value in the array passes the test specified by predicate. If no objects in the array pass the test, returns NSNotFound.

    Discussion

    If the Block parameter is nil this method will raise an exception.

    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    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.

    - (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

    Parameters

    opts

    A bit mask that specifies the options for the enumeration (whether it should be performed concurrently and whether it should be performed in reverse order).

    predicate

    The block to apply to elements in the array.

    The block takes three arguments:

    obj

    The element in the array.

    idx

    The index of the element in the array.

    stop

    A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block.

    The Block returns a Boolean value that indicates whether obj passed the test.

    Return Value

    The index whose corresponding value in the array passes the test specified by predicate and opts. If the opts bit mask specifies reverse order, then the last item that matches is returned. Otherwise, the index of the first matching object is returned. If no objects in the array pass the test, returns NSNotFound.

    Discussion

    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.


    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    init


    Initializes a newly allocated array.

    - (instancetype)init

    Return Value

    An array.

    Discussion

    After an immutable array has been initialized in this way, it cannot be modified.

    This method is a designated initializer.

    Availability

    • Available in OS X v10.9 and later.

    See Also

    • – initWithObjects:

    Declared In

    NSArray.h

    initWithArray:


    Initializes a newly allocated array by placing in it the objects contained in a given array.

    - (instancetype)initWithArray:(NSArray *)anArray

    Parameters

    anArray

    An array.

    Return Value

    An array initialized to contain the objects in anArray. The returned object might be different than the original receiver.

    Discussion

    After an immutable array has been initialized in this way, it cannot be modified.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithObject:
    • – initWithObjects:

    Declared In

    NSArray.h

    initWithArray:copyItems:


    Initializes a newly allocated array using anArray as the source of data objects for the array.

    - (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag

    Parameters

    array

    An array containing the objects with which to initialize the new array.

    flag

    If YES, each object in array receives a copyWithZone: message to create a copy of the object—objects must conform to the NSCopying protocol. In a managed memory environment, this is instead of the retain message the object would otherwise receive. The object copy is then added to the returned array.

    If NO, then in a managed memory environment each object in array simply receives a retain message when it is added to the returned array.

    Return Value

    An array initialized to contain the objects—or if flag is YES, copies of the objects—in array. The returned object might be different than the original receiver.

    Discussion

    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.

    Availability

    • Available in OS X v10.2 and later.

    See Also

    • – init
    • – initWithArray:
    • + arrayWithObject:
    • – initWithObjects:

    Declared In

    NSArray.h

    initWithContentsOfFile:


    Initializes a newly allocated array with the contents of the file specified by a given path.

    - (id)initWithContentsOfFile:(NSString *)aPath

    Parameters

    aPath

    The path to a file containing a representation of an array produced by the writeToFile:atomically: method.

    Return Value

    An array initialized to contain the contents of the file specified by aPath or nil if the file can’t be opened or the contents of the file can’t be parsed into an array. The returned object might be different than the original receiver.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithContentsOfFile:
    • – writeToFile:atomically:

    Declared In

    NSArray.h

    initWithContentsOfURL:


    Initializes a newly allocated array with the contents of the location specified by a given URL.

    - (id)initWithContentsOfURL:(NSURL *)aURL

    Parameters

    aURL

    The location of a file containing a string representation of an array produced by the writeToURL:atomically: method.

    Return Value

    An array initialized to contain the contents specified by aURL. Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array. The returned object might be different than the original receiver.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • + arrayWithContentsOfURL:
    • – writeToURL:atomically:

    Declared In

    NSArray.h

    initWithObjects:


    Initializes a newly allocated array by placing in it the objects in the argument list.

    - (instancetype)initWithObjects:(id)firstObj,,...

    Parameters

    firstObj, ...

    A comma-separated list of objects ending with nil.

    Return Value

    An array initialized to include the objects in the argument list. The returned object might be different than the original receiver.

    Discussion

    After an immutable array has been initialized in this way, it can’t be modified.

    This method is a designated initializer.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – init
    • – initWithObjects:count:
    • + arrayWithObjects:
    • – initWithArray:

    Declared In

    NSArray.h

    initWithObjects:count:


    Initializes a newly allocated array to include a given number of objects from a given C array.

    - (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)count

    Parameters

    objects

    A C array of objects.

    count

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

    Return Value

    A newly allocated array including the first count objects from objects. The returned object might be different than the original receiver.

    Discussion

    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.

    This method is a designated initializer.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – initWithObjects:
    • + arrayWithObjects:
    • – initWithArray:

    Declared In

    NSArray.h

    isEqualToArray:


    Compares the receiving array to another array.

    - (BOOL)isEqualToArray:(NSArray *)otherArray

    Parameters

    otherArray

    An array.

    Return Value

    YES if the contents of otherArray are equal to the contents of the receiving array, otherwise NO.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    Declared In

    NSArray.h

    lastObject


    Returns the last object in the array.

    - (id)lastObject

    Return Value

    The last object in the array. If the array is empty, returns nil.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    Related Sample Code

    Declared In

    NSArray.h

    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.

    - (void)makeObjectsPerformSelector:(SEL)aSelector

    Parameters

    aSelector

    A selector that identifies the message to send to the objects in the array. The method must not take any arguments, and must not have the side effect of modifying the receiving array.

    Discussion

    This method raises an NSInvalidArgumentException if aSelector is NULL.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – makeObjectsPerformSelector:withObject:

    Related Sample Code

    Declared In

    NSArray.h

    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.

    - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)anObject

    Parameters

    aSelector

    A selector that identifies the message to send to the objects in the array. The method must take a single argument of type id, and must not have the side effect of modifying the receiving array.

    anObject

    The object to send as the argument to each invocation of the aSelector method.

    Discussion

    This method raises an NSInvalidArgumentException if aSelector is NULL.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – makeObjectsPerformSelector:

    Related Sample Code

    Declared In

    NSArray.h

    objectAtIndex:


    Returns the object located at the specified index.

    - (id)objectAtIndex:(NSUInteger)index

    Parameters

    index

    An index within the bounds of the array.

    Return Value

    The object located at index.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – count
    • – objectsAtIndexes:
    • – objectAtIndexedSubscript:

    Related Sample Code

    Declared In

    NSArray.h

    objectAtIndexedSubscript:


    Returns the object at the specified index.

    - (id)objectAtIndexedSubscript:(NSUInteger)idx

    Parameters

    idx

    An index within the bounds of the array.

    Return Value

    The object located at index.

    Discussion

    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:.

    Availability

    • Available in OS X v10.8 and later.

    See Also

    • – count
    • – objectsAtIndexes:

    Declared In

    NSArray.h

    objectEnumerator


    Returns an enumerator object that lets you access each object in the array.

    - (NSEnumerator *)objectEnumerator

    Return Value

    An enumerator object that lets you access each object in the array, in order, from the element at the lowest index upwards.

    Discussion

    Returns an enumerator object that lets you access each object in the array, in order, starting with the element at index 0, as in:

    NSEnumerator *enumerator = [myArray objectEnumerator];


    id anObject;


     


    while (anObject = [enumerator nextObject]) {


        /* code to act on each element as it is returned */


    }


    Special Considerations

    When you use this method with mutable subclasses of NSArray, you must not modify the array during enumeration.

    It is more efficient to use the fast enumeration protocol (see NSFastEnumeration). Fast enumeration is available on OS X v10.5 and later and iOS 2.0 and later.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – reverseObjectEnumerator
    • nextObject (NSEnumerator)

    Related Sample Code

    Declared In

    NSArray.h

    objectsAtIndexes:


    Returns an array containing the objects in the array at the indexes specified by a given index set.

    - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes

    Return Value

    An array containing the objects in the array at the indexes specified by indexes.

    Discussion

    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, indexes is nil.

    Availability

    • Available in OS X v10.4 and later.

    See Also

    • – count
    • – objectAtIndex:

    Related Sample Code

    Declared In

    NSArray.h

    pathsMatchingExtensions:


    Returns an array containing all the pathname elements in the receiving array that have filename extensions from a given array.

    - (NSArray *)pathsMatchingExtensions:(NSArray *)filterTypes

    Parameters

    filterTypes

    An array of NSString objects containing filename extensions. The extensions should not include the dot (“.”) character.

    Return Value

    An array containing all the pathname elements in the receiving array that have filename extensions from the filterTypes array.

    Availability

    • Available in OS X v10.0 and later.

    Declared In

    NSPathUtilities.h

    removeObserver:forKeyPath:


    Raises an exception.

    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath

    Parameters

    observer

    The object to remove as an observer.

    keyPath

    A key-path, relative to the array, for which observer is registered to receive KVO change notifications. This value must not be nil.

    Special Considerations

    NSArray objects are not observable, so this method raises an exception when invoked on an NSArray object. Instead of observing an array, observe the to-many relationship for which the array is the collection of related objects.

    Availability

    • Available in OS X v10.4 and later.

    See Also

    • – addObserver:forKeyPath:options:context:
    • – removeObserver:fromObjectsAtIndexes:forKeyPath:

    Declared In

    NSKeyValueObserving.h

    removeObserver:forKeyPath:context:


    Raises an exception.

    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context

    Parameters

    observer

    The object to remove as an observer.

    keyPath

    A key-path, relative to the set, for which observer is registered to receive KVO change notifications. This value must not be nil.

    context

    The context passed to the notifications.

    Special Considerations

    NSArray objects are not observable, so this method raises an exception when invoked on an NSArray object. Instead of observing a array, observe the ordered to-many relationship for which the array is the collection of related objects.

    Availability

    • Available in OS X v10.7 and later.

    See Also

    • – addObserver:forKeyPath:options:context:
    • – removeObserver:forKeyPath:

    Declared In

    NSKeyValueObserving.h

    removeObserver:fromObjectsAtIndexes:forKeyPath:


    Removes anObserver from all key value observer notifications associated with the specified keyPath relative to the array’s objects at indexes.

    - (void)removeObserver:(NSObject *)anObserver fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath

    Parameters

    anObserver

    The observer.

    indexes

    The index set.

    keyPath

    The key path, relative to the array, to be observed.

    Discussion

    This is not merely a convenience method; invoking this method is potentially much faster than repeatedly invoking removeObserver:forKeyPath:.

    Availability

    • Available in OS X v10.3 and later.

    See Also

    • – addObserver:toObjectsAtIndexes:forKeyPath:options:context:

    Related Sample Code

    Declared In

    NSKeyValueObserving.h

    removeObserver:fromObjectsAtIndexes:forKeyPath:context:


    Raises an exception.

    - (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath context:(void *)context

    Parameters

    observer

    The object to remove as an observer.

    keyPath

    A key-path, relative to the array, for which observer is registered to receive KVO change notifications. This value must not be nil.

    context

    The context passed to the notifications.

    Special Considerations

    NSArray objects are not observable, so this method raises an exception when invoked on an NSArray object. Instead of observing an array, observe the to-many relationship for which the array is the collection of related objects.

    Availability

    • Available in OS X v10.7 and later.

    See Also

    • – addObserver:forKeyPath:options:context:
    • – removeObserver:fromObjectsAtIndexes:forKeyPath:

    Declared In

    NSKeyValueObserving.h

    reverseObjectEnumerator


    Returns an enumerator object that lets you access each object in the array, in reverse order.

    - (NSEnumerator *)reverseObjectEnumerator

    Return Value

    An enumerator object that lets you access each object in the array, in order, from the element at the highest index down to the element at index 0.

    Special Considerations

    When you use this method with mutable subclasses of NSArray, you must not modify the array during enumeration.

    It is more efficient to use the fast enumeration protocol (see NSFastEnumeration). Fast enumeration is available on OS X v10.5 and later and iOS 2.0 and later.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    Related Sample Code

    Declared In

    NSArray.h

    setValue:forKey:


    Invokes setValue:forKey: on each of the array's items using the specified value and key.

    - (void)setValue:(id)value forKey:(NSString *)key

    Parameters

    value

    The object value.

    key

    The key to store the value.

    Availability

    • Available in OS X v10.3 and later.

    See Also

    • – valueForKey:

    Declared In

    NSKeyValueCoding.h

    sortedArrayHint


    Analyzes the array and returns a “hint” that speeds the sorting of the array when the hint is supplied to sortedArrayUsingFunction:context:hint:.

    - (NSData *)sortedArrayHint

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – sortedArrayUsingFunction:context:hint:

    Declared In

    NSArray.h

    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.

    - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr

    Parameters

    cmptr

    A comparator block.

    Return Value

    An array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified cmptr.

    Availability

    • Available in OS X v10.6 and later.

    Related Sample Code

    Declared In

    NSArray.h

    sortedArrayUsingDescriptors:


    Returns a copy of the receiving array sorted as specified by a given array of sort descriptors.

    - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

    Parameters

    sortDescriptors

    An array of NSSortDescriptor objects.

    Return Value

    A copy of the receiving array sorted as specified by sortDescriptors.

    Discussion

    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.

    Availability

    • Available in OS X v10.3 and later.

    See Also

    • – sortedArrayUsingSelector:
    • – sortedArrayUsingFunction:context:
    • – sortedArrayUsingFunction:context:hint:

    Related Sample Code

    Declared In

    NSSortDescriptor.h

    sortedArrayUsingFunction:context:


    Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.

    - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

    Discussion

    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:

    NSInteger intSort(id num1, id num2, void *context)


    {


        int v1 = [num1 intValue];


        int v2 = [num2 intValue];


        if (v1 < v2)


            return NSOrderedAscending;


        else if (v1 > v2)


            return NSOrderedDescending;


        else


            return NSOrderedSame;


    }


    A sorted version of anArray is created in this way:

    NSArray *sortedArray; sortedArray = [anArray sortedArrayUsingFunction:intSort context:NULL];


    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – sortedArrayUsingDescriptors:
    • – sortedArrayUsingFunction:context:hint:
    • – sortedArrayUsingSelector:

    Declared In

    NSArray.h

    sortedArrayUsingFunction:context:hint:


    Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.

    - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint

    Discussion

    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 {A, B, D, E, F} with corresponding hash values {25, 96, 78, 32, 17}, may be subject to small changes that result in contents {E, A, C, B, F}. The mapping table maps the hashes {25, 96, 78, 32, 17} to the indices {#0, #1, #2, #3, #4}. If the hashes for {E, A, C, B, F} are {32, 25, 99, 96, 17}, then by using the mapping table you can get a first order sort {#3, #0, ?, #1, #4}, so therefore create an initial semi-sorted array {A, B, E, F}, and then perform a cheap merge sort with {C} that yields {A, B, C, E, F}.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – sortedArrayUsingDescriptors:
    • – sortedArrayUsingFunction:context:
    • – sortedArrayUsingSelector:

    Declared In

    NSArray.h

    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.

    - (NSArray *)sortedArrayUsingSelector:(SEL)comparator

    Parameters

    comparator

    A selector that identifies the method to use to compare two elements at a time. The method should return NSOrderedAscending if the receiving array is smaller than the argument, NSOrderedDescending if the receiving array is larger than the argument, and NSOrderedSame if they are equal.

    Return Value

    An array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by the selector comparator.

    Discussion

    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:

         NSArray *sortedArray =


             [anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];


    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – sortedArrayUsingDescriptors:
    • – sortedArrayUsingFunction:context:
    • – sortedArrayUsingFunction:context:hint:

    Related Sample Code

    Declared In

    NSArray.h

    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.

    - (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr

    Parameters

    opts

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

    cmptr

    A comparator block.

    Return Value

    An array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified cmptr.

    Availability

    • Available in OS X v10.6 and later.

    Declared In

    NSArray.h

    subarrayWithRange:


    Returns a new array containing the receiving array’s elements that fall within the limits specified by a given range.

    - (NSArray *)subarrayWithRange:(NSRange)range

    Parameters

    range

    A range within the receiving array’s range of elements.

    Return Value

    A new array containing the receiving array’s elements that fall within the limits specified by range.

    Discussion

    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).

    NSArray *halfArray;


    NSRange theRange;


     


    theRange.location = 0;


    theRange.length = [wholeArray count] / 2;


     


    halfArray = [wholeArray subarrayWithRange:theRange];


    Availability

    • Available in OS X v10.0 and later.

    Related Sample Code

    Declared In

    NSArray.h

    valueForKey:


    Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.

    - (id)valueForKey:(NSString *)key

    Parameters

    key

    The key to retrieve.

    Return Value

    The value of the retrieved key.

    Discussion

    The returned array contains NSNull elements for each object that returns nil.

    Availability

    • Available in OS X v10.3 and later.

    See Also

    • – setValue:forKey:

    Declared In

    NSKeyValueCoding.h

    writeToFile:atomically:


    Writes the contents of the array to a file at a given path.

    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

    Parameters

    path

    The path at which to write the contents of the array.

    If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.

    flag

    If YES, the array is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the array is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

    Return Value

    YES if the file is written successfully, otherwise NO.

    Discussion

    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.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – initWithContentsOfFile:

    Declared In

    NSArray.h

    writeToURL:atomically:


    Writes the contents of the array to the location specified by a given URL.

    - (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag

    Parameters

    aURL

    The location at which to write the array.

    flag

    If YES, the array is written to an auxiliary location, and then the auxiliary location is renamed to aURL. If NO, the array is written directly to aURL. The YES option guarantees that aURL, if it exists at all, won’t be corrupted even if the system should crash during writing.

    Return Value

    YES if the location is written successfully, otherwise NO.

    Discussion

    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:.

    Availability

    • Available in OS X v10.0 and later.

    See Also

    • – initWithContentsOfURL:

    Declared In

    NSArray.h

    Constants


    NSBinarySearchingOptions


    Options for searches and insertions using indexOfObject:inSortedRange:options:usingComparator:.

    enum {

       NSBinarySearchingFirstEqual = (1 << 8),

       NSBinarySearchingLastEqual = (1 << 9),

       NSBinarySearchingInsertionIndex = (1 << 10),

    };

    typedef NSUInteger NSBinarySearchingOptions;

    Constants

    NSBinarySearchingFirstEqual

    Specifies that the search should return the first object in the range that is equal to the given object.

    Available in OS X v10.6 and later.

    Declared in NSArray.h.

    NSBinarySearchingLastEqual

    Specifies that the search should return the last object in the range that is equal to the given object.

    Available in OS X v10.6 and later.

    Declared in NSArray.h.

    NSBinarySearchingInsertionIndex

    Returns the index at which you should insert the object in order to maintain a sorted array.

    Available in OS X v10.6 and later.

    Declared in NSArray.h.

    Next




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



    Provide Feedback

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 16,001
精华内容 6,400
关键字:

nsarray