NSArray reverseObjectEnumerator
- (NSEnumerator *)reverseObjectEnumerator
Return Value of [NSArray reverseObjectEnumerator]
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.
NSArray reverseObjectEnumerator example.
@implementation NSArray (Reverse)
- (NSArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end
- (NSArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end
Example of [NSArray reverseObjectEnumerator].
NSMutableArray * reverseArray = [NSMutableArray arrayWithCapacity:[myArray count]];
for (id element in [myArray reverseObjectEnumerator]) {
[reverseArray addObject:element];
}
for (id element in [myArray reverseObjectEnumerator]) {
[reverseArray addObject:element];
}
NSArray reverseObjectEnumerator example.
NSArray *array2=[myArray mutableCopy];
NSArray* reversed = [[array2 reverseObjectEnumerator] allObjects];
[myarray removeAllObjects];
myarray =[reversed mutableCopy];
NSArray* reversed = [[array2 reverseObjectEnumerator] allObjects];
[myarray removeAllObjects];
myarray =[reversed mutableCopy];