NSArray.h

作者: ShenYj | 来源:发表于2016-09-09 18:21 被阅读112次
/*  NSArray.h
    Copyright (c) 1994-2013, Apple Inc. All rights reserved.
*/

#import <Foundation/NSObject.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSRange.h>
#import <Foundation/NSObjCRuntime.h>

@class NSData, NSIndexSet, NSString, NSURL;

/****************   Immutable Array     ****************/
#pragma mark - NSArray
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>

- (NSUInteger)count;    // 元素个数
- (id)objectAtIndex:(NSUInteger)index;  // 元素索引
    
@end

@interface NSArray (NSExtendedArray)

- (NSArray *)arrayByAddingObject:(id)anObject;                      // 增加一个元素,返回一个新的数组
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;   // 从另一个数组中拷贝全部元素到新的数组
- (NSString *)componentsJoinedByString:(NSString *)separator;       // 将数组元素用分隔符来拼接成一个字符串
- (BOOL)containsObject:(id)anObject;                                // 判断是否包含某一个元素
- (NSString *)description;                                          // 重写父类的 description 方法
- (NSString *)descriptionWithLocale:(id)locale;                     //
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
- (id)firstObjectCommonWithArray:(NSArray *)otherArray;
- (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range; // 取得range范围类的所有元素
- (NSUInteger)indexOfObject:(id)anObject;                           // 返回某一个元素的索引
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;    // 返回在range范围内的某一元素的索引
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject;                // 返回和某一对象相同的元素的索引
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range; // 返回在range范围内和某一对象相同的元素的索引
- (BOOL)isEqualToArray:(NSArray *)otherArray;                       // 判断两个数组是否相等
- (id)firstObject NS_AVAILABLE(10_6, 4_0);                          // 返回首元素
- (id)lastObject;                                                   // 返回最后一个元素
- (NSEnumerator *)objectEnumerator;                                 // 返回一个数组迭代器
- (NSEnumerator *)reverseObjectEnumerator;                          // 返回一个反序迭代器
- (NSData *)sortedArrayHint;
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context;
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint;
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;
- (NSArray *)subarrayWithRange:(NSRange)range;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; // 将数组写入到文件,并选择是否采用原子性
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;        // 将数组写到URL中

- (void)makeObjectsPerformSelector:(SEL)aSelector;                  // 让某一个元素响应某个方法
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;

- (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);

#pragma mark - if NS_BLOCKS_AVAILABLE

#if NS_BLOCKS_AVAILABLE
// 遍历数组元素
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);

- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
//
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);

typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) {
    NSBinarySearchingFirstEqual = (1UL << 8),
    NSBinarySearchingLastEqual = (1UL << 9),
    NSBinarySearchingInsertionIndex = (1UL << 10),
};

- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search

#endif

@end

#pragma mark - NSArray的初始化

@interface NSArray (NSArrayCreation)

+ (instancetype)array;                                          // 创建一个数组
+ (instancetype)arrayWithObject:(id)anObject;                   // 创建一个带有某对象的数组
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;    // 创建一个带有多个对象的数组
+ (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; // 创建一个带有多个对象的数组
+ (instancetype)arrayWithArray:(NSArray *)array;    // 从数组创建数组

- (instancetype)init;                           // 初始化
- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;   // 带有多个对象的数组的初始化

- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;  // 带有多个对象的数组的初始化
- (instancetype)initWithArray:(NSArray *)array; // 初始化并从数组创建数组
- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag;

+ (id /* NSArray * */)arrayWithContentsOfFile:(NSString *)path; // 从文件创建数组
+ (id /* NSArray * */)arrayWithContentsOfURL:(NSURL *)url;      // 从URL创建数组
- (id /* NSArray * */)initWithContentsOfFile:(NSString *)path;  // 从文件创建数组
- (id /* NSArray * */)initWithContentsOfURL:(NSURL *)url;       // 从URL创建数组

@end



@interface NSArray (NSDeprecated)

/* This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:range: instead.
*/
- (void)getObjects:(id __unsafe_unretained [])objects;

@end

/****************   Mutable Array       ****************/
#pragma mark - NSMutableArray

@interface NSMutableArray : NSArray

- (void)addObject:(id)anObject;                                 // 增加数组元素
- (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;             // 将某一数组中的元素加入到数组中
- (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; // 替换某些索引处的元素

- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);

#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)

+ (instancetype)arrayWithCapacity:(NSUInteger)numItems;

- (instancetype)init;   /* designated initializer */
- (instancetype)initWithCapacity:(NSUInteger)numItems;  /* designated initializer */

@end


相关文章

网友评论

      本文标题:NSArray.h

      本文链接:https://www.haomeiwen.com/subject/ehzeettx.html