문제

- (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";
        }];

}

In the usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){ line, the compiler is giving me an error saying Incompatible block pointer types sending 'NSString *(^)(NSTextCheckingResult *__strong, NSMatchingFlags, BOOL *)' to parameter of type 'void (^)(NSTextCheckingResult *__strong, NSMatchingFlags, BOOL *)'.

I'm not sure where the problem is, I'm also having the same problem in another method in my project, where it has a NSString where it should be void. I've never encountered this error before.

도움이 되었습니까?

해결책

The block can't have return value. Try something like this:

- (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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top