user115076
user115076

Reputation: 13

How can I access an item from an array dynamically with Swift

I've got an Array that I'm using to populating the rows of a UITableView.

After a row is selected I need to retrieve information from the Array based on the row selected to populate some outlets (labels, textfields, etc.)

For example:

I create an itemSelected variable in the didSelectRowAtIndexPath in my ViewController for the TableView which I set to indexPath.row

itemSelected = indexPath.row

Then in my viewDidLoad for my otherViewController I need to retrieve the info by

array[itemSelected]

But, I get a compiler error that says: "Expression resolves to unused i-value"

Upvotes: 1

Views: 145

Answers (1)

Bishan
Bishan

Reputation: 401

In here you simply accessing the array but not calling any value. As a example if you have a key call "Name" in your array and you want to set it to a UILabel just do it as this.

self.Name.text = array[itemSelected].valueForKey("Name") as! String

if not just do something with it.

self.Name.text = array[itemSelected] as! String

OR

print(array[itemSelected])

Upvotes: 3

Related Questions