2014资料整理--NSArray用法(二)
本文来自转载
1.使用block 块遍历整个数组。这个block 需要三个参数,id obj 表示数组中的元素。
NSUInteger
idx 标示元素的下标,
bool
*stop 是一个bool类型的参数。 官方描述如下:
A
reference to a Boolean value. The block can set the value to YES to
stop further processing of the
array.
The
stop argument is an out-only argument. You should only ever set
this Boolean to YES within the Block.
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger
idx,BOOL *stop))block
例如:
NSArray
*array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test",
nil];
[array
enumerateObjectsUsingBlock:^(id str,NSUInteger index, BOOL*
te){
NSLog(@"%@,%d",str,index);
}];
2、同上面的方法一项,区别在于,这里多添加了一个参数,用来标示
是从前向后遍历,还是从后往前遍历。
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts
usingBlock:(void (^)(id obj, NSUInteger idx,BOOL
*stop))block
例如:
NSArray
*array = [NSArray arrayWithObjects:@"wendy",@"andy",@"tom",@"test",
nil];
[array
enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id
str,NSUInteger index, BOOL* te){
NSLog(@"%@,%d",str,index);
}];
3、
同上面的方法一项,不过NSIndexSet 参数标示,根据下标取出的数组,这里真正在block
中遍历的数组,是根据NSindexSet 取到的子数组
- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s
options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj,
NSUInteger idx,BOOL
*stop))block
例如:
[array
enumerateObjectsAtIndexes:[NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(1, 3)]
options:NSEnumerationReverse usingBlock:^(id str,NSUInteger index,
BOOL* te){
NSLog(@"%@,%d",str,index);
}];
4、
根据条件用来获取一个NSUIndex 对象,主要是根据条件进行数据遍历使用(测试用)
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger
idx,BOOL *stop))predicate
调用如下:
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);
5、同上面的方法相同,却别在于,这里添加了一个参数,用来表示遍历是从前向后遍历还是从后遍历。
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(id obj, NSUInteger idx,BOOL
*stop))predicate
6、这个添加了参数NSIntexSet
参数,用来获取子数组,然后使用这个子数组进行遍历,处理数据
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s
options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj,
NSUInteger idx,BOOL *stop))predicate
7、
根据block 的处理获取一个NSIndexSet 对象。
- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj,
NSUInteger idx,BOOL *stop))predicate
调用如下:
NSIndexSet *index = [array
indexesOfObjectsPassingTest: ^ BOOL (id tr, NSUInteger index,BOOL
*te){
NSString
*s = (NSString *)tr;
if([s
isEqualToString:@"andy"]){
return YES;
}
return
NO;
}];
NSLog(@"%@",index);
33
、 这个方法添加了参数,用来表示,是从前向后,遍历还是从后向前遍历
- (NSIndexSet
*)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(id 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);
34、
添加参数NSIndexSet 用来获取子数组,使用子数组进行遍历
- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s
options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj,
NSUInteger idx,BOOL *stop))predicate
35、对数组进行排序操作
参数cmptr 是一个block 函数块,返回的数据类型是一个NSComparisonResult
对象
- (NSArray
*)sortedArrayUsingComparator:(NSComparator)cmptr
调用例子如下:
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);
36、进行排序操作,NSSortOptions
排序的参数 用来表示是同时排序,还是稳定执行。
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts
usingComparator:(NSComparator)cmptr
NS_AVAILABLE(10_6,4_0);
NSArray *test = [array
sortedArrayWithOptions:NSSortStable usingComparator:^
NSComparisonResult (NSString *s,NSString *s2){
if(s.length < s2.length){
return
NSOrderedAscending;
}
if(s.length > s2.length){
return
NSOrderedDescending;
}
return
NSOrderedSame;
}];
NSLog(@"%@",test);
37、指定区域返回通过代码块方法的索引
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