这是一个理智的Objective-C Block实现吗?

我想要一个NSRegularExpression的variation0ѭ方法的变体,它采用块而不是模板。块的返回值将用作替换值。您可以想象,这比模板更灵活。有点像在Perl正则表达式中使用
/e
修饰符。 所以我写了一个类别来添加方法。这就是我想出的:
@implementation NSRegularExpression (Block)

- (NSString *)stringByReplacingMatchesInString:(NSString *)string
                                       options:(NSMatchingOptions)options
                                         range:(NSRange)range
                                    usingBlock:(NSString* (^)(NSTextCheckingResult *result))block
{
    NSMutableString *ret = [NSMutableString string];
    NSUInteger pos = 0;

    for (NSTextCheckingResult *res in [self matchesInString:string options:options range:range]) {
        if (res.range.location > pos) {
            [ret appendString:[string substringWithRange:NSMakeRange(pos, res.range.location - pos)]];
        }
        pos = res.range.location + res.range.length;
        [ret appendString:block(res)];
    }
    if (string.length > pos) {
        [ret appendString:[string substringFromIndex:pos]];
    }
    return ret;
}

@end
这是我第一次尝试使用Objective C中的块。感觉有点奇怪,但似乎运行良好。不过,我有几个问题: 这看起来像是实现这种方法的理智方式吗? 有没有办法用
-enumerateMatchesInString:options:range:usingBlock:
实现其内部?我试过了,但无法从块内分配到
pos
。但是如果有一种方法可以使它工作,那么传递NSMatchingFlags和BOOL并以与该方法相同的方式处理它们也会很酷。 DO-能? 更新 感谢Dave DeLong的回答,我有一个使用块的新版本:
@implementation NSRegularExpression (Block)

- (NSString *)stringByReplacingMatchesInString:(NSString *)string
                                       options:(NSMatchingOptions)options
                                         range:(NSRange)range
                                    usingBlock:(NSString * (^)(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop))block
{
    NSMutableString *ret = [NSMutableString string];
    __block NSUInteger pos = 0;

    [self enumerateMatchesInString:string options:options range:range usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
    {
        if (match.range.location > pos) {
            [ret appendString:[string substringWithRange:NSMakeRange(pos, match.range.location - pos)]];
        }
        pos = match.range.location + match.range.length;
        [ret appendString:block(match, flags, stop)];
    }];
    if (string.length > pos) {
        [ret appendString:[string substringFromIndex:pos]];
    }
    return [NSString stringWithString:ret];
}

@end
工作得很好,谢谢!     
已邀请:
能够从块内分配到
pos
就像更改声明一样简单:
NSUInteger pos = 0;
至:
__block NSUInteger pos = 0;
关于
__block
关键字的更多信息:
__block
变量     

要回复问题请先登录注册