不兼容的块指针类型,当它应该为void时发送NSString

时间:2014-03-22 15:27:43

标签: ios objective-c regex pointers objective-c-blocks

- (NSString *)userIDRegex:(NSData *)data {

    NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //Create a regular expression
    NSString *regexStr = @"tr id=\"[0-9]*\"";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];

    //Enumerate all matches
    if ((regex==nil) && (error!=nil)){
        NSLog(@"Regex failed for url: %@, error was: %@", string, error);
    } else {
        [regex enumerateMatchesInString:string
        options:0
        range:NSMakeRange(0, [string length])
        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){
            if (result!=nil){
                // iterate ranges
                for (int i=0; i<[result numberOfRanges]; i++) {
                    NSRange range = [result rangeAtIndex:i];
                    NSLog(@"%ld,%ld group #%d %@", range.location, range.length, i, (range.length==0 ? @"--" : [string substringWithRange:range]));
                    return [string substringWithRange:range];
                }
            }
            return @"Regex failed";
        }];

}

usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){行中,编译器给出了一个错误Incompatible block pointer types sending 'NSString *(^)(NSTextCheckingResult *__strong, NSMatchingFlags, BOOL *)' to parameter of type 'void (^)(NSTextCheckingResult *__strong, NSMatchingFlags, BOOL *)'

我不确定问题出在哪里,我在项目中的另一个方法中遇到了同样的问题,其中NSString应该是void 。我之前从未遇到过这个错误。

1 个答案:

答案 0 :(得分:3)

该块不能具有返回值。尝试这样的事情:

- (NSString *)userIDRegex:(NSData *)data {
    NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //Create a regular expression
    NSString *regexStr = @"tr id=\"[0-9]*\"";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];

    __block NSString *myResult = nil;
    //Enumerate all matches
    if ((regex==nil) && (error!=nil)){
        NSLog(@"Regex failed for url: %@, error was: %@", string, error);
    } else {
        [regex enumerateMatchesInString:string
        options:0
        range:NSMakeRange(0, [string length])
        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){
            if (result!=nil){
                // iterate ranges
                for (int i=0; i<[result numberOfRanges]; i++) {
                    NSRange range = [result rangeAtIndex:i];
                    NSLog(@"%ld,%ld group #%d %@", range.location, range.length, i, (range.length==0 ? @"--" : [string substringWithRange:range]));
                    myResult = [string substringWithRange:range];
                    *stop = YES;
                }
            } else {
                myResult = @"Regex failed";
                *stop = YES;
            }
        }];
    }

    return myResult;
}