简体   繁体   中英

error: Value of type '[String]' has no member 'objectAtIndex' while implementing UISearchController

in the below code i implemented a UITablView Controller which is populated by the PFObjects (parse.com) everything is working fine but now i want to add the search function in my uitableview for that i used UISearchController method and its able to Found the search but the problem is that am not able to navigate or open the detail view controller after tapping the search result cell it is giving me a blank detail view controller and i know its because this function :

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController

  if (segue.identifier == "openStory"){

      let indexPath = self.tableView.indexPathForSelectedRow!

      if self.resultSearchController.active
      {

        // how can i implement the same for filteredNotes as below (on else ) ???

        let object: PFObject = self.filteredNotes.objectAtIndex(indexPath.row) as! PFObject


        upcoming.object = object
  }
      else{

          let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject

          upcoming.object = object

          self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
      }}}

when am trying to make this function workable am getting some errors while declaring 'Object' Error:

  Value of type '[String]' has no member 'objectAtIndex'

how can i make it correct please tell me if anybody knows , thanks

full code:

  import UIKit

  class MasterTableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, UISearchResultsUpdating {


  //var searchNotes: PFObject = PFObject()
  var searchNotes = PFObject(className: "myClass")
  var filteredNotes = [String]()

  var resultSearchController = UISearchController()
  var searchActive: Bool = false



  // creating array for holding ojects
  var noteObjects: NSMutableArray! = NSMutableArray()
  var v = 0

  override func viewDidLoad() {
      super.viewDidLoad()

      self.resultSearchController = UISearchController(searchResultsController: nil)
      self.resultSearchController.searchResultsUpdater = self

      self.resultSearchController.dimsBackgroundDuringPresentation = false
      self.resultSearchController.searchBar.sizeToFit()

      self.tableView.tableHeaderView = self.resultSearchController.searchBar

      self.tableView.reloadData()
      self.definesPresentationContext = true   //self takes priority over the searchController presentation
      self.resultSearchController.hidesNavigationBarDuringPresentation = false




  }

  override func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)



      if v == 0 {
          self.fetchAllObjectsFromLocalDataStore()
          //self.fetchAllObjects()

      }
  }


  // fetching data from local datastore and from parse

  func fetchAllObjectsFromLocalDataStore(){

      let query: PFQuery = PFQuery(className: "myClass")
      query.orderByDescending("createdAt")


      query.fromLocalDatastore()

      query.findObjectsInBackgroundWithBlock { ( objects, error) -> Void in

          if (error == nil) {




              let temp: NSArray = objects as NSArray!

              self.noteObjects = temp.mutableCopy() as! NSMutableArray

              self.tableView.reloadData()

          }else {
              print(error!.userInfo)

          }
      }

  }



  func fetchAllObjects(){

      let query: PFQuery = PFQuery(className: "myClass")
      query.orderByDescending("createdAt")


      query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

          if (error == nil) {

              PFObject.pinAllInBackground(objects, block:  nil )


              self.fetchAllObjectsFromLocalDataStore()

              // self.tableView.reloadData()

          } else {
              print(error?.userInfo)

          }
      }
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }



  // MARK: - Table view data source

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      // #warning Incomplete implementation, return the number of sections
      return 1
  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      // #warning Incomplete implementation, return the number of rows
      if self.resultSearchController.active
      {
          return self.filteredNotes.count
      }else{

          return self.noteObjects.count
      }}


  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MasterTableViewCell


      if self.resultSearchController.active
      {
          cell.textLabel?.text = self.filteredNotes[indexPath.row] as? String





          searchNotes  = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
          cell.MasterTitleLabel?.text = searchNotes["Title"] as? String

          cell.MasterTextLabel.text = searchNotes["Fstory"] as? String
          cell.MasterTimeLabel.text = searchNotes["Time"] as? String
          cell.MasterLocationLabel.text =  searchNotes["Location"] as? String


          return cell

      } else {
          let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject



          cell.MasterTitleLabel?.text = object["Title"] as? String
          cell.MasterTextLabel.text = object["Fstory"] as? String
          cell.MasterTimeLabel.text = object["Time"] as? String
          cell.MasterLocationLabel.text = object["Location"] as? String



          return cell
      }}

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      var object :AnyObject?
      if self.resultSearchController.active{
          object = filteredNotes[indexPath.row]
          print(filteredNotes[indexPath.row])
          self.performSegueWithIdentifier("openStory", sender: self)

      } else {

          object = self.noteObjects[indexPath.row]
          print(noteObjects[indexPath.row])

          self.performSegueWithIdentifier("openStory", sender: self)
      }}


  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

      let upcoming: AddNoteTableViewController = segue.destinationViewController as! AddNoteTableViewController

      if (segue.identifier == "openStory"){

          let indexPath = self.tableView.indexPathForSelectedRow!

          if self.resultSearchController.active
          {

            // how can i implement the same for filteredNotes as below (on else ) ???

            let object: PFObject = self.filteredNotes.objectAtIndex(indexPath.row) as! PFObject


            upcoming.object = object





          }
          else{

              let object: PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject

              upcoming.object = object

              self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
          }}}


  @IBAction func btnReload(sender: AnyObject) {

      fetchAllObjects()

  }


  override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
      return true
  }
  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
      if (editingStyle == UITableViewCellEditingStyle.Delete ){

          if self.resultSearchController.active
          {


          }else{



              let object : PFObject = self.noteObjects.objectAtIndex(indexPath.row) as! PFObject
              // the below for deleting the selected cell's object from server's database
              // object.deleteInBackground()

              //the below for deleting the selected cell's object from localstorage
              object.unpinInBackground()


              self.noteObjects.removeObjectAtIndex(indexPath.row)
          }
          tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

      }
  }
  func updateSearchResultsForSearchController(searchController: UISearchController) {

      // self.filteredNotes.removeAll(keepCapacity: false)
      self.filteredNotes.removeAll(keepCapacity: false)

      //        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
      //
      //        let array = (self.noteObjects as NSArray).filteredArrayUsingPredicate(searchPredicate)
      //
      //        self.filteredNotes = array as! [String]

      let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
      var array = (self.noteObjects as! [PFObject]).map { (obj) -> String in
          obj["Title"] as! String



      }
      array = (array as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [String]
      self.filteredNotes = array as! [String]


      self.tableView.reloadData()



  }
  }

To access an element of an array in swift, you should use the following syntax:

array[n]

where array is your array, and n is the index of the element you want to access.

Your error is that the objectAtIndex method doesnt exist in Arrays.

So when you declared your FilteredNotes, you do var filteredNotes = String

objectAtIndex would work if it was a NSArray

Instead, try

let object: PFObject = self.filteredNotes[indexPath.row] as! PFObject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM