版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.26 |
前言
NSArray
是数组的不变数组类,不边数组在初始化的时候元素就是不变的,不能更改任何一个元素,实际上我们用的较多的是可变数组,因为很多时候我们都需要对数组元素进行增删改查,其中增删改也只有可变数组可以做,也就是说可变数组相对来说更加灵活,这几篇我们就说一下可变数组的这个类及其相关知识,还是老规矩从整体到局部,从浅入深进行讲解,谢谢大家。
整体了解
NSMutableArray
是NSArray
的子类,它是可变的数组,支持我们进行以下操作:
- 增
- 删
- 改
- 查
我们使用可变数组而不是用不可变数组的时候,一般也就是因为可变数组支持的这些特性和操作。
API
我们先看一下苹果给我们的API接口。
/**************** Mutable Array ****************/
@interface NSMutableArray<ObjectType> : NSArray<ObjectType>
- (void)addObject:(ObjectType)anObject;
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCapacity:(NSUInteger)numItems NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
@interface NSMutableArray<ObjectType> (NSExtendedMutableArray)
- (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray;
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
- (void)removeAllObjects;
- (void)removeObject:(ObjectType)anObject inRange:(NSRange)range;
- (void)removeObject:(ObjectType)anObject;
- (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (void)removeObjectIdenticalTo:(ObjectType)anObject;
- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);
- (void)removeObjectsInArray:(NSArray<ObjectType> *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray range:(NSRange)otherRange;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray;
- (void)setArray:(NSArray<ObjectType> *)otherArray;
- (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))compare context:(nullable void *)context;
- (void)sortUsingSelector:(SEL)comparator;
- (void)insertObjects:(NSArray<ObjectType> *)objects atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray<ObjectType> *)objects;
- (void)setObject:(ObjectType)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
- (void)sortUsingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
@end
@interface NSMutableArray<ObjectType> (NSMutableArrayCreation)
+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;
+ (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSMutableArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSMutableArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSMutableArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;
@end
NS_ASSUME_NONNULL_END
大家可以看到,API给出了NSMutableArray
一个本类,还有两个分类,分别是:NSExtendedMutableArray
和NSMutableArrayCreation
。
开发文档
下面我们看一下苹果给我们的开发指导文档。
从上面的开发文档NSMutableArray
的目录就可以看到NSMutableArray
的主要功能增删等等。我们接下来说的也是这些功能。
后记
未完,待续~~~