![](/img/trans.png)
[英]Search bar is not working in swift 3. Cannot convert value of type (_) -> Bool to expected argument type NSPredicate
[英]Cannot convert value of type '(_) -> Bool' to expected argument type 'NSPredicate'
我正在使用Swift 3中的filter()方法,但遇到问题,我的代码是....
filtered = arrayTag.filter(using: { (text) -> Bool in
//Access the title and sectors
let tmpTitle = text["tag_name"] as! String
let tmpSector = text["tag_id"] as! String
//Create a range for both
let range1 = tmpTitle.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
let range2 = tmpSector.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
print("search result \(text)")
//Return true if either match
return range1 != nil || range2 != nil
})
NSMutableArray
对象的方法filter(using:)
将NSPredicate
作为参数。 要使用filter
功能,请更改以下代码:
filtered = arrayTag.filter { text in
//your code
}
这是上述问题的解决方案
let tmpTitle = (text as! NSDictionary)["tag_name"] as? String
把文字as! NSDictionary
as! NSDictionary
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.