accessing core data to return first word in swift

Viewed 29

I need help deleting a row in core data

outline:

i have a table view which gets populated from a form which asks a user to input details about a friend.

The user needs an option to delete a friend from the list. when a user clicks on a row from the table view i need to access the string to get the ID and then query the core data for that ID and then delete it.

I am stuck on accessing the data the user clicks on and getting the ID

this is what a friend in the table view looks like

12345 James Bond 19 Male Sydney ICT

where 12345 is a unique student number

this is my function to remove a friend from core data

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        friends = appDelegate.getFriendInfo()
        
        
        self.tableView.rowHeight = 33.0
        
    }
    
}

I am having difficulties getting the ID within the row and then deleting that row from core data

1 Answers

Retrieve the id from the array using the given indexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let id = friends[indexPath.row].studentID
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    appDelegate.removeRecord(id: id)
}
Related