NSMutableArray 进阶

对于这种基础类型的东西其实没啥好多讲的,自己跳到头文件看看基本就知道怎么用了。几个有点疑问的地方我测试了一下加上了注释。

/****************	MutableArray		****************/

@interface NSMutableArray : NSArray

- (void)addObject:(id)anObject;//LW:add object at the last of the array.
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

@end

@interface NSMutableArray (NSExtendedMutableArray)
    
- (void)addObjectsFromArray:(NSArray *)otherArray;//LW:add otherArray at the end ,the order will not change
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
- (void)removeAllObjects;
- (void)removeObject:(id)anObject inRange:(NSRange)range;
- (void)removeObject:(id)anObject;
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;
- (void)removeObjectIdenticalTo:(id)anObject;
- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;
- (void)setArray:(NSArray *)otherArray;
- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;
- (void)sortUsingSelector:(SEL)comparator;

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;

#if NS_BLOCKS_AVAILABLE
- (void)sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
#endif

@end

@interface NSMutableArray (NSMutableArrayCreation)

+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;

@end

最近项目有个需求,要把一个Array里面的某一个Object移到这个Array的第一个位置,于是写了个Category方法,贴在下面:

//  NSMutableArray+LWUtils.h

#import 

@interface NSMutableArray (LWUtils)
- (void)moveObjectToFirstAtIndex:(NSUInteger)index;
- (void)moveObjectAtIndex:(NSUInteger)idx1 toIndex:(NSUInteger)idx2;
@end

//  NSMutableArray+LWUtils.m

#import "NSMutableArray+LWUtils.h"

@implementation NSMutableArray (LWUtils)
- (void)moveObjectToFirstAtIndex:(NSUInteger)index
{
    [self moveObjectAtIndex:index toIndex:0];
}

- (void)moveObjectAtIndex:(NSUInteger)idx1 toIndex:(NSUInteger)idx2
{
    id object = [self objectAtIndex:idx1];
    [self removeObjectAtIndex:idx1];
    [self insertObject:object atIndex:idx2];
}
@end


你可能感兴趣的:(IOS开发(所有IOS文章),Objective-C)