版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.24 |
前言
NSArray
是集合类型中的一种,是OC中很重要的概念,这个是我们一定会用到的对象,下面我就继续由整体到细节,由简单到复杂的和大家说一下它的用法。
基本了解
NSArray
系统提供的数组类:类似于C语言中的数组的功能,数组是一个大容器,数组中航韩国可以存储不同类型的对象,但必须要保证数组中储存的都是对象。
OC提供了两类数组:一类是NSArray
(不可变数组),一类是NSMutableArray
(可变数组)。可变与不可变的区别:可变就意味着可以对原有对象进行操作,不可变就意味着一旦创建,内容就不可改变。
swift
对于可变数组与不可变数组的定义也就是let
与 var
的区别。
API接口
下面我们就看一下NSArray的API接口。
#import <Foundation/NSObject.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSRange.h>
#import <Foundation/NSObjCRuntime.h>
@class NSData, NSIndexSet, NSString, NSURL;
/**************** Immutable Array ****************/
NS_ASSUME_NONNULL_BEGIN
@interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
@property (readonly) NSUInteger count;
- (ObjectType)objectAtIndex:(NSUInteger)index;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithObjects:(const ObjectType _Nonnull [_Nullable])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
@interface NSArray<ObjectType> (NSExtendedArray)
- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;
- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray;
- (NSString *)componentsJoinedByString:(NSString *)separator;
- (BOOL)containsObject:(ObjectType)anObject;
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
- (nullable ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray;
- (void)getObjects:(ObjectType __unsafe_unretained [])objects range:(NSRange)range NS_SWIFT_UNAVAILABLE("Use 'subarrayWithRange()' instead");
- (NSUInteger)indexOfObject:(ObjectType)anObject;
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (BOOL)isEqualToArray:(NSArray<ObjectType> *)otherArray;
@property (nullable, nonatomic, readonly) ObjectType firstObject NS_AVAILABLE(10_6, 4_0);
@property (nullable, nonatomic, readonly) ObjectType lastObject;
- (NSEnumerator<ObjectType> *)objectEnumerator;
- (NSEnumerator<ObjectType> *)reverseObjectEnumerator;
@property (readonly, copy) NSData *sortedArrayHint;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
- (void)makeObjectsPerformSelector:(SEL)aSelector NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument NS_SWIFT_UNAVAILABLE("Use enumerateObjectsUsingBlock: or a for loop instead");
- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {
NSBinarySearchingFirstEqual = (1UL << 8),
NSBinarySearchingLastEqual = (1UL << 9),
NSBinarySearchingInsertionIndex = (1UL << 10),
};
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp NS_AVAILABLE(10_6, 4_0); // binary search
@end
@interface NSArray<ObjectType> (NSArrayCreation)
+ (instancetype)array;
+ (instancetype)arrayWithObject:(ObjectType)anObject;
+ (instancetype)arrayWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt;
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)arrayWithArray:(NSArray<ObjectType> *)array;
- (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithArray:(NSArray<ObjectType> *)array;
- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag;
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
- (nullable NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;
@end
@interface NSArray<ObjectType> (NSDeprecated)
/* This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:range: instead.
*/
- (void)getObjects:(ObjectType __unsafe_unretained [])objects NS_SWIFT_UNAVAILABLE("Use 'as [AnyObject]' instead");
@end
从上面可以看出来,有一个本类,还有几个分类,分别是NSExtendedArray
、 NSArrayCreation
和NSDeprecated
,一共是三个分类。
下面我们看一下开发文档里面NSArray的描述。
下面我们就以表格的形式给出这些模块和模块信息。
模块名称 | 模块内容 |
---|---|
创建一个数组 | + (instancetype)array; |
+ (instancetype)arrayWithArray:(NSArray<ObjectType> *)array; | |
+ (NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path; | |
+ (NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url; | |
+ (instancetype)arrayWithObject:(ObjectType)anObject; | |
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...; | |
+ (instancetype)arrayWithObjects:(const ObjectType _Nonnull [])objects count:(NSUInteger)cnt; | |
初始化数组 | - (instancetype)init; |
- (instancetype)initWithArray:(NSArray<ObjectType> *)array; | |
- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag; | |
- (NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path; | |
- (NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url; | |
- (instancetype)initWithObjects:(ObjectType)firstObj, ...; | |
- (instancetype)initWithObjects:(ObjectType _Nonnull const *)objects count:(NSUInteger)cnt; | |
查询数组 | - (BOOL)containsObject:(ObjectType)anObject; |
@property(readonly) NSUInteger count; | |
- (void)getObjects:(ObjectType _Nonnull [])objects; | |
- (void)getObjects:(ObjectType _Nonnull [])objects range:(NSRange)range; | |
@property(nonatomic, readonly) ObjectType firstObject; | |
@property(nonatomic, readonly) ObjectType lastObject; | |
- (ObjectType)objectAtIndex:(NSUInteger)index; | |
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx; | |
- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes; | |
- (NSEnumerator<ObjectType> *)objectEnumerator; | |
- (NSEnumerator<ObjectType> *)reverseObjectEnumerator; | |
数组中查找对象 | - (NSUInteger)indexOfObject:(ObjectType)anObject; |
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range; | |
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject; | |
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range; | |
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp; | |
Sending Messages to Elements | - (void)makeObjectsPerformSelector:(SEL)aSelector; |
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument; | |
- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block; | |
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block; | |
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block; | |
Comparing Arrays | - (ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray; |
- (BOOL)isEqualToArray:(NSArray<ObjectType> *)otherArray; | |
Deriving New Arrays | - (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject; |
- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray; | |
- (NSArray<ObjectType> *)filteredArrayUsingPredicate:(NSPredicate *)predicate; | |
- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range; | |
Sorting | @property(readonly, copy) NSData *sortedArrayHint; |
- (NSArray<ObjectType> )sortedArrayUsingFunction:(NSInteger ()(ObjectType, ObjectType, void *))comparator context:(void *)context; | |
- (NSArray<ObjectType> )sortedArrayUsingFunction:(NSInteger ()(ObjectType, ObjectType, void *))comparator context:(void *)context hint:(NSData *)hint; | |
- (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors; | |
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator; | |
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr; | |
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr; | |
Working with String Elements | - (NSString *)componentsJoinedByString:(NSString *)separator; |
Creating a Description | @property(readonly, copy) NSString *description; |
- (NSString *)descriptionWithLocale:(id)locale; | |
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level; | |
Sorting Arrays | - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; |
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; | |
Collecting Paths | - (NSArray<NSString *> *)pathsMatchingExtensions:(NSArray<NSString *> *)filterTypes; |
Key - Value Observing | - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context; |
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath; | |
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context; | |
- (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath context:(void *)context; | |
- (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context; | |
- (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath; | |
Key - Value Coding | - (void)setValue:(id)value forKey:(NSString *)key; |
- (id)valueForKey:(NSString *)key; | |
Randomly Shuffling an Array | - (NSArray<ObjectType> *)shuffledArray; |
- (NSArray<ObjectType> *)shuffledArrayWithRandomSource:(GKRandomSource *)randomSource; | |
New Methods | - (instancetype)initWithCoder:(NSCoder *)aDecoder; |
后记
未完,待续~~~