Foundition API - NSArray學(xué)習(xí)

第一次學(xué)習(xí)英文官方文檔,有很多不懂得地方,暫時(shí)空著,回頭查詢(xún)中文文檔再補(bǔ)全,如果有大神愿意指點(diǎn),請(qǐng)留言,感謝?。?!

1、創(chuàng)建一個(gè)Array

//1>創(chuàng)建一個(gè)空數(shù)組
+ (instancetype)array;
//demo
NSArray *arr = [NSArray array];

//2>創(chuàng)建并返回一個(gè)數(shù)組,這個(gè)數(shù)組擁有另一個(gè)數(shù)組中的對(duì)象
+ (instancetype)arrayWithArray:(NSArray<ObjectType> *)array;
//demo
NSArray *arr = [NSArray arrayWithArray:arr1];

//3>創(chuàng)建并返回一個(gè)數(shù)組,這個(gè)數(shù)組中包含路徑指定文件中的內(nèi)容
+ (NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;
//demo
NSArray *arr = [NSArray arrayWithContentsOfFile:path];

//4>創(chuàng)建并返回一個(gè)數(shù)組,這個(gè)數(shù)組中包含指定url中的內(nèi)容
+ (NSArray<ObjectType> *)arrayWithContentsOfURL:(NSURL *)url;
//demo
NSArray *arr = [NSArray arrayWithContentsOfURL:url];

//5>創(chuàng)建并返回一個(gè)數(shù)組,這個(gè)數(shù)組中包含一個(gè)指定的對(duì)象
+ (instancetype)arrayWithObject:(ObjectType)anObject;
//demo
NSArray *arr = [NSArray arrayWithObject:@"one"];

//6>創(chuàng)建并返回一個(gè)數(shù)組,這個(gè)數(shù)組中包含一列對(duì)象
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...;
//demo
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

//7>沒(méi)用過(guò)
+ (instancetype)arrayWithObjects:(const ObjectType  _Nonnull [])objects count:(NSUInteger)cnt;

2>初始化一個(gè)數(shù)組

//1>初始化一個(gè)最新分配內(nèi)存的數(shù)組
- (instancetype)init;
//demo
NSArray *arr = [[NSArray alloc] init];

//2>初始化一個(gè)最新分配內(nèi)存的數(shù)組,這個(gè)數(shù)組擁有另一個(gè)數(shù)組中的對(duì)象
- (instancetype)initWithArray:(NSArray<ObjectType> *)array;
//demo
NSArray *arr = [[NSArray alloc] initWithArray:arr1];

//3>初始化一個(gè)最新分配內(nèi)存的數(shù)組,這個(gè)數(shù)組擁有另一個(gè)數(shù)組中的對(duì)象,并可選擇是否copy數(shù)組中對(duì)象
- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag;
//demo
NSArray *arr = [[NSArray alloc] initWithArray:arr1 copyItems:YES];

//4>初始化一個(gè)最新分配內(nèi)存的數(shù)組,這個(gè)數(shù)組中包含指定路徑下文件的內(nèi)容
- (NSArray<ObjectType> *)initWithContentsOfFile:(NSString *)path;
//demo
NSArray *arr = [[NSArray alloc] initWithContentsOfFile:path];

//5>初始化一個(gè)最新分配內(nèi)存的數(shù)組,這個(gè)數(shù)組中包含指定url中的內(nèi)容
- (NSArray<ObjectType> *)initWithContentsOfURL:(NSURL *)url;
//demo
NSArray *arr = [[NSArray alloc] initWithContentsOfURL:url];

//6>初始化一個(gè)最新分配內(nèi)存的數(shù)組,這個(gè)數(shù)組中包含一列對(duì)象
- (instancetype)initWithObjects:(ObjectType)firstObj, ...;
//demo
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

//7>沒(méi)用過(guò)
- (instancetype)initWithObjects:(ObjectType  _Nonnull const [])objects count:(NSUInteger)cnt;

3、查詢(xún)一個(gè)字典

//1>判斷數(shù)組中是否包含指定對(duì)象
- (BOOL)containsObject:(ObjectType)anObject;
//demo
NSArray *arr1 = @[@"1",@"2",@"3"];
BOOL isContain = [arr1 containsObject:@"1"];
NSLog(@"isContain = %d",isContain);//isContain = 1

//2>判斷數(shù)組中對(duì)象的數(shù)量
@property(readonly) NSUInteger count;
//demo
NSUInteger num = arr1.count;

//3>
- (void)getObjects:(ObjectType  _Nonnull [])objects;

//4>
- (void)getObjects:(ObjectType  _Nonnull [])objects range:(NSRange)range;

//5>獲取數(shù)組中第一個(gè)對(duì)象
@property(nonatomic, readonly) ObjectType firstObject;

//6>獲取數(shù)組中最后一個(gè)對(duì)象
@property(nonatomic, readonly) ObjectType lastObject;

//7>獲取數(shù)組中指定位置的對(duì)象
- (ObjectType)objectAtIndex:(NSUInteger)index;
//demo
ObjectType obj = [arr1 objectAtIndex:1];

//8>獲取數(shù)組中指定位置的對(duì)象
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;

//9>獲取索引集合中所有索引對(duì)應(yīng)位置的對(duì)象,組成新的數(shù)組
- (NSArray<ObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;
//demo
NSArray *arr1 = @[@"1",@"2",@"3"];
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
[set addIndex:0];
[set addIndex:2];
NSArray *arr2 = [arr1 objectsAtIndexes:set];// (1,3)

//10>數(shù)組轉(zhuǎn)化為枚舉類(lèi)型
- (NSEnumerator<ObjectType> *)objectEnumerator;

4、查詢(xún)數(shù)組中相應(yīng)對(duì)象的位置(建議使用前先判斷是否存在,然后查詢(xún)位置)

//1>返回?cái)?shù)組中與給定的對(duì)象相等對(duì)象的最小索引
- (NSUInteger)indexOfObject:(ObjectType)anObject;
//demo
NSArray *arr = @[@"1",@"2",@"3"];
NSUInteger index = [arr indexOfObject:@"1"];

//2>返回?cái)?shù)組中特定范圍內(nèi)與給定的對(duì)象相等對(duì)象的最小索引
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
//demo
NSArray *arr = @[@"1",@"2",@"3"];
NSInteger index1 = [arr indexOfObject:@"3" inRange:NSMakeRange(2, 1)];

//3>返回?cái)?shù)組中與給定的對(duì)象完全相同對(duì)象的最小索引
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
//demo
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
NSArray *arr = @[arr1,arr2,arr3];
NSInteger index1 = [arr indexOfObjectIdenticalTo:arr1];

//4>返回?cái)?shù)組中特定范圍內(nèi)與給定的對(duì)象完全相同對(duì)象的最小索引
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
//demo
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];
NSArray *arr = @[arr1,arr2,arr3];
NSInteger index2 = [arr indexOfObjectIdenticalTo:arr1 inRange:NSMakeRange(0, 2)];

//5>
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//6>
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//7>
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//8>
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//9>
- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//10>
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

//11>
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp;

5、Sending Message to Elements

//1>
- (void)makeObjectsPerformSelector:(SEL)aSelector;

//2>
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

//3>
- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

//4>
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

//5>
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

6、數(shù)組比較

//1>返回兩個(gè)數(shù)組中第一個(gè)相等的對(duì)象
- (ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray;

//2>比較兩個(gè)數(shù)組是否相等
- (BOOL)isEqualToArray:(NSArray<ObjectType> *)otherArray;

7、得到新的數(shù)組

//1>拷貝一個(gè)數(shù)組,在其末尾增加一個(gè)對(duì)象并返回
- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;
//demo
NSArray *arr4 = [arr1 arrayByAddingObject:@"4"];

//2>拷貝一個(gè)數(shù)組,在其末尾增加另一個(gè)數(shù)組的所有對(duì)象并返回
- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray;

//3>
- (NSArray<ObjectType> *)filteredArrayUsingPredicate:(NSPredicate *)predicate;

//4>復(fù)制數(shù)組中給定范圍內(nèi)的對(duì)象,并作為新數(shù)組返回
- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;
//demo
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil]; 
NSArray *arr4 = [arr3 subarrayWithRange:NSMakeRange(0, 2)];

8、排序

//1>
@property(readonly, copy) NSData *sortedArrayHint;

//2>
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void *))comparator context:(void *)context;

//3>
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void *))comparator context:(void *)context hint:(NSData *)hint;

//4>
- (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors;

//5>
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;

//6>
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr;


//7>
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;

9、用字符串拼接數(shù)組

- (NSString *)componentsJoinedByString:(NSString *)separator;
//demo
NSArray *arr4 = [[NSArray alloc] initWithObjects:@"2", @"1", @"3", nil];
NSString *str = [arr4 componentsJoinedByString:@","];

10、創(chuàng)建描述

//1>
@property(readonly, copy) NSString *description;

//2>
- (NSString *)descriptionWithLocale:(id)locale;

//3>
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;

11、存儲(chǔ)數(shù)組

//1>將數(shù)組存入指定的路徑文件下
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

//2>將數(shù)組存入指定的url中
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

12、收集路徑

- (NSArray<NSString *> *)pathsMatchingExtensions:(NSArray<NSString *> *)filterTypes;

13、觀(guān)察者模式(Key-Value Observing)
使用:http://www.itdecent.cn/p/b0c30891f1bf

//1>添加觀(guān)察者
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
//demo
[_a addObserver:_b forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

//2>移除觀(guān)察者
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
//demo
[_a removeObserver:_b forKeyPath:@"name"];

//3>移除觀(guān)察者
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context;
//demo
[_a removeObserver:_b forKeyPath:@"name" context:NULL];

//4>移除觀(guān)察者
- (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath context:(void *)context;

//5>添加觀(guān)察者
- (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;

//6>移除觀(guān)察者
- (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath;

14、鍵-值編碼(Key-Value Coding)

//1>寫(xiě)值
- (void)setValue:(id)value forKey:(NSString *)key;

//2>取值
- (id)valueForKey:(NSString *)key;

15、Randomly Shuffling an Array

//1>
- (NSArray<ObjectType> *)shuffledArray;

//2>
- (NSArray<ObjectType> *)shuffledArrayWithRandomSource:(GKRandomSource *)randomSource;

16、新方法

- (instancetype)initWithCoder:(NSCoder *)aDecoder;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容