user4926503
user4926503

Reputation:

how I get the index of an array with a NSDictionary for key in swift?

I have this array:

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"]
]

i need to get the "name" of each item here:

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

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

Upvotes: 0

Views: 942

Answers (1)

milo526
milo526

Reputation: 5083

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

You store a dictionary in a array.

cars[indexPath.row]

Is used to get the dictionary in the array that is corresponding to the row in the UITableView

.valueForKey(“name”)

Is used to get the name out of the dictionary

EDIT:

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))

Is used for testing (Swift 1.2) and is confirmed to work

Upvotes: 1

Related Questions