Ios 无法用Int类型的索引为dictionary类型的值下标

Ios 无法用Int类型的索引为dictionary类型的值下标,ios,swift,uisearchcontroller,Ios,Swift,Uisearchcontroller,在IOS应用程序(swift 1.2,Xcode 6.3)上工作时,我在ViewController中实现UISearchController,当我尝试为表视图返回单元格时,数据源方法(cellForRowAtIndexPath)中出现错误,因为我不知道如何从字典中获取indexpath.row。错误是无法在[String:AnyObject]类型的值下标Int类型的索引,并且视图控制器的代码是: 导入基础 导入UIKit 类UsersViewController:UIViewControlle

在IOS应用程序(swift 1.2,Xcode 6.3)上工作时,我在ViewController中实现UISearchController,当我尝试为表视图返回单元格时,数据源方法(cellForRowAtIndexPath)中出现错误,因为我不知道如何从字典中获取indexpath.row。错误是
无法在[String:AnyObject]类型的值下标Int类型的索引,并且视图控制器的代码是:

<代码>导入基础 导入UIKit 类UsersViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{ @IBVAR表格视图:UITableView! 设apiClient=apiClient() var用户:[用户]! var searchArray:[AnyObject]=[AnyObject](){ didSet{self.tableview.reloadData()} } var usersSearchController=UISearchController() 重写func viewDidLoad(){ super.viewDidLoad() self.usersSearchController=({ //以下提供了两种设置: //设置一:此设置在当前视图中显示结果。 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 返回控制器 })() } 覆盖功能视图将出现(动画:Bool){ 超级。视图将显示(动画) println(“UsersController视图将出现”) apiClient.usersService.getList(){users,中出现错误 如果用户!=nil{ self.users=用户 self.tableview?.reloadData() }否则{ println(“错误:\(错误)”) } } } 重写函数didReceiveMemoryWarning(){ 超级。我收到了记忆警告() //处置所有可以重新创建的资源。 } func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{ if(self.usersSearchController.active){ 返回self.searchArray.count??0 }否则{ 返回self.users?计数??0 } } func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{ var cell=tableView.dequeueReusableCellWithIdentifier(“userObjectCell”)作为!UserTableViewCell if(self.usersSearchController.active){ cell.userObject=self.searchArray[indexPath.row]as?User//错误如下 返回单元 }否则{ cell.userObject=self.users?[indexPath.row] 返回单元 } } } 扩展用户ViewController:UITableViewDelegate { func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath) { tableView.declerowatinexpath(indexPath,动画:true) } } 扩展用户viewcontroller:uisearchresultsupdated { func updateSearchResultsForSearchController(searchController:UISearchController) { //self.searchArray.removeAll(keepCapacity:false) // //让searchPredicate=NSPredicate(格式:“SELF-CONTAINS[c]@”,searchController.searchBar.text) //让数组=(self.users作为NSArray.filteredArrayUsingPredicate(searchPredicate) //self.searchArray=数组形式![字符串:AnyObject] } }
字典没有索引,它们有键。您可能需要考虑解析字典数据并将其放入有序列表中的方法,如数组,这样您的数据将出现在同一个位置。(字典未订购)

但是users是一个dictionary对象,self.users?[indexath.row]可以工作。users是一个数组。当您声明类似于[String:AnyObject]的内容时,这意味着键是一个字符串,它的值是AnyObject;当您声明[User]时,这意味着它是一个数组,其中包含类UserI的实例。我已经更新了代码,使用AnyObjects数组而不是dictionary,现在错误在同一行,但不同的数组索引超出范围。数组中有什么内容吗?这将是一个完全不同的问题。