gpt4 book ai didi

xcode - 如何知道 UITextView 中包含多少个单词

转载 作者:可可西里 更新时间:2023-11-01 02:19:01 25 4
gpt4 key购买 nike

我正在使用谷歌搜索制作 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")
}
}
}

最佳答案

对不起,我误解了你的问题......

//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)

关于xcode - 如何知道 UITextView 中包含多少个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32698339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com