Flipkart Search

Search This Blog

Friday, September 24, 2010

Reverse objects of an NSArray/NSMutableArray easily

Here is an example of reversing a NSArray/NSMutableArray

    NSString *string = @"a,b,c,d,e,f,g,h,i,j,k";
    NSArray *array = [string componentsSeparatedByString:@","];
    NSLog(@"array = %@",array);
    NSArray* reversedArray = [[array reverseObjectEnumerator] allObjects];
    NSLog(@"Reverse array = %@",reversedArray);

Happy coding all iPhone coderz


Anonymous said...

You cant use this with NSMutableArray!
NSArray and NSMutableArray are different.

Anonymous said...

@Anonymous: NSMutableArray is a subclass of NSArray, so it works with both. In both cases it creates a new array and does not affect the original.

Anonymous said...

Tried reversing an array of dictionaries, and it creates a reversed array of arrays

tarun kumar said...

For NSMutableArray.

NSArray*Arry=[[self.xAxisArrayForPlot reverseObjectEnumerator]allObjects];

NSMutableArray *xAxesArray = [NSMutableArray arrayWithArray:Arry];