Re: NSUndoManager.prepareWithInvocationTarget:
Re: NSUndoManager.prepareWithInvocationTarget:
- Subject: Re: NSUndoManager.prepareWithInvocationTarget:
- From: Charles Jenkins <email@hidden>
- Date: Sun, 10 May 2015 21:05:36 -0400
Solved, thanks to Charles Srstka for the example!!!
I wondered why the invocation would work for him but not me. In the example, Charles marks the method to be invoked “dynamic.” That was the difference. Like most examples in the docs, NSUndoManager’s invocation example is shown in obj-c, where this isn’t necessary, and I don’t think the current BNR mentions it. But the invoked method must be available through dynamic dispatch. This is probably stated somewhere in the docs, but I just missed it. EDIT: Actually the fact is obvious when you think about it, but because I’m working with a subclass of NSObject/NSDocument, it never would’ve occurred to me I’d have to do something special to achieve it.
Here’s my registerUndo function that now works great:
// Document_Undo.swift
import Cocoa
extension Document {
private func objectsNecessaryForUndo()
-> ( undoManager:NSUndoManager, textView:NSTextView )
{
// We need to crash if these objects can't be retrieved
return ( self.undoManager!, self.textView! )
}
// Register undo for any method, with captured parameters.
// IMPORTANT: Dynamic dispatch is required, so if the code you add in
// the invocationClosure results in the error "AnyObject
// does not have a member named...", just mark the invoked method
// with the "dynamic" keyword.
func registerUndo( actionName:String, invocationClosure:( AnyObject ) -> () )
{
let ( undoManager, textView ) = objectsNecessaryForUndo()
if textView.allowsUndo {
println( "Preparing UNDO for \(actionName)" )
invocationClosure( undoManager.prepareWithInvocationTarget( self ) )
undoManager.setActionName( actionName )
} else {
println( "Skipping UNDO for \(actionName) because textView.allowsUndo is false" )
}
}
}
I call it like this:
registerUndo( “Action Name” ) {
$0.dynamicMethodName( neededParameter )
}
—
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden