0

I am trying to work with NSUndoManager's prepareWithInvocationTarget. I would like to have something like

[[self.undoManager prepareWithInvocationTarget:self] doSomethingWithObject:[self.aMutableArray objectAtIndex:0]]

where the argument of doSomethingWithObject is not evaluated until the undo method is called. In other words I don't want the argument to be the current first element of aMutableArray, but the first element of aMutableArray at the time of undo.

Is there a particular part of NSInvocation or NSMethodSignature that I should look at?

Joseph Johnston
  • 533
  • 1
  • 3
  • 14

1 Answers1

0

As in any method call, it just a question of what happens where. If you don't want [self.aMutableArray objectAtIndex:0] evaluated now, don't include it in the invocation expression! Instead, make the invocation expression a call to a method where that method will call [self.aMutableArray objectAtIndex:0]:

[[self.undoManager prepareWithInvocationTarget:self] 
    doSomethingWithObjectAtIndex:0];

...where doSomethingWithObjectAtIndex: is where [self.aMutableArray objectAtIndex:0] is called.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes, I see. I just thought there might be a way to do it without creating an extra method. Thank you. – Joseph Johnston Apr 26 '15 at 17:58
  • Adding methods in order to adapt your code to the demands of the undo manager is perfectly standard. And for the very reason you have encountered - there is a difference between what you want to do now and what you want to do in the future when/if the user says "undo". – matt Apr 26 '15 at 20:32