-1

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

7
  • 2
    Add your code and error as text.It will help for answer to the question. Commented May 8, 2020 at 8:20
  • 1
    Why do you need NSMutableDictionary in this case? Commented May 8, 2020 at 8:20
  • 1
    Also as @Dilan mentioned, you have to include code as Plain text, so that search engines can find. Commented May 8, 2020 at 8:22
  • are you sure you have dictionaries on each index of the array ? Commented May 8, 2020 at 8:29
  • The exception message tells you that the array contains an empty array, not a mutable dictionary. You should basically never use NS types in swift. Use properly typed Swift collections and you will catch errors like this at compile time. Commented May 8, 2020 at 9:52

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.