【问题标题】:how to know how many words contains in UITextView如何知道 UITextView 中包含多少个单词
【发布时间】:2015-09-21 14:52:49
【问题描述】:

我正在使用谷歌搜索制作UISearchController。我面临一个问题,UISearchBar.text 中包含多少个单词,我可以做出不同的动作。或者如何分隔不同的单词被分隔的空间? 我也想知道,如何计算UITextView 中的单词。

我的尝试

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    let text = searchController.searchBar.text!
    if text.containsString("www.") {
        let url = NSURL(string: "http://\(text)")!
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
    } else {
        if text.containsString(" ") == false {
        let url = NSURL(string: "https://www.google.com/#newwindow=1&q=\(text)")!
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
        } else {
            let url = NSURL(string: "https://www.google.com/#newwindow=1&q=\(text)")! // it is wrong
            let request = NSURLRequest(URL: url)
            webView.loadRequest(request)
            print("two words")
        }
    }
}

【问题讨论】:

  • 如果我知道了,NSString 的 componentsSeparatedByString 会做你需要的

标签: xcode swift text swift2 uisearchcontroller


【解决方案1】:

对不起,我看错了你的问题...

//Separate all words into an array
let allText = SearchText.text.componentsSeparatedByString(" ")

//then count the array
var wordsCount:Int = allText.count

要计算 UITextView.text 的字符数,您可以使用 count(UISearchBar.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    相关资源
    最近更新 更多