Swift:检查UISearchBar.text是否包含网址 [英] Swift: check if UISearchBar.text contains a url

查看:107
本文介绍了Swift:检查UISearchBar.text是否包含网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查UISearchBar.text是否包含URL?我想做这样的事情:

How can I check if UISearchBar.text contains a URL? I thought of doing something like this:

if (searchBar.text == NSTextCheckingType.Link) {

}

但我得到了错误:

String is not convertible to NSObject


推荐答案

在Swift 3中,您可以使用 NSDataDetector NSDataDetector 有一个名为 init(types) init(types)具有以下声明:

With Swift 3, you can use NSDataDetector. NSDataDetector has an initializer called init(types:). init(types:) has the following declaration:

init(types checkingTypes: NSTextCheckingTypes) throws




初始化并返回数据检测器实例。 / p>

Initializes and returns a data detector instance.

要创建一个查找URL的数据检测器,您必须传递 NSTextCheckingResult.CheckingType.link 作为 init(types)

In order to create a data detector that finds urls, you have to pass NSTextCheckingResult.CheckingType.link as the parameter for init(types:).

作为 NSRegularExpression 的子类, NSDataDetector 具有一种名为 enumerateMatches(in :options:range:using:) enumerateMatches(in:options:range:using:)具有以下声明:

As a subclass of NSRegularExpression, NSDataDetector has a method called enumerateMatches(in:options:range:using:). enumerateMatches(in:options:range:using:) has the following declaration:

func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void)




枚举允许Block处理每个正则表达式匹配项的字符串。

Enumerates the string allowing the Block to handle each regular expression match.

下面的游乐场代码显示了如何使用 NSDataDetector enumerateMatches(in: options:range:using:)方法,以检测 String 是否包含 URL s:

The Playground code below shows how to use NSDataDetector and enumerateMatches(in:options:range:using:) method in order to detect if a String contains URLs:

import Foundation

let testString = " lorem http://www.yahoo.com ipsum google.fr"

do {
    let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let range = NSRange(location: 0, length: testString.characters.count)
    let block = { (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
        if let result = result, result.resultType == NSTextCheckingResult.CheckingType.link {
            print(result.url)
        }
    }
    detector.enumerateMatches(in: testString, options: [], range: range, using: block)
} catch {
    print(error)
}

/*
 prints:
 Optional(http://www.yahoo.com)
 Optional(http://google.fr)
 */






#2。使用 NSDataDetector NSRegularExpression matches(in:options:range:)方法



作为 NSRegularExpression 的子类, NSDataDetector 有一个名为 matches(in:options :range:) matches(in:options:range:)具有以下声明:


#2. Using NSDataDetector and NSRegularExpression's matches(in:options:range:) method

As a subclass of NSRegularExpression, NSDataDetector has a method called matches(in:options:range:). matches(in:options:range:) has the following declaration:

func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult]




返回包含正则表达式所有匹配项的数组

Returns an array containing all the matches of the regular expression in the string.






这是调用 enumerateMatches(in:options:range:using:)传递适当的字符串,选项和范围。

This is a convenience method that calls enumerateMatches(in:options:range:using:) passing the appropriate string, options, and range.

下面的Playground代码显示了如何使用 NSDataDetector matches(in:options:range:)方法,以检测 String 是否包含 URL s:

The Playground code below shows how to use NSDataDetector and matches(in:options:range:) method in order to detect if a String contains URLs:

import Foundation

let testString = " lorem http://www.yahoo.com ipsum google.fr"

do {
    let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
    let range = NSRange(location: 0, length: testString.characters.count)
    let resultArray = detector.matches(in: testString, options: [], range: range)
    for result in resultArray {
        if result.resultType == NSTextCheckingResult.CheckingType.link {
            print(result.url)
        }
    }
} catch {
    print(error)
}

/*
 prints:
 Optional(http://www.yahoo.com)
 Optional(http://google.fr)
 */

这篇关于Swift:检查UISearchBar.text是否包含网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆