convert NSArray of NSString(s) to NSArray of NSMutableString(s)

前端 未结 3 2065
长情又很酷
长情又很酷 2021-01-26 06:35

How to do that without having to \"scroll\" the entire given array with a \"for\" loop?

3条回答
  •  借酒劲吻你
    2021-01-26 06:55

    I wrote a dictionary method on NSArray to be able to write cleaner functional code

    -(NSArray *)arrayByPerformingBlock:(id (^)(id))performBlock 
    {
        NSMutableArray *array = [NSMutableArray array];
        for (id element in self)
             [array addObject:performBlock(element)];
        return [NSArray arrayWithArray:array];
    }
    

    usage:

    arrayWithStrings = [arrayWithStrings arrayByPerformingBlock:^id(id element) {return [[element mutableCopy] autorelease];}];
    

    This was inspired by list comprehensions I know from Python. I also wrote versions of this methods with testing. See my arraytools.

提交回复
热议问题