Giving up on UnDo

None of the explanations for the error message

Value of type ‘AnyObject’ has no member ‘removeObjectFromEmployeesAtIndex’

seem to give a way to fix:

let undo: NSUndoManager = undoManager!
undo.prepareWithInvocationTarget(self)
.removeObjectFromEmployeesAtIndex(employees.count)

Error says you must implement “removeObjectFromEmployeesAtIndex” method.

Hey rbowlwr,

I had the same problem. Turns out I just kept going with the code and the issue was fixed.

FinnTheHuman helped me realize the issue. I hadn’t yet implemented removeObjectFromEmployeesAtIndex so when I wrote undo.prepareWithInvocationTarget(self).removeObjectFromEmployeesAtIndex(employees.count) it was trying to call removeObjectFromEmployeesAtIndex on self, and that method didn’t exist yet.

In the book, the method is implemented just after this first method. Problem solved! Here’s my full implementation of that section:

        func insertObject(employee: Employee, inEmployeesAtIndex index: Int) {
        print("adding \(employee) to the employees array")

        let undo: NSUndoManager = undoManager!
        undo.prepareWithInvocationTarget(self).removeObjectFromEmployeesAtIndex(employees.count)

        if undo.undoing == false {
            undo.setActionName("Add Person")
        }

        employees.append(employee)
    }

    func removeObjectFromEmployeesAtIndex(index: Int) {
        let employee = employees[index]
        print("removing \(employee) from the employees array")

        let undo: NSUndoManager = undoManager!
        undo.prepareWithInvocationTarget(self).insertObject(employee, inEmployeesAtIndex: index)

        if undo.undoing == false {
            undo.setActionName("Remove Person")
        }

        employees.removeAtIndex(index)
    }

Try copy/pasting that and see what you get.