Montana bucks Montana bucks - 4 years ago 81
Swift Question

How to filter an array which has different elements?

I have a TableView, from which I am getting the selected

tableViewCell's
values in an array but it's not the desired array:


[<NSIndexPath: 0xc000000000200016\> {length = 2, path = 0 - 1}: "test1234",
<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}: "Judson"]



How can I filter it to get
["test1234","Judson"]
?

This is the code I am using:

var selectedTextLabels = [NSIndexPath: String]()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let cell = tableView.cellForRowAtIndexPath(indexPath) as! UsersTableViewCell
if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
cell.accessoryType = UITableViewCellAccessoryType.None;
selectedTextLabels[indexPath] = nil
} else {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
if let text = cell.collegeNameLbl?.text {
selectedTextLabels[indexPath] = text
}
}
}
print("\(selectedTextLabels) outside")
}

Answer Source

Since var selectedTextLabels = [NSIndexPath: String]() is a dictionary and you are trying to retrieve the String part of it, namely the values you can get them via:

let values = Array(selectedTextLabels.values)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download