2

I going through Cocoa Programming for OS X book by Big Nerd Ranch, and I'm stuck on Chapter 11 -- working with NSUndoManager. As I understand, NSUndoManager is now just UndoManager, but I can't get the rest of the sample code to compile.

The relevant code is:

class Document: NSDocument {

    @objc dynamic var employees: [Employee] = [] 

    func insertObject(employee: Employee, inEmployeesAtIndex index: Int) {
        let undo: UndoManager = undoManager!

        undo.prepare(withInvocationTarget: self).insertObjectFromEmployeesAtIndex(employees.count)

        if !undo.isUndoing {
            undo.setActionName("Add Person")
        }
    }

    func insertObjectFromEmployeesAtIndex(index: Int) {
        let employee: Employee = employees[index]

        let undo: UndoManager = undoManager!

        undo.prepare(withInvocationTarget: self).insertObject(employee: employee, inEmployeesAtIndex: index)

        employees.remove(at: index)
    }
}

The error I get is for lines undo.prepare in both functions. The error is:

Value of type 'Any' has no member 'insertObjectFromEmployeesAtIndex'

and

Cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members

Cœur
  • 37,241
  • 25
  • 195
  • 267
Genu
  • 1,094
  • 1
  • 9
  • 22
  • See https://stackoverflow.com/questions/45789096/invocation-based-undo-manager-in-swift – rmaddy Jan 01 '19 at 18:32
  • Did you try what the error messages suggest? – matt Jan 01 '19 at 18:43
  • See also https://stackoverflow.com/questions/39562636/using-nsundomanager-and-preparewithinvocationtarget-in-swift-3 – matt Jan 01 '19 at 18:47
  • @rmaddy The linked question seem to resolve the compiler issues, but the functionality actually doesn't work. Does NSArrayController suppose to call those methods automatically? (Maybe separate question?) – Genu Jan 01 '19 at 18:59
  • @Genu What you're being shown is a key-value coding method. – matt Jan 01 '19 at 20:26
  • The second function is probably "removeObjectFromEmployees..." – mikeD Jan 02 '19 at 04:11
  • check here https://forums.bignerdranch.com/t/nsundomanager-document-swift-and-employee-swift-for-swift-4-2-xcode-10-1/15416 – XueYu Mar 08 '19 at 06:13

0 Answers0