NSArray.h

/*  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;    // 元素個數(shù)
- (id)objectAtIndex:(NSUInteger)index;  // 元素索引
    
@end

@interface NSArray (NSExtendedArray)

- (NSArray *)arrayByAddingObject:(id)anObject;                      // 增加一個元素流译,返回一個新的數(shù)組
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;   // 從另一個數(shù)組中拷貝全部元素到新的數(shù)組
- (NSString *)componentsJoinedByString:(NSString *)separator;       // 將數(shù)組元素用分隔符來拼接成一個字符串
- (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范圍內(nèi)的某一元素的索引
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject;                // 返回和某一對象相同的元素的索引
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range; // 返回在range范圍內(nèi)和某一對象相同的元素的索引
- (BOOL)isEqualToArray:(NSArray *)otherArray;                       // 判斷兩個數(shù)組是否相等
- (id)firstObject NS_AVAILABLE(10_6, 4_0);                          // 返回首元素
- (id)lastObject;                                                   // 返回最后一個元素
- (NSEnumerator *)objectEnumerator;                                 // 返回一個數(shù)組迭代器
- (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; // 將數(shù)組寫入到文件宴偿,并選擇是否采用原子性
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;        // 將數(shù)組寫到URL中

- (void)makeObjectsPerformSelector:(SEL)aSelector;                  // 讓某一個元素響應(yīng)某個方法
- (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
// 遍歷數(shù)組元素
- (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;                                          // 創(chuàng)建一個數(shù)組
+ (instancetype)arrayWithObject:(id)anObject;                   // 創(chuàng)建一個帶有某對象的數(shù)組
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;    // 創(chuàng)建一個帶有多個對象的數(shù)組
+ (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; // 創(chuàng)建一個帶有多個對象的數(shù)組
+ (instancetype)arrayWithArray:(NSArray *)array;    // 從數(shù)組創(chuàng)建數(shù)組

- (instancetype)init;                           // 初始化
- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;   // 帶有多個對象的數(shù)組的初始化

- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;  // 帶有多個對象的數(shù)組的初始化
- (instancetype)initWithArray:(NSArray *)array; // 初始化并從數(shù)組創(chuàng)建數(shù)組
- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag;

+ (id /* NSArray * */)arrayWithContentsOfFile:(NSString *)path; // 從文件創(chuàng)建數(shù)組
+ (id /* NSArray * */)arrayWithContentsOfURL:(NSURL *)url;      // 從URL創(chuàng)建數(shù)組
- (id /* NSArray * */)initWithContentsOfFile:(NSString *)path;  // 從文件創(chuàng)建數(shù)組
- (id /* NSArray * */)initWithContentsOfURL:(NSURL *)url;       // 從URL創(chuàng)建數(shù)組

@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;                                 // 增加數(shù)組元素
- (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;             // 將某一數(shù)組中的元素加入到數(shù)組中
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; // 交換兩個索引處的元素
- (void)removeAllObjects;                                      // 刪除所有的元素
- (void)removeObject:(id)anObject inRange:(NSRange)range;      // 刪除某個范圍內(nèi)的某一元素
- (void)removeObject:(id)anObject;                             // 刪除某一元素
- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range; // 刪除某個范圍內(nèi)和某對象相同的元素
- (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;                   // 刪除某個范圍內(nèi)的所有的元素
- (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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末颗味,一起剝皮案震驚了整個濱河市灼芭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌柬讨,老刑警劉巖肢藐,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件别渔,死亡現(xiàn)場離奇詭異楣黍,居然都是意外死亡匾灶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門锡凝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粘昨,“玉大人垢啼,你說我怎么就攤上這事窜锯≌派觯” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵锚扎,是天一觀的道長吞瞪。 經(jīng)常有香客問我,道長驾孔,這世上最難降的妖魔是什么芍秆? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮翠勉,結(jié)果婚禮上妖啥,老公的妹妹穿的比我還像新娘。我一直安慰自己对碌,他們只是感情好荆虱,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著朽们,像睡著了一般怀读。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上骑脱,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天菜枷,我揣著相機與錄音,去河邊找鬼叁丧。 笑死啤誊,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的歹袁。 我是一名探鬼主播坷衍,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼条舔!你這毒婦竟也來了枫耳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤孟抗,失蹤者是張志新(化名)和其女友劉穎迁杨,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凄硼,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡铅协,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了摊沉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狐史。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出骏全,到底是詐尸還是另有隱情苍柏,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布姜贡,位于F島的核電站试吁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏楼咳。R本人自食惡果不足惜熄捍,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望母怜。 院中可真熱鬧余耽,春花似錦、人聲如沸苹熏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柜裸。三九已至缕陕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間疙挺,已是汗流浹背扛邑。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铐然,地道東北人蔬崩。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像搀暑,于是被迫代替她去往敵國和親沥阳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容