簡體   English   中英

當此字符串包含NSDataDetector的中文時,無法分析NSString中的url

[英]Can not analyse url in NSString when this string contains chinese by NSDataDetector

沒有匹配項============================================== ======

我知道,這是“空白”的問題,如果我在“ 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];
    }  
}

這是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輸入=“這是一個檢測到URLhttps://www.sharpkits.com的測試。”

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

所以用NSDataDetector不能完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM