分类声明文件
#import <Foundation/Foundation.h>
@interface NSArray (Reserve)
- (NSArray *)reserve;
@end
分类实现文件
#import "NSArray+Reserve.h"
@implementation NSArray (Reserve)
- (NSArray *)reserve{
NSMutableArray *arr = [NSMutableArray arrayWithArray:self];
for (int i = 0; i < [arr count]/2; i++) {
[arr exchangeObjectAtIndex:i withObjectAtIndex:[arr count] - i - 1];
}
return arr;
}
@end
主函数调用
NSArray *array = [NSMutableArray arrayWithObjects:@"aa",@"ww",@"cc",@"dd",@"bb",nil];
NSArray *newArr = [array reserve];
NSLog(@"%@", newArr);