0

I am trying to make an app, with a list of movie titles, with a search function, the list of movies is imported into the appellation via a text file, I am having issues using the filter function given in Xcode, I know it's meant to look something along these lines, but what would I put in the area for "AnyObject"

func updateSearchResultsForSearchController(searchController: UISearchController) {

    self.filteredMovies = self.arrayMovies.filter { (<#AnyObject#>) -> Bool in
        return true
    }


    self.resultsController.tableView.reloadData()
}

These are what my variables are

var dictMovies = [String:String]()
var arrayMovies = NSMutableArray()
var searchController : UISearchController!
var resultsController = UITableViewController()
var filteredMovies = [String]()

I hope that's enough information for someone to give out some information. :D

EDIT This is what Im storing in the arrayMovies

let path = NSBundle.mainBundle().pathForResource("movielist2", ofType: "txt")

    let filemgr = NSFileManager.defaultManager()
    if filemgr.fileExistsAtPath(path!){

        do{

            let fullText = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)

            let readings = fullText.componentsSeparatedByString("@") as [String]

            for i in 1..<readings.count {

                let movieData = readings[i].componentsSeparatedByString("\t")

                dictMovies["MovieTitle1"] = "\(movieData[0])"
                dictMovies["MovieYear"] = "\(movieData[1])"


                arrayMovies.addObject(dictMovies)
            }

        } catch let error as NSError{
            print("Error: \(error)")
        }
    }
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
Ibby
  • 1
  • 1
  • 1
    What kind of object are you storing in `arrayMovies`? (Also, why not use Swift data types instead of NS* ones?) – Phillip Mills Sep 29 '16 at 16:59
  • 1
    As always, **use native Swift `Array` rather than unrelated Foundation `NSMutableArray`**. The compiler has no idea – me neither – what the array actually contains. – vadian Sep 29 '16 at 17:00
  • hey sorry Im still very new to coding, this is what Im storing in barry movies at this stage.... – Ibby Sep 29 '16 at 19:07
  • let movieData = readings[i].componentsSeparatedByString("\t") dictMovies["MovieTitle1"] = "\(movieData[0])" dictMovies["MovieYear"] = "\(movieData[1])" arrayMovies.addObject(dictMovies) } – Ibby Sep 29 '16 at 19:07

1 Answers1

0

You can do it with Predicate :

func updateSearchResultsForSearchController(searchController: UISearchController) {
           let resultPredicate = NSPredicate(format: "name contains[c] %@", searchController.searchBar.text)
           self.filteredUserData = self.arrayMovies.filteredArrayUsingPredicate(resultPredicate)
           self.resultsController.tableView.reloadData()
    }

Hope This help.

Ketan P
  • 4,259
  • 3
  • 30
  • 36