数组.h文件的分析
NSArray.h分析
[代码]c#/cpp/oc代码:
001 | /* NSArray.h |
002 | Copyright (c) 1994-2012, Apple Inc. All rights reserved. |
003 | */ |
004 |
005 |
006 | #import <Foundation/NSObject.h> 根类,提供基本运行能力的接口 |
007 | #import <Foundation/NSEnumerator.h> 集合类 |
008 | #import <Foundation/NSRange.h> 截取字符串和数组 |
009 | #import <Foundation/NSObjCRuntime.h> 大量宏定义 |
010 |
011 |
012 | @ class NSData, NSIndexSet, NSString, NSURL; @ class 告诉编译器,其后面声明的名称是类的名称 |
013 |
014 |
015 | /**************** Immutable Array ****************/ |
016 |
017 |
018 | @ interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration> |
019 |
020 |
021 | - (NSUInteger)count; 数组元素个数 |
022 | - (id)objectAtIndex:(NSUInteger)index; 返回指定索引的数组元素 |
023 |
024 | @end |
025 |
026 |
027 | @ interface NSArray (NSExtendedArray) |
028 |
029 |
030 | - (NSArray *)arrayByAddingObject:(id)anObject;追加对象 |
031 | - (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray; 追加其他数组 |
032 | - (NSString *)componentsJoinedByString:(NSString *)separator;分割数组为字符串 |
033 | - (BOOL)containsObject:(id)anObject;判断数组是否存在指定元素 |
034 | - (NSString *)description; 格式化为一个属性列表 |
035 | - (NSString *)descriptionWithLocale:(id)locale;本地化格式化为一个属性列表 |
036 | - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;本地化格式化为一个属性列表(设置缩进) |
037 | - (id)firstObjectCommonWithArray:(NSArray *)otherArray;取出array对象跟demo对象第一个交集元素 |
038 | - ( void )getObjects:(id __unsafe_unretained [])objects range:(NSRange)range; 返回指定区域的对象 |
039 | - (NSUInteger)indexOfObject:(id)anObject; 获取指定元素的索引 |
040 | - (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;指定区域获取元素的索引 |
041 | - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject;获取指定元素的索引 |
042 | - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range;指定区域获取元素的索引 |
043 | - (BOOL)isEqualToArray:(NSArray *)otherArray; 判断二个数组对象是否相等 |
044 | - (id)lastObject;取出数组最后一个元素 |
045 | - (NSEnumerator *)objectEnumerator;返回一个枚举对象 |
046 | - (NSEnumerator *)reverseObjectEnumerator;返回一个翻转的枚举对象 |
047 | - (NSData *)sortedArrayHint;升序排序数组元素 |
048 | - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:( void *)context; 调用指定方法排序数组元素 |
049 | - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:( void *)context hint:(NSData *)hint;调用指定方法排序数组元素 |
050 | - (NSArray *)sortedArrayUsingSelector:(SEL)comparator;指定比较方法排序数据元素 |
051 | - (NSArray *)subarrayWithRange:(NSRange)range;返回指定区域的数组元素组成新的数组对象 |
052 | - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;保存数组对象到指定文件 |
053 | - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;保存数组对象到指定URL |
054 |
055 |
056 | - ( void )makeObjectsPerformSelector:(SEL)aSelector;每个数组元素都调用指定方法 |
057 | - ( void )makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;每个数组元素都调用指定方法(指定参数) |
058 |
059 |
060 | - (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes;指定索引集合返回新数组 |
061 |
062 |
063 | - (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); 返回指定索引数组元素 |
064 |
065 |
066 | #if NS_BLOCKS_AVAILABLE SDK 版本判断和代码适配处理 |
067 | - ( void )enumerateObjectsUsingBlock:( void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);数组每个元素执行代码块中的方法( foreach ) |
068 | - ( void )enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:( void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);指定块使数组每个元素执行代码块中的方法 |
069 | - ( void )enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:( void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 遍历数组每个元素执行代码块中的方法 |
070 |
071 |
072 | - (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);数组第一个元素对象执行代码块方法(测试用) |
073 | - (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);返回通过代码块方法测试的元素索引集第一个索引 |
074 |
075 |
076 | - (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);返回通过代码块方法测试的元素索引集 |
077 | - (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); 返回通过指定【索引集合《=》枚举选项】的代码块方法的元素索引集 |
078 | - (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);返回通过指定【索引集合《=》枚举选项】的代码块方法的元素索引集 |
079 |
080 |
081 | - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);返回指定对比方法升序数组 |
082 | - (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);返回指定对比方法升序数组 |
083 |
084 |
085 | typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) { |
086 | NSBinarySearchingFirstEqual = (1UL << 8), |
087 | NSBinarySearchingLastEqual = (1UL << 9), |
088 | NSBinarySearchingInsertionIndex = (1UL << 10), |
089 | }; |
090 |
091 |
092 | - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search 指定区域返回通过代码块方法的索引 |
093 |
094 |
095 | #endif |
096 |
097 |
098 | @end |
099 |
100 |
101 | @ interface NSArray (NSArrayCreation) |
102 |
103 |
104 | + (id)array;创建一个空数组 |
105 | + (id)arrayWithObject:(id)anObject;指定一个元素创建数组对象 |
106 | + (id)arrayWithObjects:( const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(类消息) |
107 | + (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;指定多个元素创建数组对象 |
108 | + (id)arrayWithArray:(NSArray *)array;生成新另外一个数组 |
109 |
110 |
111 | - (id)initWithObjects:( const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(实例消息) |
112 | - (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象 |
113 | - (id)initWithArray:(NSArray *)array;从已有数组初始化 |
114 | - (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;从已有数组初始化(标示作用,浅复制,还是深复制) |
115 |
116 |
117 | + (id)arrayWithContentsOfFile:(NSString *)path;从文件加载数据 |
118 | + (id)arrayWithContentsOfURL:(NSURL *)url;从网络地址加载数据 |
119 | - (id)initWithContentsOfFile:(NSString *)path;从文件加载数据 |
120 | - (id)initWithContentsOfURL:(NSURL *)url;从网络地址加载数据 |
121 |
122 |
123 | @end |
124 |
125 |
126 | @ interface NSArray (NSDeprecated) |
127 |
128 |
129 | /* This method is unsafe because it could potentially cause buffer overruns. You should use -getObjects:range: instead. |
130 | */ |
131 | - ( void )getObjects:(id __unsafe_unretained [])objects; 获取指定区域对象,容易内存泄露 |
132 |
133 |
134 | @end |
135 |
136 |
137 | /**************** Mutable Array ****************/ |
138 |
139 |
140 | @ interface NSMutableArray : NSArray (可变数组) |
141 |
142 |
143 | - ( void )addObject:(id)anObject; 添加对象 |
144 | - ( void )insertObject:(id)anObject atIndex:(NSUInteger)index; 指定索引位置插入对象 |
145 | - ( void )removeLastObject;移除最后一个对象 |
146 | - ( void )removeObjectAtIndex:(NSUInteger)index; 移除指定索引位置对象 |
147 | - ( void )replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;替换指定索引位置对象 |
148 |
149 |
150 | @end |
151 |
152 |
153 | @ interface NSMutableArray (NSExtendedMutableArray) |
154 |
155 | - ( void )addObjectsFromArray:(NSArray *)otherArray; 追加数组A到数组B |
156 | - ( void )exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; 交互指定索引之间的对象 |
157 | - ( void )removeAllObjects; 销毁数组 |
158 | - ( void )removeObject:(id)anObject inRange:(NSRange)range;移除指定区域指定对象 |
159 | - ( void )removeObject:(id)anObject;移除指定对象 |
160 | - ( void )removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;移除指定区域指定对象 |
161 | - ( void )removeObjectIdenticalTo:(id)anObject;移除指定对象 |
162 | - ( void )removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);移除指定索引之间的对象 |
163 | - ( void )removeObjectsInArray:(NSArray *)otherArray;移除数组A包含的指定数组B元素 |
164 | - ( void )removeObjectsInRange:(NSRange)range;移除指定区域所有对象 |
165 | - ( void )replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;移除(数组A指定区域)包含的(指定数组B的指定区域)的元素 |
166 | - ( void )replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;移除(数组A指定区域)包含的(指定数组B)的元素 |
167 | - ( void )setArray:(NSArray *)otherArray;追加数组B到A |
168 | - ( void )sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:( void *)context; 指定方法升序比较 |
169 | - ( void )sortUsingSelector:(SEL)comparator;指定方法升序比较 |
170 |
171 |
172 | - ( void )insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes; 指定索引集合插入数组元素 |
173 | - ( void )removeObjectsAtIndexes:(NSIndexSet *)indexes; 移除指定索引集合元素 |
174 | - ( void )replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects; 替换指定索引集合的数组元素 |
175 |
176 |
177 | - ( void )setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); 指定索引替换新的数组元素 |
178 |
179 |
180 | #if NS_BLOCKS_AVAILABLE |
181 | - ( void )sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); 指定块方法排序数组元素 |
182 | - ( void )sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);指定块比较数组 |
183 | #endif |
184 |
185 |
186 | @end |
187 |
188 |
189 | @ interface NSMutableArray (NSMutableArrayCreation) |
190 |
191 |
192 | + (id)arrayWithCapacity:(NSUInteger)numItems; 初始化化数组对象容量 |
193 | - (id)initWithCapacity:(NSUInteger)numItems;初始化化数组对象容量 |
194 |
195 |
196 | @end |