无法使用 Int 类型的索引下标字典类型的值

Cannot subscript a value of type dictionary with an index of type Int

在 IOS 应用程序(swift 1.2,Xcode 6.3)上工作,我在 ViewController 中实现 UISearchController,但出现错误在数据源方法 (cellForRowAtIndexPath) 中,当我尝试 return 一个 table 视图的单元格时,因为我不知道从字典中获取 indexpath.row。错误是Cannot subscript a value of type [String : AnyObject] with an index of type Int,视图控制器的代码是:

import Foundation
import UIKit

class UsersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

  @IBOutlet var tableview:UITableView!

  let apiClient = ApiClient()

  var users: [User]!

  var searchArray:[AnyObject] = [AnyObject](){
    didSet  {self.tableview.reloadData()}
  }

  var usersSearchController = UISearchController()

  override func viewDidLoad() {
    super.viewDidLoad()

    self.usersSearchController = ({
      // Two setups provided below:

      // Setup One: This setup present the results in the current view.
      let controller = UISearchController(searchResultsController: nil)
      controller.searchResultsUpdater = self
      controller.hidesNavigationBarDuringPresentation = false
      controller.dimsBackgroundDuringPresentation = false
      controller.searchBar.searchBarStyle = .Minimal
      controller.searchBar.sizeToFit()
      self.tableview.tableHeaderView = controller.searchBar

      return controller
    })()
  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    println("UsersController viewWillAppear")

    apiClient.usersService.getList() { users, error in
      if users != nil {
        self.users = users
        self.tableview?.reloadData()
      } else {
        println("error: \(error)")
      }
    }
  }

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

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if ( self.usersSearchController.active){

      return self.searchArray.count ?? 0

    } else {

      return self.users?.count ?? 0

    }
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("userObjectCell") as! UserTableViewCell

    if (self.usersSearchController.active){

      cell.userObject = self.searchArray[indexPath.row] as? User//HERE IS THE ERROR

      return cell

    } else {

      cell.userObject = self.users?[indexPath.row]

      return cell
    }
  }

}

extension UsersViewController: UITableViewDelegate
{
  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
  }
}

extension UsersViewController: UISearchResultsUpdating
{
  func updateSearchResultsForSearchController(searchController: UISearchController)
  {
//    self.searchArray.removeAll(keepCapacity: false)
//    
//    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
//    let array = (self.users as NSArray).filteredArrayUsingPredicate(searchPredicate)
//    self.searchArray = array as! [String: AnyObject]
  }
}

字典没有索引,它们有键。您可能需要考虑一种解析字典数据并将其放入有序列表(如数组)的方法,这样您的数据就会出现在同一个地方。 (字典没有排序)。