Sunday, May 17, 2026
HomeiOS Developmentios - Utilizing swipe to delete operate in swift on dictionary array

ios – Utilizing swipe to delete operate in swift on dictionary array

[ad_1]

I’m making an attempt to make use of the swipe to delete operate in Swift for my dictionary array and I carry on getting the error “Can not convert worth of sort ‘Int’ to anticipated argument sort ‘Dictionary<String, String>.Index.” I’m not certain learn how to change the info sort and learn how to repair this.

That is my code:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return time period.depend
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let phrases = Array(time period.keys)
        let that means = Array(time period.values)
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.textual content = phrases[indexPath.row]
        cell.detailTextLabel?.textual content = that means[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
       if editingStyle == .delete {
          time period.take away(at: indexPath.row)
          tableView.deleteRows(at: [indexPath], with: .fade)
       }
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Do any extra setup after loading the view.
        tblTerms.delegate = self
        tblTerms.dataSource = self
    }
    // Retailers
    @IBOutlet weak var txtTerm: UITextField!
    @IBOutlet weak var txtMeaning: UITextField!
    @IBOutlet weak var tblTerms: UITableView!
    @IBOutlet weak var lblCount: UILabel!
    @IBOutlet weak var lblMessage: UILabel!
    
    var time period: [String: String] = [:]
    
    // Actions
    @IBAction func btnAdd(_ sender: Any) {
        if txtTerm.textual content != "" && txtMeaning.textual content != "" {
        time period[String(txtTerm.text!)] = txtMeaning.textual content
        lblCount.textual content = String(time period.depend)
        tblTerms.reloadData()
        txtTerm.textual content = ""
        txtMeaning.textual content = ""
    } else {
        lblMessage.textual content = "No time period or that means entered"
    }
}
}

Cheers

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments