Skip to content

Commit

Permalink
数组和字典支持泛型标记,字典模仿swift支持mapValues方法
Browse files Browse the repository at this point in the history
  • Loading branch information
jueying committed Apr 20, 2020
1 parent 7aec75c commit 36f5c8d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
18 changes: 9 additions & 9 deletions JKCategories/Foundation/NSArray/NSArray+JKBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#import <Foundation/Foundation.h>

@interface NSArray (JKBlock)
- (void)jk_each:(void (^)(id object))block;
- (void)jk_eachWithIndex:(void (^)(id object, NSUInteger index))block;
- (NSArray *)jk_map:(id (^)(id object))block;
- (NSArray *)jk_filter:(BOOL (^)(id object))block;
- (NSArray *)jk_reject:(BOOL (^)(id object))block;
- (id)jk_detect:(BOOL (^)(id object))block;
- (id)jk_reduce:(id (^)(id accumulator, id object))block;
- (id)jk_reduce:(id)initial withBlock:(id (^)(id accumulator, id object))block;
@interface NSArray<__covariant ObjectType> (JKBlock)
- (void)jk_each:(void (^)(ObjectType object))block;
- (void)jk_eachWithIndex:(void (^)(ObjectType object, NSUInteger index))block;
- (NSArray *)jk_map:(id (^)(ObjectType object))block;
- (NSArray *)jk_filter:(BOOL (^)(ObjectType object))block;
- (NSArray <ObjectType>*)jk_reject:(BOOL (^)(ObjectType object))block;
- (ObjectType)jk_detect:(BOOL (^)(ObjectType object))block;
- (id)jk_reduce:(id (^)(id accumulator, ObjectType object))block;
- (id)jk_reduce:(id)initial withBlock:(id (^)(id accumulator, ObjectType object))block;
@end
11 changes: 6 additions & 5 deletions JKCategories/Foundation/NSDictionary/NSDictionary+JKBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

#import <Foundation/Foundation.h>

@interface NSDictionary (JKBlock)
@interface NSDictionary<__covariant KeyType, __covariant ObjectType> (JKBlock)

#pragma mark - RX
- (void)jk_each:(void (^)(id k, id v))block;
- (void)jk_eachKey:(void (^)(id k))block;
- (void)jk_eachValue:(void (^)(id v))block;
- (NSArray *)jk_map:(id (^)(id key, id value))block;
- (void)jk_each:(void (^)(KeyType k, ObjectType v))block;
- (void)jk_eachKey:(void (^)(KeyType k))block;
- (void)jk_eachValue:(void (^)(ObjectType v))block;
- (NSArray *)jk_map:(id (^)(KeyType key, ObjectType value))block;
- (NSDictionary <KeyType, id>*)jk_mapValues:(id (^)(ObjectType value))block;
- (NSDictionary *)jk_pick:(NSArray *)keys;
- (NSDictionary *)jk_omit:(NSArray *)key;

Expand Down
13 changes: 13 additions & 0 deletions JKCategories/Foundation/NSDictionary/NSDictionary+JKBlock.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ - (NSArray *)jk_map:(id (^)(id key, id value))block {
return array;
}

- (NSDictionary *)jk_mapValues:(id (^)(id value))block
{
NSMutableDictionary *mapped = [NSMutableDictionary dictionaryWithCapacity:self.count];

[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
id object = block(obj);
if (object) {
[mapped setObject:block(obj) forKey:key];
}
}];

return mapped;
}

- (NSDictionary *)jk_pick:(NSArray *)keys {
NSMutableDictionary *picked = [[NSMutableDictionary alloc] initWithCapacity:keys.count];
Expand Down

0 comments on commit 36f5c8d

Please sign in to comment.