2016-02-17 82 views
-1

hello我是Swift中的新手,并试图在表视图上实现搜索栏。所以我在Youtube上跟随了一个教程并正确实施了教程。但是我的数据有一些问题。正如在教程中的数据是NSArray和我从网络服务到来和在NSDictionary在Swift中过滤字典

所以我的数据保存在NSDictionary中,如果我要访问的变量我做这个

cell.countryNameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String 

还好在教程他做了这样的事情

var filterTableData = [String]() 
    var tableData = ["one","two","three"] 


func updateSearchResultsForSearchController(searchController: UISearchController) { 
     filterTableData.removeAll(keepCapacity: false) 

     let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!) 
     let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredict) 
     filterTableData = array as! [String] 
     self.tableView.reloadData() 
    } 

,现在我想知道怎样才能实现这个,因为我有存储在NSdictionary和可变数据字典

+1

当您从Web服务获取字典时,将其加载到结构数组中。字典无序并且不适用于桌面视图 – Paulw11

+1

将您的字典解析为自定义对象。不要使用字典,使用对象。 – Sulthan

+0

@ Paulw11你的无序意味着什么?意味着数据不会弹出排序 – hellosheikh

回答

1

可以将字典的键存储到数组对象 并在函数中使用它。

var filterTableData = [String]() 
var tableData = Array(dict.keys)