osx - How to get the correct callback value for NSUndoManager in Swift? -
osx - How to get the correct callback value for NSUndoManager in Swift? -
i have nsdocument
subclass, hooked nsarraycontroller
. reference, i'm trying translate illustration chapter 9 of cocoa programming mac os x 4th edition.
it seems this question asked before, need utilize object-based undo nsundomanager
. in order pass 2 values method beingness invoked, i'm packaging them nsobject
subclass 2 instance variables.
when kvo methods inserting , deleting employees
array called clicking on buttons in application, work expected.
however, when removeobjectfromemployeesatindex
called during undo operation, index
passed in wildly out of bounds (for first row, seems 55
, after index increases thousands next few rows).
how can right index perform undo action?
class document: nsdocument { var employee_list: array<person> = [] var employees: array<person> { { homecoming self.employee_list } set { if newvalue == self.employee_list { homecoming } self.employee_list = newvalue } } func insertobject(person: person, inemployeesatindex index: int) { self.undomanager.registerundowithtarget(self, selector: selector("removeobjectfromemployeesatindex:"), object: index) if (!self.undomanager.undoing) { self.undomanager.setactionname("add person") } employees.insert(person, atindex: index) } func removeobjectfromemployeesatindex(index: int) { allow person = self.employees[index] allow pair = personindexpair(person: person, index: index) self.undomanager.registerundowithtarget(self, selector: selector("insertpersonindexpair:"), object: pair) if (!self.undomanager.undoing) { self.undomanager.setactionname("remove person") } employees.removeatindex(index) } func insertpersonindexpair(pair: personindexpair) { insertobject(pair.person, inemployeesatindex: pair.index) } }
edit: i've worked around issue passing string, seems pretty obtuse:
self.undomanager.registerundowithtarget(self, selector: selector("removeobjectfromemployeesatstringindex:"), object: string(index)) //... func removeobjectfromemployeesatstringindex(index: string) { if allow = index.toint() { removeobjectfromemployeesatindex(i) } }
use nsnumber instead of int.
class="lang-swift prettyprint-override">self.undomanager.registerundowithtarget(self, selector:selector("removeobjectfromemployeesatstringindex:"), object: nsnumber(integer: index)) //... func removeobjectfromemployeesatstringindex(index: nsnumber) { removeobjectfromemployeesatindex(index.integervalue) }
osx cocoa swift
Comments
Post a Comment