ios - Q:Value of type '[String]' has no member 'filtered' -
in code when implementation
func
:filtercontentforsearchtext(searchtext:string, scope:string)
error comes:
let resultpredicate:nspredicate = nspredicate.init(format: "name contains[c] \(searchtext)", searchtext) resultdatasource = datasource?.filtered(using: resultpredicate) nsarray? // red error:value of type '[string]' has no member 'filtered'
i post code below:
import uikit class storelistviewcontroller: uiviewcontroller, uisearchbardelegate,uisearchdisplaydelegate,uitableviewdelegate,uitableviewdatasource { var datasource:[string]? = nil var resultdatasource:nsarray? = nil override func viewdidload() { super.viewdidload() initdata() initui() } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepare(for segue: uistoryboardsegue, sender: any?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ // mark: - init func initdata(){ self.searchdisplaycontroller?.searchresultsdelegate = self self.searchdisplaycontroller?.searchresultsdatasource = self self.searchdisplaycontroller?.delegate = self datasource = ["中青年年是当年是你的","中青年年是当年是你的","中青年年是当年是你的","中青年年是当年是你的","中青年年是当年是你的","中青年年是当年是你的" ] } func initui() -> void { } // mark: - tableview delegate func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { if tableview == self.searchdisplaycontroller?.searchresultstableview { return (datasource?.count)! }else { return (resultdatasource?.count)! } } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { return 50 } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell_id = "storelisttabcell" var cell:storelisttabcell = tableview.dequeuereusablecell(withidentifier: cell_id) as! storelisttabcell // 配置cell cell.title_label.text = datasource?[indexpath.row] return cell } func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { tableview.deselectrow(at: indexpath, animated: true) } // mark: - filter delegate func filtercontentforsearchtext(searchtext:string, scope:string) -> void { // nspredicate *resultpredicate = [nspredicate predicatewithformat:@"name contains[c] %@", searchtext]; // filteredist = [oldlist filteredarrayusingpredicate:resultpredicate]; let resultpredicate:nspredicate = nspredicate.init(format: "name contains[c] \(searchtext)", searchtext) resultdatasource = datasource?.filtered(using: resultpredicate) nsarray? // there error!!! } func searchdisplaycontroller(_ controller: uisearchdisplaycontroller, shouldreloadtableforsearch searchstring: string?) -> bool { //let scope_str = searchdisplaycontroller?.searchbar.scopebuttontitles.objectatindex(self.searchdisplaycontroller.searchbar.selectedscopebuttonindex) //[self filtercontentforsearchtext:searchstring scope:[[self.searchdisplaycontroller.searchbar scopebuttontitles] objectatindex:[self.searchdisplaycontroller.searchbar selectedscopebuttonindex]]]; var scopetitles:array = (searchdisplaycontroller?.searchbar.scopebuttontitles)! let selected_index = searchdisplaycontroller?.searchbar.selectedscopebuttonindex let scope_str = scopetitles[selected_index!] filtercontentforsearchtext(searchtext: searchstring!, scope:scope_str) return true } }
and in storyboard :
because swift array not support nsarray's filterred predicate, have use array.filter{ $0.contains(searchtext) }
(recommended) or array.filter{resultpredicate.evaluate(with:$0)}
(to use nspredicate - totally not cool swift) instead of 1 using
full:
array.filter { (item) -> bool in resultpredicate.evaluate(with: item) }
Comments
Post a Comment