Foundition API - NSArray学习

第一次学习英文官方文档,有很多不懂得地方,暂时空着,回头查询中文文档再补全,如果有大神愿意指点,请留言,感谢!!!

1、创建一个Array

//1>创建一个空数组
+ (instancetype)array;
//demo
NSArray *arr = [NSArray array];

//2>创建并返回一个数组,这个数组拥有另一个数组中的对象
+ (instancetype)arrayWithArray:(NSArray *)array;
//demo
NSArray *arr = [NSArray arrayWithArray:arr1];

//3>创建并返回一个数组,这个数组中包含路径指定文件中的内容
+ (NSArray *)arrayWithContentsOfFile:(NSString *)path;
//demo
NSArray *arr = [NSArray arrayWithContentsOfFile:path];

//4>创建并返回一个数组,这个数组中包含指定url中的内容
+ (NSArray *)arrayWithContentsOfURL:(NSURL *)url;
//demo
NSArray *arr = [NSArray arrayWithContentsOfURL:url];

//5>创建并返回一个数组,这个数组中包含一个指定的对象
+ (instancetype)arrayWithObject:(ObjectType)anObject;
//demo
NSArray *arr = [NSArray arrayWithObject:@"one"];

//6>创建并返回一个数组,这个数组中包含一列对象
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...;
//demo
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

//7>没用过
+ (instancetype)arrayWithObjects:(const ObjectType  _Nonnull [])objects count:(NSUInteger)cnt;

2>初始化一个数组

//1>初始化一个最新分配内存的数组
- (instancetype)init;
//demo
NSArray *arr = [[NSArray alloc] init];

//2>初始化一个最新分配内存的数组,这个数组拥有另一个数组中的对象
- (instancetype)initWithArray:(NSArray *)array;
//demo
NSArray *arr = [[NSArray alloc] initWithArray:arr1];

//3>初始化一个最新分配内存的数组,这个数组拥有另一个数组中的对象,并可选择是否copy数组中对象
- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag;
//demo
NSArray *arr = [[NSArray alloc] initWithArray:arr1 copyItems:YES];

//4>初始化一个最新分配内存的数组,这个数组中包含指定路径下文件的内容
- (NSArray *)initWithContentsOfFile:(NSString *)path;
//demo
NSArray *arr = [[NSArray alloc] initWithContentsOfFile:path];

//5>初始化一个最新分配内存的数组,这个数组中包含指定url中的内容
- (NSArray *)initWithContentsOfURL:(NSURL *)url;
//demo
NSArray *arr = [[NSArray alloc] initWithContentsOfURL:url];

//6>初始化一个最新分配内存的数组,这个数组中包含一列对象
- (instancetype)initWithObjects:(ObjectType)firstObj, ...;
//demo
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

//7>没用过
- (instancetype)initWithObjects:(ObjectType  _Nonnull const [])objects count:(NSUInteger)cnt;

3、查询一个字典

//1>判断数组中是否包含指定对象
- (BOOL)containsObject:(ObjectType)anObject;
//demo
NSArray *arr1 = @[@"1",@"2",@"3"];
BOOL isContain = [arr1 containsObject:@"1"];
NSLog(@"isContain = %d",isContain);//isContain = 1

//2>判断数组中对象的数量
@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>获取数组中第一个对象
@property(nonatomic, readonly) ObjectType firstObject;

//6>获取数组中最后一个对象
@property(nonatomic, readonly) ObjectType lastObject;

//7>获取数组中指定位置的对象
- (ObjectType)objectAtIndex:(NSUInteger)index;
//demo
ObjectType obj = [arr1 objectAtIndex:1];

//8>获取数组中指定位置的对象
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;

//9>获取索引集合中所有索引对应位置的对象,组成新的数组
- (NSArray *)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>数组转化为枚举类型
- (NSEnumerator *)objectEnumerator;

4、查询数组中相应对象的位置(建议使用前先判断是否存在,然后查询位置)

//1>返回数组中与给定的对象相等对象的最小索引
- (NSUInteger)indexOfObject:(ObjectType)anObject;
//demo
NSArray *arr = @[@"1",@"2",@"3"];
NSUInteger index = [arr indexOfObject:@"1"];

//2>返回数组中特定范围内与给定的对象相等对象的最小索引
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
//demo
NSArray *arr = @[@"1",@"2",@"3"];
NSInteger index1 = [arr indexOfObject:@"3" inRange:NSMakeRange(2, 1)];

//3>返回数组中与给定的对象完全相同对象的最小索引
- (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>返回数组中特定范围内与给定的对象完全相同对象的最小索引
- (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、数组比较

//1>返回两个数组中第一个相等的对象
- (ObjectType)firstObjectCommonWithArray:(NSArray *)otherArray;

//2>比较两个数组是否相等
- (BOOL)isEqualToArray:(NSArray *)otherArray;

7、得到新的数组

//1>拷贝一个数组,在其末尾增加一个对象并返回
- (NSArray *)arrayByAddingObject:(ObjectType)anObject;
//demo
NSArray *arr4 = [arr1 arrayByAddingObject:@"4"];

//2>拷贝一个数组,在其末尾增加另一个数组的所有对象并返回
- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray;

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

//4>复制数组中给定范围内的对象,并作为新数组返回
- (NSArray *)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 *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void *))comparator context:(void *)context;

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

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

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

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


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

9、用字符串拼接数组

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

10、创建描述

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

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

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

11、存储数组

//1>将数组存入指定的路径文件下
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

//2>将数组存入指定的url中
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

12、收集路径

- (NSArray *)pathsMatchingExtensions:(NSArray *)filterTypes;

13、观察者模式(Key-Value Observing)
使用:http://www.jianshu.com/p/b0c30891f1bf

//1>添加观察者
- (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>移除观察者
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
//demo
[_a removeObserver:_b forKeyPath:@"name"];

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

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

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

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

14、键-值编码(Key-Value Coding)

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

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

15、Randomly Shuffling an Array

//1>
- (NSArray *)shuffledArray;

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

16、新方法

- (instancetype)initWithCoder:(NSCoder *)aDecoder;

你可能感兴趣的:(Foundition API - NSArray学习)