我想获得在UISearchBar
我使用在searcbbar文本开始编辑时调用的函数。
func searchBarTextDidBeginEditing(searchBar: UISearchBar!) {
println("EDITING")
//TODO: Get length of text entered in searchBar
}
这不起作用:
func searchBarTextDidBeginEditing(searchBar: UISearchBar!) {
println("EDITING")
//TODO: Get length of text entered in searchBar
//Doesn't work. Error: String doesn't have a member named length
searchBar.text.length
}
如何检索输入文本的长度?
结束这样做:
searchBar.text.bridgeToObjectiveC().length
答案 0 :(得分:2)
问题很简单,text
属性,就像来自Cocoa API的大多数对象一样,可能是零。你必须向Swift保证它不是。像这样:
let len = countElements(searchBar.text!)