代码之家  ›  专栏  ›  技术社区  ›  Sean

使用Objective-C块

  •  17
  • Sean  · 技术社区  · 16 年前

    @interface NSArray (FunWithBlocks)
    - (NSArray *)collect:(id (^)(id obj))block;
    - (NSArray *)select:(BOOL (^)(id obj))block;
    - (NSArray *)flattenedArray;
    @end
    

    NSArray *packagePaths = [[[NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES) collect:^(id path) { return (id)[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil] collect:^(id file) { return (id)[path stringByAppendingPathComponent:file]; }]; }] flattenedArray] select:^(id fullPath) { return [[NSWorkspace sharedWorkspace] isFilePackageAtPath:fullPath]; }];
    

    NSMutableArray *packagePaths = [NSMutableArray new];
    for (NSString *searchPath in NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES)) {
        for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:searchPath error:nil]) {
            NSString *packagePath = [searchPath stringByAppendingPathComponent:file];
            if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:packagePath]) {
                [packagePaths addObject:packagePath];
            }
        }
    }
    

    3 回复  |  直到 11 年前
        1
  •  19
  •   bbum    16 年前

    使用换行符,将通话拆分为多条线路。

    你已经做到了。很好。

    现在,当编写使用所述API的代码时,请执行以下操作:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES);
    paths = [paths collect: ^(id path) {
        ...
    }];
    paths = [paths collect: ^(id path) {
        ...
    }];
    paths = [paths select: ^(id path) {
        ...
    }];
    

    即,将收集/选择/过滤/展平/映射/任何步骤作为单独的步骤进行。这不会比链式方法调用更快/更慢。

    如果您确实需要在块的侧面嵌套块,那么请使用完整缩进:

    paths = [paths collect: ^(id path) {
        ...
        [someArray select:^(id path) {
            ...
        }];
    }];
    

    就像嵌套if语句等。当它变得太复杂时,根据需要将其重构为函数或方法。

        2
  •  2
  •   Rayfleck    12 年前

    NSArray *packagePaths = [[[NSSearchPathForDirectoriesInDomains(NSAllApplicationsDirectory, NSAllDomainsMask, YES) 
                                collect:^(id path) {
                                          return [[[NSFileManager defaultManager] 
                                                     contentsOfDirectoryAtPath:path 
                                                                         error:nil] 
                                                   collect:^(id file) { 
                                                             return [path stringByAppendingPathComponent:file]; 
                                                            }
                                                 ]; 
                                         }
                                ] 
                                flattenedArray
                              ] 
    
                              select:^(id fullPath) { 
                                       return [[NSWorkspace sharedWorkspace] isFilePackageAtPath:fullPath]; 
                                     }
                             ];
    

        3
  •  0
  •   NSResponder    16 年前

    看起来你正在重新发明高阶消息。Marcel Weiher在Objective-C中的HOM方面做了大量的工作,你可以在这里找到:

    http://www.metaobject.com/blog/labels/Higher%20Order%20Messaging.html