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?