1
votes

I am getting this error:

Cannot convert value of type 'Int' to expected argument type 'String.Index'

on line Goals[0].remove(at: indexPath.row). How do I solve this?

Here is my code:

import UIKit

class GoalsViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var Goals: [String] = ["goal 1", "goal 2", "goal 3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension GoalsViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         if indexPath.section == 0 {
             Goals[0].remove(at: indexPath.row)
             tableView.reloadData()
         }
     }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Goals.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GoalCell_1", for: indexPath)
        cell.textLabel?.text = Goals[indexPath.row]
        return cell
    }

}
2
What exactly are you trying to achieve here? - Frankenstein
I am trying to delete the cell once it is clicked on and then eventually move it to a new table view - user13470903

2 Answers

-1
votes

You need to remove array element and not the SubString in String, replace your line showing error to this:

Goals.remove(at: indexPath.row)
0
votes

Goals is an array.

var Goals: [String] = ["goal 1", "goal 2", "goal 3"]

And here you choose first element of array '[0]' and try to '.remove' from it, but it's a string

Goals[0].remove(at: indexPath.row)

If you want to remove string from Goals array:

Goals.remove(at: indexPath.row)