前言
开发中经常遇到要对数组进行排序、创建、截取,特整理一个分类记录之
参考:
NSArray+LonelyFlowTool.h
@interface NSArray (LonelyFlowTool)
#pragma mark - 截取指定范围的数据
- (NSArray *)lf_subArrayWithRange:(NSRange)range;
- (NSArray *)lf_subArrayFromIndex:(NSInteger)index;
- (NSArray *)lf_subArrayToIndex:(NSInteger)index;
#pragma mark - 数组排序
// 对数组里的内容进行排序,默认升序
- (NSArray *)lf_sort;
- (NSArray *)lf_sortWithAscending:(BOOL)ascending;
// 以key进行升序排序,默认升序
- (NSArray *)lf_sortWithKey:(NSString *)key;
- (NSArray *)lf_sortWithKey:(NSString *)key ascending:(BOOL)ascending;
#pragma mark 创建有默认数据的数组
/**
创建一个有填充内容的数组
创建指定长度,并给每个位置都设置默认数据的数组
@param count 数组长度
@param fillBlock 填充默认数据的回调
@return 创建好的数组
*/
+ (NSMutableArray *)lf_fillCount:(NSInteger)count fillBlock:(id(^)(NSInteger index))fillBlock;
+ (NSMutableArray *)lf_fillCount:(NSInteger)count fillObj:obj;
#pragma mark - 创建一个数字数组
+ (NSMutableArray *)lf_fillNumFrom:(NSInteger)from to:(NSInteger)to;
+ (NSMutableArray *)lf_fillNumFrom:(NSInteger)from to:(NSInteger)to isTwo:(BOOL)isTwo;
@end
NSArray+LonelyFlowTool.m
@implementation NSArray (LonelyFlowTool)
#pragma mark - 截取指定范围的数据
/**
从一个数组里截取一个子数组,如果需要截取的长度大于数据的长度,以数据长度为准
@param range 要截取的位置
@return 截取后返回的数组
*/
- (NSArray *)lf_subArrayWithRange:(NSRange)range
{
// 没有数据,则返回空数据
if (self.count == 0) {
return @[];
}
NSInteger from = range.location;
// 如果开始位置大于数据长度,则返回空数组
if (from>=self.count) {
return @[];
}
NSInteger to = range.location+range.length-1;
// 如果结束位置大于数据长度,则以数据的最后一个为结束位置
if (to>=self.count) {
to = self.count-1;
}
return [self subarrayWithRange:NSMakeRange(from, to-from+1)];
}
- (NSArray *)lf_subArrayFromIndex:(NSInteger)index
{
NSRange range = NSMakeRange(index, self.count-index);
return [self lf_subArrayWithRange:range];
}
- (NSArray *)lf_subArrayToIndex:(NSInteger)index
{
NSRange range = NSMakeRange(0, index+1);
return [self lf_subArrayWithRange:range];
}
#pragma mark - 数组排序
// 以key进行升序排序
- (NSArray *)lf_sort
{
return [self lf_sortWithKey:nil ascending:YES];
}
- (NSArray *)lf_sortWithAscending:(BOOL)ascending
{
return [self lf_sortWithKey:nil ascending:ascending];
}
- (NSArray *)lf_sortWithKey:(NSString *)key
{
return [self lf_sortWithKey:key ascending:YES];
}
- (NSArray *)lf_sortWithKey:(NSString *)key ascending:(BOOL)ascending
{
NSSortDescriptor *sorter = nil;
if(key != nil && [key hasPrefix:@"@"]){
// 对keypath进行适配
NSArray *keys = [key componentsSeparatedByString:@"."];
NSString *prefix = [NSString stringWithFormat:@"%@.self",keys.firstObject];
NSString *realKey = [[keys lf_subArrayFromIndex:1] componentsJoinedByString:@"."];
sorter = [NSSortDescriptor sortDescriptorWithKey:realKey ascending:ascending comparator:^NSComparisonResult(id obj1, id obj2) {
id obj11 = [obj1 valueForKeyPath:prefix];
id obj21 = [obj2 valueForKeyPath:prefix];
if (obj11 == obj21) {
return NSOrderedSame;
} else if (obj11 > obj21){
// 当obj1大于obj2时, 本应该返回 NSOrderedDescending , 这里反转其结果, 使返回 NSOrderedAscending
if(ascending){
return NSOrderedDescending;
}else{
return NSOrderedAscending;
}
} else {
if(ascending){
return NSOrderedAscending;
}else{
return NSOrderedDescending;
}
}
}];
}else{
sorter = [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
}
return [self sortedArrayUsingDescriptors:@[sorter]];
}
@end
使用排序demo:
// 简单数字排序
NSArray *arr1 = @[@1,@5,@3,@8];
NSArray *arr12 = [arr1 lf_sort];
NSArray *arr13 = [arr1 trend_sortWithAscending:NO];
// 简单文本排序
NSArray *arr2 = @[@"abc",@"xyz",@"acb",@"hello"];
NSArray *arr22 = [arr2 trend_sort];
NSArray *arr23 = [arr2 trend_sortWithAscending:NO];
// 复杂类型父NSIndexPath排序
NSArray *arr3 = @[[NSIndexPath indexPathForRow:1 inSection:1],[NSIndexPath indexPathForRow:0 inSection:0],[NSIndexPath indexPathForRow:2 inSection:0]];
NSArray *arr32 = [arr3 trend_sort];
NSArray *arr33 = [arr3 trend_sortWithAscending:NO];
// 复杂的字典类型或自定义对象类型排序
NSArray *arr4 = @[@{@"age":@10,@"name":@"bom",@"nums":@[@1,@9,@3],@"index":[NSIndexPath indexPathForRow:1 inSection:1]},
@{@"age":@8,@"name":@"stv",@"nums":@[@3,@8,@5],@"index":[NSIndexPath indexPathForRow:0 inSection:0]},
@{@"age":@30,@"name":@"gates",@"nums":@[@6,@2,@4],@"index":[NSIndexPath indexPathForRow:2 inSection:0]}];
NSArray *arr44 = [arr4 trend_sortWithKey:@"age"];
NSArray *arr45 = [arr4 trend_sortWithKey:@"age" ascending:NO];
NSArray *arr46 = [arr4 trend_sortWithKey:@"index"];
NSArray *arr47 = [arr4 trend_sortWithKey:@"@sum.nums"];
NSArray *arr48 = [arr4 trend_sortWithKey:@"@min.nums"];
NSArray *arr49 = [arr4 trend_sortWithKey:@"@max.nums"];
NSArray *arr410 = [arr4 trend_sortWithKey:@"@avg.nums"];
NSArray *arr411 = [arr4 trend_sortWithKey:@"@count.nums"];
OC常见KVC编码
简单集合运算符返回一个NSNumber
共有5种:
@avg
, @count
, @max
, @min
,@sum
NSArray* arrBooks = @[book1,book2,book3,book4];
NSNumber* sum = [arrBooks valueForKeyPath:@"@sum.price"];
对象运算符比集合运算符稍微复杂,能以数组的方式返回指定的内容,一共有两种:
@distinctUnionOfObjects
,@unionOfObjects
NSArray *list = @[@{@"name":@"jim",@"grade":@89},
@{@"name":@"tom",@"grade":@77},
@{@"name":@"jobs",@"grade":@77}];
// 获取key的值组合成一个数组->[89,77,77]
NSArray *list2 = [list valueForKeyPath:@"@unionOfObjects.grade"];
// 获取key的值组合成一个数组并去重->[89,77]
NSArray *list3 = [list valueForKeyPath:@"@distinctUnionOfObjects.grade"];
网友评论