Skip to content

Commit

Permalink
给数组加上map方法
Browse files Browse the repository at this point in the history
  • Loading branch information
saucym committed Dec 4, 2019
1 parent 4a5d7b3 commit eaa2238
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions QMUIKit/UIKitExtensions/NSArray+QMUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@
* 过滤数组元素,将 block 返回 YES 的 item 重新组装成一个数组返回
*/
- (NSArray<ObjectType> *)qmui_filterWithBlock:(BOOL (^)(ObjectType item))block;

/**
* 转换数组元素,将每个 item 都经过 block 转换成一遍 返回转换后的新数组
*/
- (NSArray *)qmui_mapWithBlock:(id (^)(ObjectType item))block;

@end
12 changes: 12 additions & 0 deletions QMUIKit/UIKitExtensions/NSArray+QMUI.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ - (NSArray *)qmui_filterWithBlock:(BOOL (^)(id))block {
return [result copy];
}

- (NSArray *)qmui_mapWithBlock:(id (^)(id item))block {
if (!block) {
return self;
}

NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:self.count];
for (NSInteger i = 0; i < self.count; i++) {
[result addObject:block(self[i])];
}
return [result copy];
}

@end

0 comments on commit eaa2238

Please sign in to comment.