我如何在swift中获取带有NSDictionary的数组的索引?

时间:2015-05-26 19:31:06

标签: swift ios8

我有这个数组:

var cars : NSArray = [
    ["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"Viper", "marca":"Dodge","ano":"1969"],["name":"F344", "marca":"Dodge","ano":"1969"],["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"Cobra", "marca":"Dodge","ano":"1969"],["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"GT500", "marca":"Dodge","ano":"1969"]
]

我需要在这里获得每个项目的“名称”:

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath){

            cell.textLabel?.text = cars["name"]
    }

1 个答案:

答案 0 :(得分:1)

cars[indexPath.row].valueForKey(“name”)

您将字典存储在数组中。

cars[indexPath.row]

用于获取数组中与UITableView

中的行对应的字典
.valueForKey(“name”)

用于从字典中获取name

编辑:

var cars : NSArray = [
  ["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"Viper", "marca":"Dodge","ano":"1969"],["name":"F344", "marca":"Dodge","ano":"1969"],["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"Cobra", "marca":"Dodge","ano":"1969"],["name":"Dayatona", "marca":"Dodge","ano":"1969"],["name":"GT500", "marca":"Dodge","ano":"1969"]
]

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath){

  println(cars[indexPath.row].valueForKey("name"))
}


configureCell(UITableViewCell(), atIndexPath: NSIndexPath(forRow: 1, inSection: 1))

用于测试(Swift 1.2)并确认可以正常工作