XCode error "Cannot convert value of type 'NSIndexPath' to expected element type 'Array<IndexPath>.ArrayLiteralElement' (aka 'IndexPath')"

Hi everyone

I am new to XCode and I am following an online tutorial for a shopping list program. I have this error that appears twice, "Cannot convert value of type 'NSIndexPath' to expected element type 'Array.ArrayLiteralElement' (aka 'IndexPath')".

The tutorial I'm following is here if you need it: https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-building-a-shopping-list-application-2--cms-25516

If anyone is able to help me out or give me some guidance that would be greatly appreciated, thank you!

// MARK: -
  // MARK: Add Item View Controller Delegate Methods
  func controller(controller: AddItemViewController, didSaveItemWithName name: String, andPrice price: Float) {
    // Create Item
    let item = Item(name: name, price: price)
     
    // Add Item to Items
    items.append(item)
     
    // Add Row to Table View
    tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: (items.count - 1), inSection: 0)], withRowAnimation: .None)
    // The line above is where the error occurs
    // Save Items
    saveItems()
  }
   
  // MARK: -
  // MARK: Edit Item View Controller Delegate Methods
  func controller(controller: EditItemViewController, didUpdateItem item: Item) {
    // Fetch Index for Item
    if let index = items.firstIndex(of:item) {
      // Update Table View
      tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .Fade)
     // The line above is where the error occurs
    }
     
    // Save Items
    saveItems()
  }
Accepted Answer

You are looking at an old tutorial. So some API have changed.

Use IndexPath instead of NSIndexPath, and reloadRows instead of reloadRowsAtIndexPaths

Working code is:

    tableView.insertRows([IndexPath(row: (items.count - 1), section: 0)], with: .none)


    tableView.reloadRows([IndexPath(row: index, section: 0)], with: .fade)

Thank you so much!! that fixed those errors. I am now getting a caution that says: "archiveRootObject(_:toFile:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: and -writeToURL:options:error: instead"

Are you able to help with this?

            // Write to File
            if NSKeyedArchiver.archiveRootObject(items, toFile: itemsPath) {
              ud.set(true, forKey: "UserDefaultsSeedItems")
            }
          }
XCode error "Cannot convert value of type 'NSIndexPath' to expected element type 'Array&lt;IndexPath&gt;.ArrayLiteralElement' (aka 'IndexPath')"
 
 
Q