I have a function where I am incrementing the value of the label as below
@IBAction func buttonClicked(_ sender: UIButton) {
let indexPath = indexPath(sender: sender, tableView: profileTableView)
array[Int((indexPath.row))].likes = String(Int(array[Int((indexPath.row))].likes)!+1)
self.userProfileTableView.reloadData()
}
In the above function after the reloadData value of the label gets incremented to the next number, But when I make it a generic function in an extension as below the value of the label does not increment
func buttonClicked(data:String,tableView:UITableView) {
let data = String(Int(data)!+1)
print("Incremented data", data)
tableView.reloadData()
}
I am calling the function as following but the label does not get updated with the incremented value . It retains the old value.
buttonClicked(data: feeds[Int((indexPath.row))].likes, tableView: userProfileTableView)
Any help will be appreciated . Thank you.
you should change your code to this.
let numLikes = Int(feeds[Int((indexPath.row))].likes)!
feeds[Int((indexPath.row)).likes = String(numLikes)
Because String
is a value types.
When you do this.
buttonClicked(data: feeds[Int((indexPath.row))].likes, tableView: userProfileTableView)
func buttonClicked(data:String,tableView:UITableView) {
let data = String(Int(data)!+1)
print("Incremented data", data)
tableView.reloadData()
}
1) New value variables data
in func buttonClicked
will be created and has value = feeds[Int((indexPath.row))].likes
2) when you change data
, it just change data
but not feeds[Int((indexPath.row))].likes
Look at Value and Reference Type for more info.