Re: NSUndoManager.prepareWithInvocationTarget:
Quite right. I also tend to use the obj-c style to describe method signatures because the punctuation is cleaner. Looking at my original message, only the word AnyObject gives a clue I’m using Swift. I’ll try to do better. — Charles On May 10, 2015 at 9:47:41 PM, Graham Cox (graham@bigpond.com) wrote: On 11 May 2015, at 11:05 am, Charles Jenkins cejw...@gmail.com wrote: Here’s my registerUndo function that now works great: // Document_Undo.swift It would have been useful to have mentioned that you’re using Swift, saves us Obj-C guys giving you useless advice. You’re still in the minority you know… —Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
On 11 May 2015, at 11:05 am, Charles Jenkins cejw...@gmail.com wrote: Here’s my registerUndo function that now works great: // Document_Undo.swift It would have been useful to have mentioned that you’re using Swift, saves us Obj-C guys giving you useless advice. You’re still in the minority you know… —Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
On 11 May 2015, at 11:55 am, Charles Jenkins cejw...@gmail.com wrote: only the word AnyObject gives a clue I’m using Swift Which isn’t really a clue for people (like myself) who haven’t looked at Swift in any detail and wouldn’t recognise it as “Swiftian”. It could have been an obscure compiler warning relating to Obj-C, especially as new ones seem to be added every XCode version. Anyway, good to know some of the Obj-C stuff we all know and love (NSUndoManager - hah!) works just as well in Swift. —Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
On 10 May 2015, at 11:07 pm, Charles Jenkins cejw...@gmail.com wrote: Am I correct to believe there is NO way to use NSUndoManager.prepareWithInvocationTarget: if your undo method requires a parameter? No, that’s not correct. This approach allows you to pass any method signature whatsoever, because the return type of -prepareWithInvocationTarget: is an id, and the object it represents accepts any method signature. It’s much MORE flexible than -registerUndoWithTarget:selector:object, which allows you to pass exactly one parameter, which must be an object (or no parameters and nil). What NSUndoManager does internally when you use prepareWithInvocationTarget is to wrap whatever you pass with an NSInvocation - so understanding that class is useful to also understand NSUndoManager. I can use any method I want which has no parameter, but any method with parameters gets me the error “AnyObject does not have member named…” I’m not sure exactly why that’s happening - maybe post your code that’s causing this and the problem might be clear. But it may be because using -prepareWithInvocationTarget as if it were a property is incorrect - it’s not a property so it shouldn’t be treated as one (I realise that the way properties are implemented is supposed to be exactly equivalent to a method call, but there are cases where they are semantically different and this is likely one of them. -prepareWithInvocationTarget: is kind of tricksy. For clarity, you should probably only write code that uses the dot syntax strictly for properties, and uses classic bracket syntax for other methods. The intention of your code will be clearer, and you will likely avoid these kinds of issues). —Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
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 (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
NSUndoManager.prepareWithInvocationTarget:
Am I correct to believe there is NO way to use NSUndoManager.prepareWithInvocationTarget: if your undo method requires a parameter? I can use any method I want which has no parameter, but any method with parameters gets me the error “AnyObject does not have member named…” I thought about making a ClosureWrapper object which could have a parameterless method to call any method on a captured object, but since the invocation holds a weak reference, I think any instance of ClosureWrapper would be destroyed immediately after registration, so I didn’t bother trying. Right now I’m sticking with registerUndoWithTarget:selector:object:, but I’d like to know if there is a good way to use prepareWithInvocationTarget: instead. — Charles ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
My understanding is you call [[NSUndoManger prepareInvokationWithTarget:aTarget] doFoo:foo withBar:bar]; That is, after having registered your target with the preparation message you can send the returned proxy all the messages your object can handle. What I'm not sure is how memory management is done on the target and its calls. ___m i c h a e l s t a r k e geschäftsführer HicknHack Software GmbH www.hicknhack-software.com ___k o n t a k t +49 (170) 36 86 1 36 cont...@hicknhack.com ___H i c k n H a c k S o f t w a r e G m b H geschäftsführer - maik lathan | andreas reischuck | michael starke bayreuther straße 32 01187 dresden amtsgericht dresden HRB 30351 sitz - dresden On 10.05.2015, at 15:07, Charles Jenkins cejw...@gmail.com wrote: Am I correct to believe there is NO way to use NSUndoManager.prepareWithInvocationTarget: if your undo method requires a parameter? I can use any method I want which has no parameter, but any method with parameters gets me the error “AnyObject does not have member named…” I thought about making a ClosureWrapper object which could have a parameterless method to call any method on a captured object, but since the invocation holds a weak reference, I think any instance of ClosureWrapper would be destroyed immediately after registration, so I didn’t bother trying. Right now I’m sticking with registerUndoWithTarget:selector:object:, but I’d like to know if there is a good way to use prepareWithInvocationTarget: instead. — Charles ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/michael.starke%40hicknhack-software.com This email sent to michael.sta...@hicknhack-software.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUndoManager.prepareWithInvocationTarget:
On May 10, 2015, at 7:07 AM, Charles Jenkins cejw...@gmail.com wrote: Am I correct to believe there is NO way to use NSUndoManager.prepareWithInvocationTarget: if your undo method requires a parameter? I can use any method I want which has no parameter, but any method with parameters gets me the error “AnyObject does not have member named…” I thought about making a ClosureWrapper object which could have a parameterless method to call any method on a captured object, but since the invocation holds a weak reference, I think any instance of ClosureWrapper would be destroyed immediately after registration, so I didn’t bother trying. Right now I’m sticking with registerUndoWithTarget:selector:object:, but I’d like to know if there is a good way to use prepareWithInvocationTarget: instead. As was indicated, -prepareWithInvocationTarget: is the precise method used anytime you have a single non-object parameter or multiple parameters. Chances are, the error that you are experiencing is due to stringing a bunch of selectors together with the period operator, which doesn't always work as you expect. In particular, although I am not up to date with compilers, I am pretty sure you (still) cannot use dot notation to invoke methods that take parameters, so you must use bracket syntax: [NSUndoManager prepareWithInvocationTarget: aTarget] HTH, Keary Suska Esoteritech, Inc. Demystifying technology for your home or business ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com