如何在搜索栏中输入文本长度?

时间:2014-07-14 15:03:11

标签: swift uisearchbar

我想获得在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

1 个答案:

答案 0 :(得分:2)

问题很简单,text属性,就像来自Cocoa API的大多数对象一样,可能是零。你必须向Swift保证它不是。像这样:

let len = countElements(searchBar.text!)