NSArray
用属性表显示一个数组的内容。
- (NSString *)descriptionWithLocale:(nullable id)locale;
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
- (NSString *)description
正序倒序
(NSEnumerator<ObjectType> *)objectEnumerator;
-
(NSEnumerator<ObjectType> *)reverseObjectEnumerator;
//1.原始数组
NSMutableArray array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
//2.倒序的数组
NSArray reversedArray = [[array reverseObjectEnumerator] allObjects];
//2、枚举器法
NSLog(@"------- 枚举器法---------");
//ObjectEnumerator 正序
//reverseObjectEnumerator 逆序
NSEnumerator *enumerator = [array reverseObjectEnumerator];
id obj = nil; //不确定数组里面具体对象的类型,所以定义成id 类型指针
while (obj = [enumerator nextObject]) { //通过枚举器,取数组里面的每一个元素
NSLog(@"%@", obj); //将元素赋给 obj, 直到数组结束
//取到的结果为nil,退出while
}
用来数组排序的参数:
@property (readonly, copy) NSData *sortedArrayHint;
(NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context;
(NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
使用NSArray自己的sel方法来排序,如果自定义要创建NSArray的category
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
是否已经把文件保存到辅助文件 或者 网上的URL中
(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
(BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
将一个操作 作用在数组中的每个元素上.。(不带参数/带参数)
- (void)makeObjectsPerformSelector:(SEL)aSelector
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
返回指定下标的一个对象。
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
数组的遍历
- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType 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
数组某一部分的遍历,新加遍历顺序
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
根据条件用来获取一个NSUIndex 对象,主要是根据条件进行数据遍历使用
- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
NSInteger index = [array indexOfObjectPassingTest:^ BOOL (id tr,NSUInteger index, BOOL *te){
NSString *s = (NSString *)tr;
if([@"wendy" isEqualToString:s])
{
return YES;
}
return NO;
}];
NSLog(@"index==%d=.",index);
选择数组的遍历顺序,来获取一个NSUindex对象
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
子数组 遍历 处理数据
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
根据block 的处理获取一个NSIndexSet 对象。 顺序。子数组
(NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
(NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
-
(NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
NSIndexSet *index = [array indexesOfObjectsWithOptions:NSEnumerationReverse passingTest: ^ BOOL (id tr, NSUInteger index,BOOL *te){
NSString *s = (NSString *)tr; if([s isEqualToString:@"andy"]){ return YES; } return NO;
}];
NSLog(@"%@",index);
对数组进行排序操作 参数cmptr 是一个block 函数块,返回的数据类型是一个NSComparisonResult 对象
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
NSArray *te = [array sortedArrayUsingComparator:^ NSComparisonResult (NSString *s,NSString *s2){
if(s.length < s2.length){
return NSOrderedAscending;
}
if(s.length > s2.length){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
NSLog(@"te=%@.",te);
进行排序操作,NSSortOptions 排序的参数 用来表示是同时排序,还是稳定执行。
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr NS_AVAILABLE(10_6, 4_0);
数组的二分查找法
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp NS_AVAILABLE(10_6, 4_0); // binary search
[Objective-C] NSArray的二分查找
http://www.isaced.com/post-241.html
NSMutableArray
交换指定 index1 和 index2 两个位置上的元素
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
使用anObject 对象替换 range 位置上的元素,
相当于删除 range位置的元素,然后在把 anobject 插入到这个位置
- (void)removeObject:(ObjectType)anObject inRange:(NSRange)range;
- (void)removeObject:(ObjectType)anObject;
- (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (void)removeObjectIdenticalTo:(ObjectType)anObject;
删除一定范围内的所有元素 (不推荐使用)
- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);
对当前的数组排序,使用排序算法
- (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))compare context:(nullable void *)context;
排序
- (void)sortUsingComparator:(NSComparator)cmptr
进行数组排序
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);
http://blog.sina.com.cn/s/blog_702e40a80101gagf.html
http://blog.csdn.net/u012973002/article/details/53033493