我在这段代码中使用布尔标识符时遇到了困难。
func WorkTypeSearchable() {
let pathToFile = NSBundle.mainBundle().pathForResource("work", ofType: "txt")
if let path = pathToFile {
let workString = try! NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
_ = workString.componentsSeparatedByString("\n")
tblSearchResults.reloadData()
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = searchController.searchBar.text
FilteredWorkType = WorkTypeSearchable( { (work) -> Bool in
let workText: NSString = work
return (workText.rangeofString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
})
tblSearchResults.reloadData()
}
在FilteredWorkType = WorkTypeSearchable( { (work) -> Bool in
行中,我无法使用它,因为它崩溃了应用程序。但它给我的错误,我无法理解。
(_) - > Bool不能转换为()
我猜这与Swift 1到Swift 2的变化有关。任何人都可以帮我纠正这个错误并解释为什么我会收到这个错误吗?
我从代码的这一部分调用WorkTypeSearchable
。
func WorkTypeSearchable() {
let pathToFile = NSBundle.mainBundle().pathForResource("work", ofType: "txt")
if let path = pathToFile {
let workString = try! NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
_ = workString.componentsSeparatedByString("\n")
tblSearchResults.reloadData()
}
}
`