NSMutableArrayのステップアップ
2978 ワード
このような基礎的なタイプのものについてはあまり話していませんが、自分で頭のファイルに飛び込んで、基本的にどのように使うかを知っています.いくつかの疑問点をテストして注釈をつけました.
最近のプロジェクトでは、Arrayの中のあるObjectをこのArrayの最初の位置に移動する必要があるので、Categoryメソッドを書いて、次のように貼ります.
// NSMutableArray+LWUtils.h
// NSMutableArray+LWUtils.m
/**************** 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 <Foundation/Foundation.h>
@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