1

没有匹配================================================ ======

我知道,这是“空格”的问题,如果我在“https”前面添加空格,那么它就可以了。那么NSDataDetector不能解决这个问题吗?

NSString *originString = @"【mans】下单立减5.00元https://kdt.im/uYMI4r";
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches1 = [linkDetector matchesInString:originString options:0 range:NSMakeRange(0, [originString length])];

for (NSTextCheckingResult *match in matches1) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
    }  
}
4

1 回答 1

3

这是一个已知问题NSDataDetector,如果协议之前有一个空格,即 http:// 或 https:// ,那么它可以工作,否则它不会。

例如

    let input = "This is a test with the URL https://www.sharpkits.com to be detected."
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))

for match in matches {
    let url = (input as NSString).substring(with: match.range)
    print(url)
}

输出: https ://www.sharpkits.com

并且对于 let input = "这是一个带有 URLhttps://www.sharpkits.com 的测试,将被检测到。"

输出:URL https://www.sharpkits.com

所以不能用NSDataDetector

于 2016-10-11T06:10:34.363 回答