how can i add one NSMutablearray to another empty NSmutablearray?

53 Views Asked by At

I am using NSMutableDictionary for storing data into key value pair and append these dictionary to NSMutableArray.I want to assign my filtered NSMUtablearray to another empty NSMUtablearray, but can not get result and app is crash dunring searching some text..

here is my code:

extension ViewController: UISearchBarDelegate, UISearchDisplayDelegate
{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchText)
    let result = ArrData.filtered(using: searchPredicate)
    searchCoin.add(result)
    searchActive = true
    self.tableView.reloadData()
}
}

crash is:

enter image description here

1

There are 1 best solutions below

0
Володимир Ukraine On

Just change the code from

 let dict = self.filteredArray.object(at: IndexPath.row) as! NSMutableDictionary 

to

 let dictOptional = self.filteredArray.object(at: IndexPath.row) as? NSMutableDictionary
 guard let dict = dictOptional else {
      print("Error at: \(IndexPath.row) - can't convert to dictionary")
 }

If You want more - write the types of ArrData, filteredArray - how You create and use it