It is impossible to make the function of undoing recent actions in an application on OS X. In Objective-C, this is implemented as:

// Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ добавлСния Π½ΠΎΠ²ΠΎΠΉ записи - (void)insertObject:(Person *)p inEmployeesAtIndex:(NSInteger)index { NSUndoManager *undo = [self undoManager]; [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index]; [employees insertObject:p atIndex:index]; } 

I have a class Person in which two variables name and raise declared. There is also an employees array with which the Array Controller is associated, through which I add and delete new records. Accordingly, to cancel the deletion of an entry, use the following method:

 // Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ удалСния записи - (void)removeObjectFromEmployeesAtIndex:(NSInteger)index { NSUndoManager *undo = [self undoManager]; Person *p = [employees objectAtIndex:index]; [[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index]; [employees removeObjectAtIndex:index]; } 

Everything works well here. I tried to transfer this implementation to Swift and my Undo and Redo are not active in the edit menu. Here is how I implemented it:

 //Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ добавлСния Π½ΠΎΠ²ΠΎΠΉ записи func insertObject(person: Person, inEmployeesAtIndex index: NSInteger) { let undo = NSUndoManager() undo.prepareWithInvocationTarget(self).removeObjectFromEmployeesAtIndex(index) employees.insertObject(person, atIndex: index) } 

And the method to cancel deletion:

 // Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ удалСния записи func removeObjectFromEmployeesAtIndex(index: NSInteger) { let undo = NSUndoManager() let person = employees.objectAtIndex(index) as! Person undo.prepareWithInvocationTarget(self).insertObject(person, inEmployeesAtIndex: index) employees.removeObjectAtIndex(index) } 

What am I doing wrong?

    1 answer 1

    Found a solution to your question

     //Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ добавлСния Π½ΠΎΠ²ΠΎΠΉ записи func insertObject(person: Person, inEmployeesAtIndex index: Int) { // Add the inverse of this operation to the undo stack let undo: NSUndoManager = self.undoManager! undo.prepareWithInvocationTarget(self).removeObjectFromEmployeesAtIndex(index) if !undo.undoing { undo.setActionName("Add person") } employees.insertObject(person, atIndex: index) } // Ѐункция ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ удалСния записи func removeObjectFromEmployeesAtIndex(index: Int) { let person = employees[index] as! Person // Add the inverse of this operation to the undo stack let undo: NSUndoManager = self.undoManager! undo.prepareWithInvocationTarget(self).insertObject(person, inEmployeesAtIndex: index) if !undo.undoing { undo.setActionName("Remove Person") } // Remove the Employee from the array employees.removeObjectAtIndex(index) }