2

I want to search the results using UISearchBar for JSON array value.

I am using following code:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = data.filter ({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.customerareatableview.reloadData()
}

Error will be displayed in NSSArray does not have named filter. how can i fixed it

jjj
  • 1,136
  • 3
  • 18
  • 28
  • check this out ..https://github.com/codepath/ios_guides/wiki/Search-Bar-Guide. – vaibhav Jan 28 '17 at 08:05
  • Possible duplicate of [IOS Swift Searching table from an Array](http://stackoverflow.com/questions/40547613/ios-swift-searching-table-from-an-array) – Rob Jan 28 '17 at 08:40

1 Answers1

0

NSSArray does not have named filter that means you data is of type NSArray

you should better format your dada as Array and not as NSArray

(how to filter an NSArray -> How to filter NSArray in Swift? )

you should better use in your code the new Array, String, .... and not the NSobjects

var data: [String] = ["a","b"]

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = data.filter ({ (text) -> Bool in
        return searchText.range(of: string, options: .caseInsensitive) != nil
    })

[...]

this is a little bit wrong. when you don't get any results that means not that you are not searching. also with a typo you can get an empty result. better is:

if searchText.isEmpty {
    searchActive = false;
} else {
    searchActive = true;
}
Community
  • 1
  • 1
muescha
  • 1,544
  • 2
  • 12
  • 22