[英]Cannot Assign a value of type [AnyObject] tp a value of type NSMutableArray
I am trying to get the result of a search using NSPredicate however it conflicts when I try to use filteredArrayUsingPredicate since it returns AnyObject. 我正在尝试使用NSPredicate获取搜索的结果,但是当我尝试使用filterArrayUsingPredicate时会发生冲突,因为它返回了AnyObject。 I've commented out the method I attempted since it's not returning the proper results.
我已经注释掉了我尝试的方法,因为它没有返回正确的结果。
var contacts: NSMutableArray = []
var filteredContacts: NSMutableArray = []
func newFilteringPredicateWithText(text: String) -> NSPredicate {
return NSPredicate(format:"SELF contains[cd] %@", text);
}
func contactPickerTextViewDidChange(textViewText: String!) {
if (textViewText == "") {
self.filteredContacts = self.contacts;
}
else {
var predicate: NSPredicate = self.newFilteringPredicateWithText(textViewText)
self.filteredContacts = self.contacts.filteredArrayUsingPredicate(predicate)
//var temp = NSMutableArray(array: self.contacts.filteredArrayUsingPredicate(predicate))
//self.filteredContacts = temp
}
self.tableView.reloadData()
}
NSMutableArray
and AnyObject
are not related therefore you can't easily downcast the type. NSMutableArray
和AnyObject
不相关,因此您不能轻易地向下转换类型。
I'd suggest a native Swift solution 我建议使用本机Swift解决方案
var contacts: Array<String> = []
var filteredContacts: Array<String> = []
func contactPickerTextViewDidChange(textViewText: String!) {
if textViewText.isEmpty {
filteredContacts = contacts
}
else {
filteredContacts = contacts.filter {$0.rangeOfString(textViewText, options: [.CaseInsensitiveSearch, .DiacriticInsensitiveSearch]) != nil }
}
tableView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.