我有一个tableview
,我在其中从命名为PlaylistArray
的数组中加载所有详细信息。我有一个搜索页面,用户可以在其中搜索playlistArray
类型的[[String: Any]]
。我不知道如何将用户在PlayListArray
中给出的搜索文本过滤为filteredArray
。我已附上PlaylistArray
答复供您参考。
这是我之前尝试过滤PlaylistArray
的代码。
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (constantsList.playTypeArray.lectureArraymp3 as NSArray).filtered(using: searchPredicate)
// filtered_Array.add(array)
print(array)
我需要搜索用户提供的搜索文本,我们必须将搜索文本与
title
的playlistArray
标记进行比较。当两者相同时,我需要向过滤后的数组显示特定数组索引的全部详细信息。PlaylistArray:
[["title": Kaṭha-Bhāṣyam-41 Mantra 1-2-20, "image_url": https://www.imaginetventures.name/swamijione/wp-content/uploads/2019/01/gitanew.png, "mp3": https://www.imaginetventures.name/swamiji/wp-content/uploads/2018/12/01-PURU],["title": Kaṭha-Bhāṣyam-41 Mantra 1-2-20, "image_url": https://www.imaginetventures.name/swamijione/wp-content/uploads/2019/01/gitanew.png, "mp3": https://www.imaginetventures.name/swamiji/wp-content/uploads/2018/12/01-PURU],["title": Kaṭha-Bhāṣyam-41 Mantra 1-2-20, "image_url": https://www.imaginetventures.name/swamijione/wp-content/uploads/2019/01/gitanew.png, "mp3": https://www.imaginetventures.name/swamiji/wp-content/uploads/2018/12/01-PURU]]
最佳答案
这是使用filter
API快速解决方案。在此处了解更多信息:https://developer.apple.com/documentation/swift/sequence/3018365-filter
let array = [["title": "Kaṭha-Bhāṣyam-41 Mantra 1-2-20", "image_url": "https://www.imaginetventures.name/swamijione/wp-content/uploads/2019/01/gitanew.png"],
["title": "Kaṭa-Bhāṣyam-41 Mantra 1-2-20", "image_url": "https://www.imaginetventures.name/swamijione/wp-content/uploads/2019/01/gitanew.png"]]
guard let searchText = searchController.searchBar.text else {return} //Or use if-let
let filteredArray = array.filter { (object) -> Bool in
guard let title = object["title"] as? String else {return false}
return title?.contains(searchText) ?? false
}