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

前端 未结 3 2064
长情又很酷
长情又很酷 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:45

    The best I can come up with is:

    NSMutableArray *replacementArray = [NSMutableArray array];
    
    [originalArray enumerateObjectsUsingBlock:
         ^(id obj, NSUInteger index, BOOL *stop)
         {
              [replacementArray addObject:[[obj mutableCopy] autorelease]];
         }
    ];
    

    Which more or less just tells the originalArray to construct the for loop for you. And if anything it's more work than:

    NSMutableArray *replacementArray = [NSMutableArray array];
    
    for(id obj in originalArray)
       [replacementArray addObject:[[obj mutableCopy] autorelease]];
    

提交回复
热议问题