Skip to main content
edited tags
Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129
edited tags
Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129
edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Elegant way to split Splitting an NSArray into an NSDictionary of array more elegantly

As suggested by Ammal i changed the code so this is the current version:

-(NSDictionary *)groupUsingBlock:(id<NSCopying> (^)(id obj))block {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    for (id obj in self) {
        id<NSCopying> key = block(obj);
        if (! dictionary[key]) {
            NSMutableArray *arr = [NSMutableArray array];
            dictionary[key] = arr;
        }
        [dictionary[key] addObject:obj];
    }
    return [dictionary copy];
}

I've tryed even a concurrent version, but unfortunately doesn't works

-(NSDictionary *)groupUsingBlockOptimized:(id<NSCopying> (^)(id obj))block {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    
    [self enumerateObjectsWithOptions:NSEnumerationConcurrent
                           usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
                               id<NSCopying> key = block(obj);
                               if (! dictionary[key]) {
                                   NSMutableArray *arr = [NSMutableArray array];
                                   dictionary[key] = arr;
                               }
                               [dictionary[key] addObject:obj];
                           }];
    return [dictionary copy];
    
}

Elegant way to split an NSArray into NSDictionary of array

As suggested by Ammal i changed the code so this is the current version:

-(NSDictionary *)groupUsingBlock:(id<NSCopying> (^)(id obj))block {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    for (id obj in self) {
        id<NSCopying> key = block(obj);
        if (! dictionary[key]) {
            NSMutableArray *arr = [NSMutableArray array];
            dictionary[key] = arr;
        }
        [dictionary[key] addObject:obj];
    }
    return [dictionary copy];
}

I've tryed even a concurrent version, but unfortunately doesn't works

-(NSDictionary *)groupUsingBlockOptimized:(id<NSCopying> (^)(id obj))block {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    
    [self enumerateObjectsWithOptions:NSEnumerationConcurrent
                           usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
                               id<NSCopying> key = block(obj);
                               if (! dictionary[key]) {
                                   NSMutableArray *arr = [NSMutableArray array];
                                   dictionary[key] = arr;
                               }
                               [dictionary[key] addObject:obj];
                           }];
    return [dictionary copy];
    
}

Splitting an NSArray into an NSDictionary of array more elegantly

posted my last version
Source Link
TheObjCGuy
  • 163
  • 1
  • 1
  • 4
Loading
deleted 11 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
TheObjCGuy
  • 163
  • 1
  • 1
  • 4
Loading