NSMutableArray简单细说(一)—— 整体了解

版本记录

版本号 时间
V1.0 2017.08.26

前言

NSArray是数组的不变数组类,不边数组在初始化的时候元素就是不变的,不能更改任何一个元素,实际上我们用的较多的是可变数组,因为很多时候我们都需要对数组元素进行增删改查,其中增删改也只有可变数组可以做,也就是说可变数组相对来说更加灵活,这几篇我们就说一下可变数组的这个类及其相关知识,还是老规矩从整体到局部,从浅入深进行讲解,谢谢大家。

整体了解

NSMutableArrayNSArray的子类,它是可变的数组,支持我们进行以下操作:

我们使用可变数组而不是用不可变数组的时候,一般也就是因为可变数组支持的这些特性和操作。


API

我们先看一下苹果给我们的API接口。

/****************   Mutable Array       ****************/

@interface NSMutableArray : NSArray

- (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 (NSExtendedMutableArray)
    
- (void)addObjectsFromArray:(NSArray *)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 *)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 (NS_NOESCAPE *)(ObjectType,  ObjectType, void * _Nullable))compare context:(nullable void *)context;
- (void)sortUsingSelector:(SEL)comparator;

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)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 (NSMutableArrayCreation)

+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;

+ (nullable NSMutableArray *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSMutableArray *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSMutableArray *)initWithContentsOfFile:(NSString *)path;
- (nullable NSMutableArray *)initWithContentsOfURL:(NSURL *)url;

@end

NS_ASSUME_NONNULL_END

大家可以看到,API给出了NSMutableArray一个本类,还有两个分类,分别是:NSExtendedMutableArrayNSMutableArrayCreation


开发文档

下面我们看一下苹果给我们的开发指导文档。

NSMutableArray简单细说(一)—— 整体了解_第1张图片
NSMutableArray简单细说(一)—— 整体了解_第2张图片

从上面的开发文档NSMutableArray的目录就可以看到NSMutableArray的主要功能增删等等。我们接下来说的也是这些功能。

后记

未完,待续~~~

NSMutableArray简单细说(一)—— 整体了解_第3张图片

你可能感兴趣的:(NSMutableArray简单细说(一)—— 整体了解)