I am having a very weird problem which I am not able to figure it out of what is going wrong here.
Here is my code
func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
newTableData = [String]()
for var i = 0; i < self.dict.count - 1; i++ {
let cityName = (((self.dict["\(i)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
let countryName = (((self.dict["\(i)"] as?NSDictionary)!["Country"] as?NSDictionary)!["name"] as?NSString)! as String
print("countryName is \(countryName)")
newTableData.append(cityName)
}
print("newTableData is \(newTableData)" )
let array = (newTableData as NSArray).filteredArrayUsingPredicate(searchPredict)
print("array is\(array)")
filterTableData = array as! [String]
self.tableView.reloadData()
}
Program is crashing at this line
let countryName = (((self.dict["\(i)"] as?NSDictionary)!["Country"] as?NSDictionary)!["name"] as?NSString)! as String
when I type first character in the search box all works fine but as soon as I type second character in the searchBox
program crashes and it gives me this error
Could not cast value of type 'NSNull' (0x1a03c3768) to 'NSString' (0x1a03cd798).
and for your Information countryNames exist in the dict
variable but I don't know why its giving me null value and why city name is successfully working because country also exist in the same array from which I am getting the city
UPDATE:
This line prints country name successfully
print("countryName is \(countryName)")