简体   繁体   English

无法分配类型为[AnyObject]的值tp类型为NSMutableArray的值

[英]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. NSMutableArrayAnyObject不相关,因此您不能轻易地向下转换类型。

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.

相关问题 无法分配类型 &#39;[String : AnyObject] 的值 - Cannot assign value of type '[String : AnyObject] 无法将NSMutableArray类型的值分配给UIBarButtonItem类型 - Cannot assign value of type NSMutableArray to type UIBarButtonItem 错误:无法分配类型为“AnyObject?”的值到“NSURL”类型的值 - Error: Cannot assign a value of type 'AnyObject?' to a value of type 'NSURL' 无法将类型AnyObject的值分配给字符串类型的值 - Cannot assign a value of type AnyObject to a value of type String 无法将类型&#39;NSArray&#39;的值分配给类型&#39;[AnyObject]&#39;的值 - Cannot assign a value of type 'NSArray' to a value of type '[AnyObject]' 无法将类型[AnyObject]的值分配给类型[UIViewController]的值? - Cannot assign a value of type '[AnyObject]' to a value of type '[UIViewController]?' 无法将String类型的值分配为AnyObject类型? 斯威夫特3.0 - Cannot assign value of type String to type AnyObject? Swift 3.0 使用 SwiftyJSON,数组不能将 AnyObject 类型的值分配给 String 类型 - Using SwiftyJSON, array cannot assign value of type AnyObject to type String 无法将类型为“ nil”的值分配给类型为“ NSMutableArray”的值 - Cannot assign a value of type 'nil' to a value of type 'NSMutableArray' 无法将类型&#39;(String ?, Bool,[AnyObject] ?, NSError?)-&gt;()&#39;的值分配给 - Cannot assign value of type '(String?, Bool, [AnyObject]?, NSError?) -> ()' to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM