Rocky1289
Rocky1289

Reputation: 15

What is the reason for this error: "NSIndexPath' is not implicitly convertible to 'IndexPath'; did you mean to use 'as' to explicitly convert?"

In the process of learning swift programming and have run into a snag. This is for a to-do list app. Am trying to understand why I am getting this error:

NSIndexPath' is not implicitly convertible to 'IndexPath'; did you mean to use 'as' to explicitly convert?

for the following block of code:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    var toDoItems = [ToDoItem]()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if toDoItems.count > 0 {
            return
        }
        toDoItems.append(ToDoItem(text: "feed the cat"))
        toDoItems.append(ToDoItem(text: "buy eggs"))
        toDoItems.append(ToDoItem(text: "watch WWDC videos"))
        toDoItems.append(ToDoItem(text: "rule the Web"))
        toDoItems.append(ToDoItem(text: "buy a new iPhone"))
        toDoItems.append(ToDoItem(text: "darn holes in socks"))
        toDoItems.append(ToDoItem(text: "write this tutorial"))
        toDoItems.append(ToDoItem(text: "master Swift"))
        toDoItems.append(ToDoItem(text: "learn to draw"))
        toDoItems.append(ToDoItem(text: "get more exercise"))
        toDoItems.append(ToDoItem(text: "catch up with Mom"))
        toDoItems.append(ToDoItem(text: "get a hair cut"))
    }
    
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return toDoItems.count
    }

    func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell",
                forIndexPath: indexPath) as! UITableViewCell
            let item = toDoItems[indexPath.row]
            cell.textLabel?.text = item.text
            return cell
    }
    
}

Have not been able to find this error message posted anywhere when I search it on google. Scratching my head for answers as to what might be causing such an error.

The code is from a tutorial from this page: https://www.raywenderlich.com/2153-how-to-make-a-gesture-driven-to-do-list-app-like-clear-in-swift-part-1-2

Could it be that something is outdated?

Please help. Thank you

Upvotes: 0

Views: 174

Answers (1)

aheze
aheze

Reputation: 30318

Note that the article is from 2014.

Article published date is Nov 19 2014

You are using a much older version of:

Update your code to this instead:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return toDoItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let item = toDoItems[indexPath.row]
    cell.textLabel?.text = item.text
    return cell
}

Upvotes: 1

Related Questions