0

テキストからすべての URL を削除しようとしています:

- (NSString *)cleanText:(NSString *)text{
    NSString *string = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];

    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypeLink) {
            NSString *matchingString = [match description];
            NSLog(@"found URL: %@", matchingString);
            string = [string stringByReplacingOccurrencesOfString:matchingString withString:@""];
        }
    }
    NSLog(string);
    return string;
}

ただしstring、変更されずに返されます (一致があります)。

更新:コンソール出力:

found URL: <NSLinkCheckingResult: 0xb2b03f0>{22, 36}{http://abc.com/efg.php?EFAei687e3EsA}
2013-10-02 20:19:52.772 
This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it and a number 097843.

@Raphael Schweikert が作成したすぐに使えるレシピ。

4

2 に答える 2