Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Add one function : map NSArray and skip someone #120

@Yrocky

Description

@Yrocky

Sometimes not only need to map out an array, but also need to conditional mapping. Reference to NSArray's block traversal method - (void) enumerateObjectsUsingBlock: (void (NS_NOESCAPE ^) (ObjectType obj, NSUInteger idx, BOOL * stop)) block.

Add a skip option for the mapping method:

- (NSArray *) mm_mapWithskip:(id (^)(id obj, BOOL *skip))callback{
    
    NSMutableArray * _self = [NSMutableArray arrayWithCapacity:self.count];
    
    for( id obj in self ){
        
        BOOL skip = NO;
        
        id mapObj = callback(obj, &skip);
        
        if( !skip ){
            [_self addObject:mapObj];
        }
    }
    return [_self copy];
}

Refer to this answer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions