Cannot convert value of type '[Record]' to type 'Record' in coercion

后端 未结 2 665
感动是毒
感动是毒 2021-01-21 09:00

Im re-writing an iOS app I made using C# and Xamarin to Swift for obvious reasons of Xamarin\'s pricing, and low documentation. Following this tutorial for including a UIS

2条回答
  •  没有蜡笔的小新
    2021-01-21 09:38

    Because I think type of your self.records is [Record] and you are trying to casting it as single Record which is not an Array. So it is not possible.

    Try this:

    let array = (self.records as [Record]).filteredArrayUsingPredicate(searchPredicate)
    

    UPDATE:

    Update your updateSearchResultsForSearchController method like this:

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        self.filteredRecords.removeAll(keepCapacity: false)
    
    
        self.filteredRecords = self.records.filter() {
            ($0.album.lowercaseString).containsString(searchController.searchBar.text!.lowercaseString)
        }
        self.tableView.reloadData()
    }
    

    If you want to search for both album and artist then replace this line:

    ($0.album.lowercaseString).containsString(searchController.searchBar.text!.lowercaseString)
    

    with this line:

    ($0.album.lowercaseString).containsString(searchController.searchBar.text!.lowercaseString) || ($0.artist.lowercaseString).containsString(searchController.searchBar.text!.lowercaseString)
    

提交回复
热议问题