Reputation: 6861
I am making UISearchController
with google search. And I face with problem, how many words contain in UISearchBar.text
that I will can make different actions. Or how to separate different words are separated space?
Also I want to know, how to count words in UITextView
.
My try
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")
}
}
}
Upvotes: 2
Views: 91
Reputation: 858
Sorry, I misread your question...
//Separate all words into an array
let allText = SearchText.text.componentsSeparatedByString(" ")
//then count the array
var wordsCount:Int = allText.count
To count chars of a UITextView.text you could use count(UISearchBar.text)
Upvotes: 1