首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当此字符串由NSString包含中文时,无法分析NSDataDetector中的url

当此字符串由NSString包含中文时,无法分析NSDataDetector中的url
EN

Stack Overflow用户
提问于 2016-10-11 05:22:36
回答 1查看 182关注 0票数 1

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

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

代码语言:javascript
复制
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];
    }  
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-11 06:10:34

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

例如:

代码语言:javascript
复制
    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 https://www.sharpkits.com=“这是要检测到的URL的测试。”

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

所以不能用NSDataDetector

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39970947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档